Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase.initializeApp() gives error: Null check operator used on a null value

running this

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

void main() async {
   await Firebase.initializeApp();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: ThePage(),
    );
  }
}

class ThePage extends StatelessWidget {
  const ThePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(          
    );
  }
}

is giving Null check operator used on a null value and pointing out the line Firebase.initializeApp(). I have tried flutter clean too.

and the error in the stack trace

E/flutter (31894): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Null check operator used on a null value
E/flutter (31894): #0      MethodChannel.binaryMessenger
package:flutter/…/services/platform_channel.dart:142
E/flutter (31894): #1      MethodChannel._invokeMethod
package:flutter/…/services/platform_channel.dart:148
E/flutter (31894): #2      MethodChannel.invokeMethod
package:flutter/…/services/platform_channel.dart:331
E/flutter (31894): #3      MethodChannel.invokeListMethod
package:flutter/…/services/platform_channel.dart:344
E/flutter (31894): #4      MethodChannelFirebase._initializeCore
package:firebase_core_platform_interface/…/method_channel/method_channel_firebase.dart:30
E/flutter (31894): #5      MethodChannelFirebase.initializeApp
package:firebase_core_platform_interface/…/method_channel/method_channel_firebase.dart:77
E/flutter (31894): #6      Firebase.initializeApp
package:firebase_core/src/firebase.dart:41
E/flutter (31894): #7      main
package:firebasetests/main.dart:5
E/flutter (31894): #8      _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:142:25)
E/flutter (31894): #9      _rootRun (dart:async/zone.dart:1354:13)
E/flutter (31894): #10     _CustomZone.run (dart:async/zone.dart:1258:19)
E/flutter (31894): #11     _runZoned (dart:async/zone.dart:1789:10)
E/flutter (31894): #12     runZonedGuarded (dart:async/zone.dart:1777:12)
E/flutter (31894): #13     _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:138:5)
E/flutter (31894): #14     _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:283:19)
E/flutter (31894): #15     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)

This is the stack trace for the error, after removing Firebase.initializeApp() in the main it runs fine.

like image 286
balu k Avatar asked Jun 16 '21 22:06

balu k


2 Answers

You should add the WidgetsFlutterBinding.ensureInitialized(); inside the main function:

void main() async {
  WidgetsFlutterBinding.ensureInitialized(); // Add this

  await Firebase.initializeApp();
  runApp(MyApp());
}

For Firebase initialization, access to the native code is needed using Flutter Platform Channels. For this, you need to ensure that Flutter engine binding is initialized.

like image 125
mkobuolys Avatar answered Sep 23 '22 09:09

mkobuolys


Use line of code which @mkobuolys mentioned.

I had same problem in Visual Studio Code but it didn't shows me any detailed information about error but Android Studio did.

Here is the most important information from the exception message if anyone is interested:

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.

like image 38
Samuel B. Avatar answered Sep 25 '22 09:09

Samuel B.