skip to Main Content

the app still only shows a blank screen. Here is my main app code:

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: Scaffold(
        appBar: AppBar(title: Text('Firebase Integration')),
        body: Center(child: Text('Hello, Firebase!')),
      ),
    );
  }
}

Any help or suggestions would be greatly appreciated!

3

Answers


  1. It looks like you’re missing WidgetsFlutterBinding.ensureInitialized(); before initializing Firebase:

    void main() async {
      WidgetsFlutterBinding.ensureInitialized(); // Add this line
      await Firebase.initializeApp();
      runApp(MyApp());
    }
    
    Login or Signup to reply.
  2. Add WidgetsFlutterBinding.ensureInitialized(); before initializing Firebase. Check the below code for reference.

    import 'package:flutter/material.dart';
    import 'package:firebase_core/firebase_core.dart';
    
    Future<void> main() async {
      WidgetsFlutterBinding.ensureInitialized(); // Ensure widgets binding is initialized
      try {
        await Firebase.initializeApp();
        runApp(MyApp());
      } catch (e) {
        print("Failed to initialize Firebase: $e");
        exit(0); // Exit the app if Firebase fails to initialize
      }
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(title: Text('Firebase Integration')),
            body: Center(child: Text('Hello, Firebase')),
          ),
        );
      }
    }
    
    Login or Signup to reply.
  3. You should initialize WidgetsFlutterBinding as it is said in other comment, the main in my app with Flutter & Firebase is:

    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await Firebase.initializeApp(
        options: DefaultFirebaseOptions.currentPlatform,
      );
      await Environment.initEnvirnoment();
      runApp(
        const ProviderScope(child: MainApp())
      );
    }
    

    In your case you don’t need to call the await Environment.initEnvirnoment(); or the ProviderScope

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search