[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Binding has not yet been initialized.

Problem:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: ‘package:flutter/src/services/platform_channel.dart’: Failed assertion: line 530 pos 7: ‘_binaryMessenger != null || BindingBase.debugBindingType() != null’: Cannot set the method call handler before the binary messenger has been initialized. This happens when you call setMethodCallHandler() 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 (15407): #0 _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:51:61)
E/flutter (15407): #1 _AssertionError._throwNew (dart:core-patch/errors_patch.dart:40:5)
E/flutter (15407): #2 MethodChannel.setMethodCallHandler (package:flutter/src/services/platform_channel.dart:530:7)
E/flutter (15407): #3 MethodChannelFirebaseMessaging.setMethodCallHandlers (package:firebase_messaging_platform_interface/src/method_channel/method_channel_messaging.dart:92:10)

Solution :

Just use following code in your main() function
WidgetsFlutterBinding.ensureInitialized();

for example:

void main() {
WidgetsFlutterBinding.ensureInitialized(); /// ****** Add This Line *******
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
runApp(const MyApp());
}

Leave a Reply