Trouble with running and hot reloading flutter in Android studio(Bad state: Future already completed)

This issue has been submitted to the Flutter github issues: https://github.com/flutter/flutter/issues/27450

I am having some trouble with running and hot reloading flutter apps in Android studio.

When I run an app in Android studio, I get the following message every now and again(2 out of 3 runs) and the build fails.

Launching lib/main.dart on iPhone XR in debug mode...
Unhandled exception:
Bad state: Future already completed
#0      _AsyncCompleter.complete (dart:async/future_impl.dart:39:31)
#1      _NativeSocket.startConnect.<anonymous closure>.connectNext.<anonymous closure> (dart:io/runtime/binsocket_patch.dart:490:23)
#2      _NativeSocket.issueWriteEvent.issue (dart:io/runtime/binsocket_patch.dart:844:14)
#3      _NativeSocket.issueWriteEvent (dart:io/runtime/binsocket_patch.dart:851:12)
#4      _NativeSocket.multiplex (dart:io/runtime/binsocket_patch.dart:872:11)
#5      _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)

I have not been able to get hot reload to work at all

Performing hot reload...
Syncing files to device iPhone XR...
Unhandled exception:
Bad state: Future already completed
#0      _AsyncCompleter.complete (dart:async/future_impl.dart:39:31)
#1      _NativeSocket.startConnect.<anonymous closure>.connectNext.<anonymous closure> (dart:io/runtime/binsocket_patch.dart:490:23)
#2      _NativeSocket.issueWriteEvent.issue (dart:io/runtime/binsocket_patch.dart:844:14)
#3      _NativeSocket.issueWriteEvent (dart:io/runtime/binsocket_patch.dart:851:12)
#4      _NativeSocket.multiplex (dart:io/runtime/binsocket_patch.dart:872:11)
#5      _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)

I am working on the stable channel. Here is my output of flutter doctor:

[✓] Flutter (Channel stable, v1.0.0, on Mac OS X 10.14.1 18B75, locale en-AU)
• Flutter version 1.0.0 at /Users/rakshak/flutter
• Framework revision 5391447fae (9 weeks ago), 2018-11-29 19:41:26 -0800
• Engine revision 7375a0f414
• Dart version 2.1.0 (build 2.1.0-dev.9.4 f9ebf21297)

⣽Unhandled exception:
Bad state: Future already completed
#0      _AsyncCompleter.complete (dart:async/future_impl.dart:39:31)
#1      _NativeSocket.startConnect.<anonymous closure>.connectNext.<anonymous closure> (dart:io/runtime/binsocket_patch.dart:490:23)
#2      _NativeSocket.issueWriteEvent.issue (dart:io/runtime/binsocket_patch.dart:844:14)
#3      _NativeSocket.issueWriteEvent (dart:io/runtime/binsocket_patch.dart:851:12)
#4      _NativeSocket.multiplex (dart:io/runtime/binsocket_patch.dart:872:11)
#5      _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)

I am working from China at the moment, I have implemented the changes mentioned here: https://flutter.io/community/china. Here are the relevant parts of my .bash_profile file

export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
export PATH=$PATH:/Users/rakshak/flutter/bin

I am working on a Mac.

This might look like a weird suggestion; however, did you try using vpn while completing this action? I once faced this problem, then using a vpn connection helped me. I guess, you may give it a try.

When flutter run or hot reload, flutter needs an active connection to local observatory port (on you desktop) so that those assets needed by hotreload (for example, dill files) can be synced to the vm-service (in your phone). There is a port forwarding between the observatory port and vm-service port.

If you are using a proxy, set the environment variable

NO_PROXY=127.0.0.1,localhost

// // Try running your application with "flutter run". You'll see the // application has a blue toolbar. Then, without quitting the app, try // changing the primarySwatch below to Colors.green and then invoke // "hot reload" (press "r" in the console where you ran "flutter run", // or simply save your changes to "hot reload" in a Flutter IDE).

I recommend you to install again flutter following the steps used in this popular flutter course video. I hope it will be worth watching for you and your error will be removed Flutter Installation Guide As you are from China, so you have to add these steps as mentioned in the link Using Flutter In China

[ ] Chrome - develop for the web • CHROME_EXECUTABLE = /usr/bin/brave [ ] Android Studio (version 3.5) • Android Studio at /opt/android-studio • Flutter plugin version 42.1.1 • Dart plugin version 191.8593 • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405) [ ] Connected device (3 available) • Redmi

Steps to Reproduce Good day guys, i'm having trouble on the hot reload of my sample app. When updating code on my main function the hot reload does'nt work. my guest is i that hot reload does'nt work when changes took place directly on t

2 Trouble with running and hot reloading flutter in Android studio(Bad state: Future already completed) Feb 8 '19 1 Can't run Flutter app in emulator or real device Jun 10 '19 1 Calculate double with add 0.1 gives lot of nummers behind the point Jun 16 '19

[ ] Android Studio (version 3.6) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin version 44.0.2 • Dart plugin version 192.7761 • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b4-5784211) [ ] IntelliJ IDEA Ultimate Edition (version 2019.3.3) • IntelliJ at /Applications/IntelliJ

This codelab assumes Android Studio, but you can use your preferred editor. Tip: One goal of this codelab is to introduce you to writing Flutter code in the Android Studio IDE. However, the code actually runs in DartPad, if you prefer to avoid the IDE instructions. Web support (optional): You might run your Flutter app only on a mobile device.

Comments
  • Submit an issue to github.com/flutter/flutter/issues/new
  • Done: github.com/flutter/flutter/issues/27450
  • stackoverflow.com/questions/51396769/…
  • Yeh, I think ill comment out the PUB_HOSTED_URL and FLUTTER_STORAGE_BASE_URL from my bask profile and just use a VPN.
  • I am not using a proxy.
  • Thanks for the reply. I have followed the installation steps from the Flutter dev site. I have also implemented the steps in the "Using flutter in China" link. It didn't seem to help.