balu k
balu k

Reputation: 5098

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

This is giving Null check operator used on a null value and pointing out the line Firebase.initializeApp(). The issue persists even after running flutter clean.

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(          
    );
  }
}

Upvotes: 19

Views: 9083

Answers (3)

Kamla  Saad
Kamla Saad

Reputation: 11

add this line in android/app/build.gradle, this is worked for me

apply plugin: 'com.google.gms.google-services'

Upvotes: 0

Samuel B.
Samuel B.

Reputation: 466

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.

Upvotes: 2

mkobuolys
mkobuolys

Reputation: 5353

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.

Upvotes: 64

Related Questions