在Flutter投掷错误时初始化FIrebase

nzkunb0c  于 2023-01-05  发布在  Flutter
关注(0)|答案(3)|浏览(178)

我一直在尝试用flutter应用程序初始化我的firebase,但是每次都抛出错误,代码没有问题,因为flutter构建的应用程序很好,只是没有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();

这是我一直得到的错误:

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
vcudknz3

vcudknz31#

我通过以下步骤解决了此问题:
1.运行Flutter通道稳定
1.运行Flutter升级
1.将wait Firebase. initializeApp()替换为wait Firebase. initializeApp(选项:常量Firebase选项(apiKey:"用户名:"1:270790104828:网址:1da6b11a4729a7d79729",信息发送者ID:"2707901048",项目编号:"todo-app-firebase-ce8",),);
1.在firebase控制台上初始化Web应用程序并从那里复制脚本代码时,使用复制到index.html文件的值替换这些值。
1.重新启动应用程序。
对我有用!

sg3maiej

sg3maiej2#

import语句中缺少firebase_options文件,并且Firebase.initializeApp方法中缺少选项。
1.导入firebase_core插件和firebase_options文件。

//lib/main.dart
 import 'package:firebase_core/firebase_core.dart';
 import 'firebase_options.dart';

1.将options参数添加到main函数内的Firebase.initializeApp方法。

Future<void> main() async {
   WidgetsFlutterBinding.ensureInitialized();
   await Firebase.initializeApp(options: 
   DefaultFirebaseOptions.currentPlatform);
   runApp(const MyApp());
 }

FlutterFire文档-初始化-Flutterfire

2wnc66cl

2wnc66cl3#

我遇到了这个问题,所以我只是使用了这个命令,它被修复了。请在执行其他操作之前尝试一次

$ flutter upgrade

相关问题