首页 > 解决方案 > 添加初始化 firebase_core 后,Flutter App 卡在加载屏幕

问题描述

添加 Firebase.initializeApp() 后,我的应用卡在加载屏幕上,没有 Firebase.initializeApp(),它会显示我的登录屏幕,但如果不初始化 firebase_core,我无法在 pubspec.yaml 文件中使用任何 firebase 插件。

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



class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: themeData(),
      home:Login()
    );
  }

试过这个,它加载错误屏幕并显示错误:future returned null

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

    class App extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return FutureBuilder(
          // Initialize FlutterFire
          future: Firebase.initializeApp(),
          builder: (context, snapshot) {
            // Check for errors
            if (snapshot.hasError) {
               print(snapshot.error.toString);
               return Error();
            }
    
            // Once complete, show your application
            if (snapshot.connectionState == ConnectionState.done) {
              return Login();
            }
    
            // Otherwise, show something whilst waiting for initialization to complete
            return Loading();
          },
        );
      }
    }


pubpec.yaml file
      google_sign_in: ^4.5.5
      uuid: ^2.2.2
      path_provider: ^1.6.21
      firebase_storage: ^5.0.0-dev.2
      firebase_messaging: ^7.0.3
      cloud_firestore: ^0.14.1+3
      firebase_auth: ^0.18.1+2
      firebase_core: ^0.5.0+1



flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[v] Flutter (Channel stable, v1.17.4, on Microsoft Windows [Version 10.0.10240], locale en-US)
[v] Android toolchain - develop for Android devices (Android SDK version 29.0.0)
[v] Android Studio (version 3.4)
[v] VS Code (version 1.50.1)
[v] Connected device (1 available)

标签: firebaseflutter

解决方案


推荐阅读