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.
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.
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 theWidgetsFlutterBinding.ensureInitialized()
first. If you're running a test, you can call theTestWidgetsFlutterBinding.ensureInitialized()
as the first line in your test'smain()
method to initialize the binding.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With