Home > Software design >  Flutter says Firebase app not initialised but initializeApp() is already used in void main()
Flutter says Firebase app not initialised but initializeApp() is already used in void main()

Time:03-13

I am getting the error

[core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()

But in my code I have already initialised it. Plus the android studio compiler says this before the app runs on the mobile

I/FirebaseApp(20998): Device unlocked: initializing all Firebase APIs for app x

My Code:

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  var fbApp = await Firebase.initializeApp();
  runApp(MyApp());
}

CodePudding user response:

I don't know if main() can be async or not, maybe it is source of your problem. Below is code which work for me.

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(MyApp());
}

and than i invoke initializeApp() method inside my state class.

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final Future<FirebaseApp> _initFirebaseSdk = Firebase.initializeApp();
// ... rest of my logic ...
}

CodePudding user response:

Initialize firebase without assigning to a variable

When you assign it to a variable, probably the compiler doesn't set it as the default instance.

Also remove the Future<void> and replace just void.
Then you can initialize the firebase app without assigning it to any variable.

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
}

If you are trying to access initialized firebase's (concrete)application, you can do it like:
And the same approach could be used for sub-services of the firebase.

FirebaseAuth.instance
  • Related