'Flutter Emulator Error Connecting to Service Protocol

I am having an Issue with Flutter in Android studio, where whenever I run my flutter app, it will print this error; "Error connecting to the service protocol: HttpException: Connection closed before full header was received, uri = http://127.0.0.1:53305/9So9Wn564F4=/ws" , and it will "disconnect" from the app in the emulator. The app will still run, but I can't use functions like Hot Reload.

I am on a Windows machine, and Flutter Doctor does not indicate any errors. I have tried to read up on similar issues on github posts, but I have been unable to resolve the problem.

Do any of you guys have any ideas on how to solve this issue?

The problem does not occur when running on an actual device, only in the emulator.



Solution 1:[1]

If you return to a previous version of android, (Android Pie), you will no longer have this problem, it is related only to the latest versions of android (Q, 29), in others you will no longer have the problem.

Solution 2:[2]

A quick restart of the phone worked for me

Solution 3:[3]

What exactly happened

Hey I went into the same problem too. I cannot use a Emulator inside windows because my pc is a very low end pc. So, I had to use my phone instead. It is a Samsung Galaxy J2 Pro (SM-J210F) with Android 6.0.1 (Android Marshmallow). I used that phone to develop flutter apps regularly, but suddenly, I went to this problem.

Here's what happened when I tried to run "Flutter Attach" in VS-Code(v1.52.0).

command

flutter attach --machine -d 420054e7960eb400

exception

StateError: Bad state: Existing VM service clients prevent DDS from taking control.

#0      DartDevelopmentService.startDartDevelopmentService (package:flutter_tools/src/base/dds.dart:83:11)
<asynchronous suspension>
#1      FlutterDevice.connect.<anonymous closure> (package:flutter_tools/src/resident_runner.dart:249:11)
<asynchronous suspension>

Fixing the issue (In Windows 10)

So, I ran these commands to fix the issue.

Fixing the Flutter SDK

-flutter clean -v

-flutter channel stable ** (Optional if these didn't fix the issue)

-flutter upgrade -v

-flutter pub cache repair -v (Disclaimer: It will download each and every version of all the packages. Be sure to have really fast internet or give at least an hour to this.)

Configure your Proxy/VPN

If you are using a Proxy or VPN to connect to the internet,
Make sure that application isn't resolving localhost via that.(DNS)

Kill ADB and Dart processes.

After that, kill adb and dart process on Windows.
(I don't know exactly how to kill these in Mac/Linux. Sorry About That)
Run these commands in the Command Prompt.

taskkill /f /im dart*
taskkill /f /im adb* (You can run "adb kill-server" too)

Fixing the ADB in the Windows (*Optional).

Navigate to %HOMEDRIVE%:\Users%USERNAME%.android"
In my case, It is ( C:\Users\Isira Adithya.android )
Then delete adbkey.

Fixing the Android Debugging in the Android Device

Disconnect the device from the computer.
Go to Settings -> Developer Options,
Turn off USB Debugging and Turn it on again.
Revoke USB Debugging authorizations.
Restart the device.

Update Windows and Android System (If above didn't solve the problem)

Conclusion

I think these steps should fix your problem.
Also I am still 16 years old and I am not a very experienced developer.
If these didn't fix, use this to get more info. Github Issue

Solution 4:[4]

Uninstall app in Emulator fixed same issue for me

Solution 5:[5]

Sorry I don't know how you can solve it on Windows but for Mac Os users: Go to System Preferences -> Network -> Click on your Phone -> Uncheck "Disable unless needed" Uncheck Disable unless needed

Thanks to https://github.com/flutter/flutter/issues/46698#issuecomment-565643127

Solution 6:[6]

Try

flutter run --release

I faced the same issue after upgrading flutter 1.13 but the above command helped.

Solution 7:[7]

For my case, I met the error when I used 3rd parties such as facebook_login, twitter_login incorrectly.

To check:

  • comment the libs from pubspec.yaml, corresponding code as well.

To fix:

  • update correctly config for the 3rd parties.

Hope this help.

Solution 8:[8]

I had a simliar problem with ios with the error message Error connecting to the service protocol: failed to connect to http://127.0.0.1:....... I solved it by removing the app from the simulator and ran a clean in xcode, then it could build successfully from vs code!

Solution 9:[9]

For me it was caused by flutter_facebook_login plugin causing issue i removed it and i could afford to remove facebook login option but in your case you can try different version of flutter_facebook_login or you can use different plugin for loggin in using facebook.

Solution 10:[10]

In virtual device manager, try one of these options.

Wipe Data
Cold Boot Now

enter image description here

Solution 11:[11]

Try

flutter doctor -v

I had faced same issue. So I run flutter doctor -v command.

Then I found the error

? Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses.

flutter doctor --android-licenses this command resolved my issue.

Solution 12:[12]

I had faced the same issue but the only thing which solved my problem is to uninstall the app on my physical phone and I again try to run. and truly its work.

Solution 13:[13]

I solved this problem by:

  • turn off my phone.
  • disconnect.
  • turn on my phone.

By doing this, you will restart the VM Service.

Solution 14:[14]

check if you have enough storage in your phone.

I had the same problem and that was the reason.

Solution 15:[15]

It seems it can be different things. In my case, I removed a package I was using, barcode_scan.

Solution 16:[16]

I have the same problem and I go to xcode -> window -> device & simulator, under Installed Apps I remove some apps and it works.

Solution 17:[17]

Open the project in android studio => Start your Emulator - OR - Connect your android device with Debug mode "on" => Run the Project on android studio => Run your project from VS code or from command line using flutter run

Solution 18:[18]

When I deleted the emulator in AVD Manager and rebuilt it, it worked.

Solution 19:[19]

For me, it was caused by phone screen saver. Emulator screen was locked. After I pressed on power icon, the screen opened. then I run it again and everything is working.

Solution 20:[20]

I have resolved this problem after add some code in android folder.

1.Add below code in AndroidManifest.xml

 <meta-data android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/facebook_app_id"/>

    <activity android:name="com.facebook.FacebookActivity"
        android:configChanges=
            "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="@string/app_name" />
    <activity
        android:name="com.facebook.CustomTabActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="@string/fb_login_protocol_scheme" />
        </intent-filter>
    </activity>

2.Add your Facebook app id in strings.xml

<string name="facebook_app_id">380000000000000</string>
<string name="fb_login_protocol_scheme">fb380000000000000</string>

3.Add dependencies in pubspec.yaml

flutter_facebook_login: ^1.1.1

Pub get and run.

Solution 21:[21]

In my case, I just closed the simulator, and ran it again.

Solution 22:[22]

I was having the same error message. In my case, the problem was just a configuration that I changed in Runner.xcodeproj.

Open runner.xcodeproj in Xcode, go to the General tab -> App Icons and Lauch Images -> "Launch Screen File". I had changed it to Main and that cause the error, changing back to "LaunchScreen" option solved the problem for me.

Solution 23:[23]

This happened to me too, the problem was I was using a vpn in my PC, I turned the vpn off, and everything just worked fine

Solution 24:[24]

If you are using Flutter 2.10 or higher it may be that you tried to emulate a Windows desktop app but you don't have Visual Studio - develop for Windows (Visual Studio Community 2022 17.1.0) installed.

Apparently, it won't let you emulate anything until you install VS. Run flutter doctor to find out.

Solution 25:[25]

Open AVD from Android Studio and clear data of emulator and re-run again. it worked for me.