Home > Software engineering >  Cannot initialize firebase on android - Cannot use this MethodChannel before the binary messenger ha
Cannot initialize firebase on android - Cannot use this MethodChannel before the binary messenger ha

Time:03-05

I've created a flutter app with the skeleton template and I'm trying to test out some basic firestore features. I've followed the flutter fire docs and if I deploy the app to the web it works perfectly, but on android I get the following error:

E/flutter (26271): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: 'package:flutter/src/services/platform_channel.dart': Failed assertion: line 134 pos 7: '_binaryMessenger != null || ServicesBinding.instance != null': Cannot use this MethodChannel before the binary messenger has been initialized. This happens when you invoke platform methods before the WidgetsFlutterBinding has been initialized. You can fix this by either calling WidgetsFlutterBinding.ensureInitialized() before this or by passing a custom BinaryMessenger instance to MethodChannel().
E/flutter (26271): #0      _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:51:61)
E/flutter (26271): #1      _AssertionError._throwNew (dart:core-patch/errors_patch.dart:40:5)
E/flutter (26271): #2      MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:134:7)
E/flutter (26271): #3      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:167:36)
E/flutter (26271): #4      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:350:12)
E/flutter (26271): #5      MethodChannel.invokeListMethod (package:flutter/src/services/platform_channel.dart:363:41)
E/flutter (26271): #6      MethodChannelFirebase._initializeCore (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:31:37)
E/flutter (26271): #7      MethodChannelFirebase.initializeApp (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:73:13)
E/flutter (26271): #8      Firebase.initializeApp (package:firebase_core/src/firebase.dart:40:47)

The suggestion of adding WidgetsFlutterBinding.ensureInitialized doesn't seem to make sense as my main.dart is as follows:

    import 'package:fam/firebase_options.dart';
    import 'package:firebase_core/firebase_core.dart';
    import 'package:flutter/material.dart';
    import 'src/app.dart';
    import 'src/settings/settings_controller.dart';
    import 'src/settings/settings_service.dart';
    
    void main() async {
      WidgetsFlutterBinding.ensureInitialized;
    
      await Firebase.initializeApp(
        options: DefaultFirebaseOptions.currentPlatform,
      );
    
      final settingsController = SettingsController(SettingsService());
      await settingsController.loadSettings();
    
      runApp(MyApp(settingsController: settingsController));
    }

The app configuration on Firebase was all done through flutterfire configure so it's added all the relevant sections into the firebase_options.dart

My pubspec.yaml has the following added entries

cloud_firestore: ^3.1.10
firebase_core: ^1.13.1

I'm a bit stuck on what to try to resolve the issue.

CodePudding user response:

I use WidgetsFlutterBinding.ensureInitialized(); with parenthesis in my code. Maybe the missing parenthesis are the cause of your problem ?

  • Related