close
Flutter: Unhandled Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized

Flutter: Unhandled Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized

Hello Guys. from the last many time I have faced Flutter: Unhandled Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized this error in a flutter.

so finally I have decided to make a solution to this error. so without wasting your time let’s start this article.

How Unhandled Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized Error occur?

I am Facing This issue at my Stacktrace.

[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.
If you're running an application and need to access the binary messenger before `runApp()` has been called (for example, during plugin initialization), then you need to explicitly call the `WidgetsFlutterBinding.ensureInitialized()` first.
If you're running a test, you can call the `TestWidgetsFlutterBinding.ensureInitialized()` as the first line in your test's `main()` method to initialize the binding.
#0      defaultBinaryMessenger.<anonymous closure> (package:flutter/src/services/binary_messenger.dart:73:7)
#1      defaultBinaryMessenger (package:flutter/src/services/binary_messenger.dart:86:4)
#2      MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:140:62)
#3      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:314:35)
<asynchronous suspension>
#4      MethodChannel.invokeMapMethod (package:f<…>

How to solve Unhandled Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized ?

  1. How to solve Unhandled Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized ?

    to solve Unhandled Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized Main Reason behind this error is when you are awaiting on main() method. So, the solution would be. So, solution is to add WidgetsFlutterBinding.ensureInitialized(); at main.dart’s void main() method.

  2. Unhandled Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized

    to solve Unhandled Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized Main Reason behind this error is when you are awaiting on main() method. So, the solution would be. So, solution is to add WidgetsFlutterBinding.ensureInitialized(); at main.dart’s void main() method.

Solution 1

Flutter: Unhandled Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized

Main Reason behind this error is when you are awaiting on main() method. So, the solution would be:

void main() {
  // add this line at void main()
  WidgetsFlutterBinding.ensureInitialized(); 

  runApp(
    MaterialApp(...),
  );
}

So, solution is to add WidgetsFlutterBinding.ensureInitialized(); at main.dart’s void main() method.

Solution 2 :

just add this line in main.dart

WidgetsFlutterBinding.ensureInitialized(); 

your code seems like

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  return runApp(MultiProvider(
    providers: [
      ChangeNotifierProvider.value(
        value: AppState(),
      )
    ],
    child: MyApp(),
  ));
}

Here is Git Issue #47033

Summery

So, It’s All About This tutorial. I hope this tutorial helps you lot. Please Comment Below if You stucks anywhere with my code. Thank You.

Also Check Out Below Tutorials


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *