5

This error is not about firebase. Both shared_preferences and path_provider packages will report this error. This exception only appears on Android devices (both emulators and real devices). When building on Windows11, everything is normal. This exception troubles me because it suddenly occurs when I upgrade to flutter 3.19.6 and flutter upgrade some packages. All my codes should be normal:

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await ConfigService().initConfigurations();
  await DataStore().initDB();
  MediaKit.ensureInitialized();

  runApp(const ProviderScope(child: MyApp()));
}
  Future<void> initConfigurations() async {
    prefs = await SharedPreferences.getInstance();
  }
 Future<void> initDB() async {
    final dir = await getApplicationDocumentsDirectory();
}

ERROR:

 [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel., null, null)

It will be so good if anyone has clues to solve this problem. It came suddenly and I can no longer dev on Android since.

I have tried flutter pub outdated, flutter pub upgrade outdated_package, flutter clean, flutter pub get then flutter run. Those just won't work.

3

4 Answers 4

2

If the problem is specifically with release mode. It might be because Flutter obfuscates code and sometimes it create errors on Android or Java libraries. This might be the case if you see an error like this:

W/FlutterEngineCxnRegstry( 5882): Attempted to register plugin (J.a@3981a52) but it was already registered with this FlutterEngine (Y0.b@d346123).
W/FlutterEngineCxnRegstry( 5882): Attempted to register plugin (N0.e@3e1ec7f) but it was already registered with this FlutterEngine (Y0.b@d346123).
W/FlutterEngineCxnRegstry( 5882): Attempted to register plugin (J.a@67a944c) but it was already registered with this FlutterEngine (Y0.b@d346123).
W/FlutterEngineCxnRegstry( 5882): Attempted to register plugin (N0.e@5e2a938) but it was already registered with this FlutterEngine (Y0.b@d346123).
W/FlutterEngineCxnRegstry( 5882): Attempted to register plugin (J.a@e156750) but it was already registered with this FlutterEngine (Y0.b@d346123).
W/FlutterEngineCxnRegstry( 5882): Attempted to register plugin (V0.a@c778d49) but it was already registered with this FlutterEngine (Y0.b@d346123).
E/flutter ( 5882): [ERROR:flutter/runtime/dart_vm_initializer.cc(40)] Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel: "dev.flutter.pigeon.shared_preferences_android.SharedPreferencesApi.getAll"., null, null)

Here are the things I tried:

1. Add this to the app/build.gradle file:

buildTypes {  
release {  
            // add this lines  
            minifyEnabled true  
            shrinkResources true  
            proguardFiles getDefaultProguardFile(  
                    'proguard-android-optimize.txt'),  
                    'proguard-rules.pro'  
        }

2. If that doesn't fix it then, add this to the proguard-rules.pro file:

-if class * implements io.flutter.embedding.engine.plugins.FlutterPlugin
-keep,allowshrinking,allowobfuscation class <1>

See: Issue#154580 for more details

Sign up to request clarification or add additional context in comments.

Comments

1

A temporary solution is changing minifyEnabled to false in your app-level gradle to solve the issue. Probably minifyEnabled: true with a proper proguard file is a more favor solution, I will try to update it

   buildTypes {
    debug {
        minifyEnabled false
        shrinkResources false
        debuggable true
        signingConfig signingConfigs.debug
    }
   release {
        minifyEnabled false
        shrinkResources false
        signingConfig signingConfigs.release
   }

}

1 Comment

2024 not working anymore
1

Double check your Android emulator to make sure it targets the same API level for your Flutter project.

I had the same issue when my emulator was targeting API level 30 and creating an emulator that target API level 33 or 34 fixed it.

Comments

1

If you are using native bridge and adding flutter module into native iOS code add this line where you are initialising flutter engine GeneratedPluginRegistrant.register(with: flutterEngine!)

and

import FlutterPluginRegistrant

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.