Flutter/Dart/firebase:: Error when trying to sign out

Advertisements

Not sure why I am not able to sign out.
When I click a button on the app bar, see ‘sign out’,
I click it,
system then asks me to confirm to sign out, I click it.

then I get an error and I am not signed out,

The expected result should be signing out without an error

CODE SNIP::

class _NotesViewState extends State<NotesView> {
  void _popupMenuActionsPressed(value) async {
    devtools.log('App bar $value was pressed');
    switch (value) {
      case MenuAction.logout:
        final shouldLogout = await showLogOutDialog(context);
        devtools.log('User logout');

        if (shouldLogout) {
          devtools.log('inside if statement');
          await FirebaseAuth.instance.signOut();
          Navigator.of(context)
              .pushNamedAndRemoveUntil(homeRoute, (_) => false);
        }

        break;
      default:
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Main UI'),
        actions: [
          PopupMenuButton<MenuAction>(
            onSelected: (value) {
              _popupMenuActionsPressed(value);
            },
            itemBuilder: (context) {
              return [
                const PopupMenuItem<MenuAction>(
                  value: MenuAction.logout,
                  child: Text('Sign out'),
                )
              ];
            },
          )
        ],
      ),
      body: const Text('Hello world'),
    );
  }
}

ERROR SNIP

User logout
[log] inside if statement
E/flutter (10193): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: [core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()
E/flutter (10193): #0      MethodChannelFirebase.app
package:firebase_core_platform_interface/…/method_channel/method_channel_firebase.dart:193
E/flutter (10193): #1      Firebase.app
package:firebase_core/src/firebase.dart:56
E/flutter (10193): #2      FirebaseAuth.instance
package:firebase_auth/src/firebase_auth.dart:44
E/flutter (10193): #3      _NotesViewState._popupMenuActionsPressed
package:ijob_clone_app/main.dart:81
E/flutter (10193): <asynchronous suspension>
E/flutter (10193):

>Solution :

The error is not from the code, it is because you are not initializing firebase in your main function.
Edit your main function as follows:

Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(myApp());
}

Leave a ReplyCancel reply