Flutter 误差:Firebase异常([core/no-app]未创建Firebase应用程序"[DEFAULT]"-调用Firebase. initializeApp())

lnlaulya  于 2023-02-09  发布在  Flutter
关注(0)|答案(2)|浏览(247)

我知道我必须做Firebase.initializeApp(),但我不知道把它放在哪里,我尝试了一些关于同一问题的其他问题的答案,但没有一个工作。
我还尝试在void main()中添加WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp();

主省道

void main() => runApp(MyApp());

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

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {

    final AuthenticationService _authenticationService =
        AuthenticationService();
    final AuthenticationBloc _authenticationBloc =
        AuthenticationBloc(_authenticationService);

    return AuthenticationBlocProvider(
      authenticationBloc: _authenticationBloc,
      key: null,
      child: StreamBuilder(
        initialData: null,
        stream: _authenticationBloc.user,
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return Container(
              color: Colors.pink,
              child: CircularProgressIndicator(),
            );
          } else if (snapshot.hasData) {
            return HomeBlocProvider(
              homeBloc: HomeBloc(DbFirestoreService(),
                  _authenticationService), // Inject the DbFirestoreService() & AuthenticationService()
              uid: snapshot.data,
              key: null,
              child: _buildMaterialApp(HomePage()),
            );
          } else {
            return _buildMaterialApp(Login());
          }
        },
      ),
    );
  }

  MaterialApp _buildMaterialApp(Widget homePage) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Journal',
      theme: ThemeData(
        primarySwatch: Colors.lightGreen,
        canvasColor: Colors.lightGreen.shade50,
        bottomAppBarColor: Colors.lightGreen,
      ),
      home: homePage,
    );
  }
}

验证日期

class AuthenticationService implements AuthenticationApi {
  final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;

  FirebaseAuth getFirebaseAuth() {
    return _firebaseAuth;
  }

  Future<String> currentUserUid() async {
    var user = _firebaseAuth.currentUser;
    return user!.uid;
  }

  Future<void> signOut() async {
    return _firebaseAuth.signOut();
  }

  Future<String> signInWithEmailAndPassword(
      {required String email, required String password}) async {
    final UserCredential user = await _firebaseAuth.signInWithEmailAndPassword(
        email: email, password: password);
    return user.user!.uid;
  }

  Future<String> createUserWithEmailAndPassword(
      {required String email, required String password}) async {
    final UserCredential user = await _firebaseAuth
        .createUserWithEmailAndPassword(email: email, password: password);
    return user.user!.uid;
  }
}

我是新来的,谢谢你的帮助

elcex8rz

elcex8rz1#

你应该像这样初始化你的Firebase:

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

确保导入Firebase核心

import 'package:firebase_core/firebase_core.dart';
nc1teljy

nc1teljy2#

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

...

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await Firebase.initializeApp({
    appId: 'your_app_id',
    apiKey: 'your_db_api_key',
    messagingSenderId: 'your_sender_id',
    projectId: 'your_project name',
  });

  runApp(MyApp());
}

...

这些app_id、API_key、sender_id、project_name可以从firebase控制台轻松获得。

相关问题