在 Flutter 上初始化 FRebase 会引发错误

pet*_*nja 3 firebase flutter

所以我一直在尝试用我的 flutter 应用程序初始化我的 firebase,但它每次都会抛出错误,代码没有问题,因为 flutter 构建应用程序很好,但不是 firebase。这是我初始化 firebase 的代码;

import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';

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

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool _initialized = false;
  bool _error = false;

  void initializeFlutterFire() async {
    try {
      // Wait for Firebase to initialize and set `_initialized` state to true
      await Firebase.initializeApp();
      setState(() {
        _initialized = true;
      });
    } catch (e) {
      // Set `_error` state to true if Firebase initialization fails
      setState(() {
        _error = true;
      });
    }
  }

  @override
  void initState() {
    initializeFlutterFire();
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp();
Run Code Online (Sandbox Code Playgroud)

这是我不断收到的错误:

Error: Assertion failed:
file:///home/pete/snap/flutter/common/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_core_web-1.4.0/lib/src/fire
base_core_web.dart:271:11
options != null
"FirebaseOptions cannot be null when creating the default app."
    at Object.throw_ [as throw] (http://localhost:35305/dart_sdk.js:5061:11)
    at Object.assertFailed (http://localhost:35305/dart_sdk.js:4986:15)
    at firebase_core_web.FirebaseCoreWeb.new.initializeApp
    (http://localhost:35305/packages/firebase_core_web/firebase_core_web.dart.lib.js:243:42)
    at initializeApp.next (<anonymous>)
    at http://localhost:35305/dart_sdk.js:38640:33
    at _RootZone.runUnary (http://localhost:35305/dart_sdk.js:38511:59)
    at _FutureListener.thenAwait.handleValue (http://localhost:35305/dart_sdk.js:33713:29)
    at handleValueCallback (http://localhost:35305/dart_sdk.js:34265:49)
    at Function._propagateToListeners (http://localhost:35305/dart_sdk.js:34303:17)
    at _Future.new.[_completeWithValue] (http://localhost:35305/dart_sdk.js:34151:23)
    at async._AsyncCallbackEntry.new.callback (http://localhost:35305/dart_sdk.js:34172:35)
    at Object._microtaskLoop (http://localhost:35305/dart_sdk.js:38778:13)
    at _startMicrotaskLoop (http://localhost:35305/dart_sdk.js:38784:13)
    at http://localhost:35305/dart_sdk.js:34519:9
Run Code Online (Sandbox Code Playgroud)

小智 11

我通过以下步骤修复了该问题:

  1. 运行flutter通道稳定
  2. 运行颤振升级
  3. 将等待Firebase.initializeApp()替换为等待Firebase.initializeApp(选项:const FirebaseOptions(apiKey:“AIzaSyD1C8QaEAxv9QJIm2DDF9N3_b3UZv5o”,appId:“1:270790104828:web:1da6b11a4729a7d79729”,messagingSenderId:“27079010 48”,projectId:“todo-app-firebase -ce8",),);
  4. 将这些值替换为在 Firebase 控制台上初始化 Web 应用程序并从那里复制脚本代码时复制到 index.html 文件的值。
  5. 重新启动应用程序。

为我工作!