调用Firebase.initializeApp(),但我已经在我的代码中有它了。

13 浏览
0 Comments

调用Firebase.initializeApp(),但我已经在我的代码中有它了。

这个问题已经有了答案

在Flutter和Firebase中没有创建默认的Firebase App \'[DEFAULT]\' - 调用Firebase.initializeApp()

所以我想用firebase制作一个flutter应用程序,它运行得很好,但是当我运行应用程序时,我会得到这个错误:

════════ Exception caught by widgets library ═══════════════════════════════════
The following FirebaseException was thrown building _BodyBuilder:
[core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()
    The relevant error-causing widget was
    Scaffold
    package:app_with_firebase/main.dart:14
    When the exception was thrown, this was the stack
    #0      MethodChannelFirebase.app
    package:firebase_core_platform_interface/…/method_channel/method_channel_firebase.dart:122
    #1      Firebase.app
    package:firebase_core/src/firebase.dart:54
    #2      FirebaseAuth.instance
    package:firebase_auth/src/firebase_auth.dart:37
    #3      new AuthService
    package:app_with_firebase/Services/auth.dart:4
    #4      new _LoginPageState
    package:app_with_firebase/main.dart:29
    ...
    ════════════════════════════════════════════════════════════════════════════════

但问题是我已经在我的代码里有这行:

//import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'Services/auth.dart';
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  Firebase.initializeApp();
  runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: LoginPage(),
      ),
    ),
  );
}

我该如何去掉这个错误?应用程序运行得很好,但这非常令人分心,任何帮助都将不胜感激,谢谢。

admin 更改状态以发布 2023年5月24日
0
0 Comments

在Firebase.initializeApp()之前添加await关键字;\n由于它是一个耗时的函数,你应该在该语句中等待一段时间。如果你忘记添加await关键字,则应返回null值。\n

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(
  MaterialApp(
    debugShowCheckedModeBanner: false,
    home: Scaffold(
      body: LoginPage(),
    ),
  ),
);
}

0