0
votes

How to solve this issue? I need to check the user is logged in or not

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized. E/flutter (21284): 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. E/flutter (21284): 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. E/flutter (21284): #0 defaultBinaryMessenger. (package:flutter/src/services/binary_messenger.dart:76:7) E/flutter (21284): #1 defaultBinaryMessenger (package:flutter/src/services/binary_messenger.dart:89:4) E/flutter (21284): #2 MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:140:62) E/flutter (21284): #3 MethodChannel.setMethodCallHandler (package:flutter/src/services/platform_channel.dart:368:5) E/flutter (21284): #4 new FirebaseAuth._ (package:firebase_auth/src/firebase_auth.dart:15:13) E/flutter (21284): #5 FirebaseAuth.instance (package:firebase_auth/src/firebase_auth.dart:25:53) E/flutter (21284): #6 FirebaseAuth.instance (package:firebase_auth/src/firebase_auth.dart:25:29) E/flutter (21284): #7 main (package:alqaiser/main.dart:44:36) E/flutter (21284): #8 _runMainZoned.. (dart:ui/hooks.dart:239:25) E/flutter (21284): #9 _rootRun (dart:async/zone.dart:1126:13) E/flutter (21284): #10
_CustomZone.run (dart:async/zone.dart:1023:19) E/flutter (21284): #11 _runZoned (dart:async/zone.dart:1518:10) E/flutter (21284): #12 runZoned (dart:async/zone.dart:1502:12) E/flutter (21284): #13
_runMainZoned. (dart:ui/hooks.dart:231:5) E/flutter (21284): #14 _startIsolate. (dart:isolate-patch/isolate_patch.dart:307:19) E/flutter (21284): #15 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12) E/flutter (21284):

This is my man function

void main() async {
  FirebaseAuth auth = FirebaseAuth.instance;
  auth.currentUser().then((u) {
    if (u == null?true:u.phoneNumber==null?true:false) {
      runApp(MaterialApp(

        home: PhoneAuth(),
      ));
    } else {
      runApp(MyApp());
    }
  }).catchError((e) {
    print(e);
    runApp(MyAppT2(e.toString() + ";"));
  });
}
2

2 Answers

2
votes

you can put WidgetsFlutterBinding.ensureInitialized(); in first line
code snippet

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  FirebaseAuth auth = FirebaseAuth.instance;
  auth.currentUser().then((u) {
    if (u == null?true:u.phoneNumber==null?true:false) {
      runApp(MaterialApp(

        home: PhoneAuth(),
      ));
    } else {
      runApp(MyApp());
    }
  }).catchError((e) {
    print(e);
    runApp(MyAppT2());
  });
}

full test code

import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  FirebaseAuth auth = FirebaseAuth.instance;
  auth.currentUser().then((u) {
    if (u == null?true:u.phoneNumber==null?true:false) {
      runApp(MaterialApp(

        home: PhoneAuth(),
      ));
    } else {
      runApp(MyApp());
    }
  }).catchError((e) {
    print(e);
    runApp(MyAppT2());
  });
}

class PhoneAuth extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}


class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyAppT2 extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}
0
votes

I had the same issue and I just added WidgetsFlutterBinding.ensureInitialized(); in the first line of my main and the problem was gone. The ensureInitialized method, I quote flutters documentation,

(https://api.flutter.dev/flutter/widgets/WidgetsFlutterBinding/ensureInitialized.html)

returns an instance of the WidgetsBinding, creating and initializing it if necessary. If one is created, it will be a WidgetsFlutterBinding. If one was previously initialized, then it will at least implement WidgetsBinding.

You only need to call this method if you need the binding to be initialized before calling runApp.