简体   繁体   English

Firebase 尽管调用了 Firebase.initializeApp() 但仍未初始化

[英]Firebase not initializing despite calling Firebase.initializeApp()

I'm writing my first Flutter up and trying to set up connection to Firebase. I have registered my app with Firebase as per instructions on the official website.我正在编写我的第一个 Flutter 并尝试建立与 Firebase 的连接。我已经按照官方网站上的说明使用 Firebase 注册了我的应用程序。 I added the core and auth packages in the.yaml file, updated build.gradle files and imported them in my source code.我在 .yaml 文件中添加了 core 和 auth 包,更新了 build.gradle 文件并将它们导入到我的源代码中。 Then I initialize Firebase by calling the proper method before starting the app.然后我通过在启动应用程序之前调用正确的方法来初始化 Firebase。 Whenever I try to sign in anonymously, I get the error:每当我尝试匿名登录时,都会收到错误消息:

[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: [core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp() [错误:flutter/lib/ui/ui_dart_state.cc(209)] 未处理的异常:[core/no-app] No Firebase App '[DEFAULT]' 已创建 - 调用 Firebase.initializeApp()

I really don't know what's going on as I think I'm calling the method according to FlutterFire docs.我真的不知道发生了什么,因为我认为我正在根据 FlutterFire 文档调用该方法。 Here's my main.dart file:这是我的 main.dart 文件:

import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:roomie/views/sign_in_page.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Roomie',
      theme: ThemeData(
        primarySwatch: Colors.pink,
      ),
      home: SignInPage(),
    );
  }
}

And here's my sign in method.这是我的登录方法。 Whenever I press the sign in button, I get the error that Firebase has not been initialized:每当我按下登录按钮时,我都会收到 Firebase 尚未初始化的错误消息:

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

class SignInPage extends StatelessWidget {

  Future<void> _signInAnonymously() async {
    final userCredentials = await FirebaseAuth.instance.signInAnonymously();
    print(userCredentials.user!.uid.toString());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Roomie'),
        elevation: 2.0,
      ),
      body: _buildContent(),
      backgroundColor: Colors.grey[200],
    );
  }

  Widget _buildContent() {
    return Padding(
      padding: EdgeInsets.all(16.0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text(
            'Sign In',
            textAlign: TextAlign.center,
            style: TextStyle(
              fontSize: 26.0,
              fontWeight: FontWeight.w500,
            ),
          ),
          SizedBox(
            height: 50.0,
            child: ElevatedButton(
              onPressed: _signInAnonymously,
              child: Text(
                'Sign in anonymously',
                style: TextStyle(
                  color: Colors.black87,
                ),
              ),
              style: ElevatedButton.styleFrom(
                primary: Colors.white,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(50),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
  1. I think it is either that you haven't placed the google_services.json file in the right path.我认为要么是您没有将 google_services.json 文件放在正确的路径中。 Ensure that the file is placed in the app directory under the android directory.确保文件放在android目录下的app目录下。

  2. Have you enabled Authentication in the Firebase console.您是否在 Firebase 控制台中启用了身份验证。 Check your project console, click on Authentication, and ensure you enable your chosen authentication method in the "Sign-In methods".检查您的项目控制台,单击身份验证,并确保在“登录方法”中启用您选择的身份验证方法。

火力控制台

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS 上的 Flutter:尽管在未来的构建器上调用 Firebase.initializeApp(),应用程序未初始化 - Flutter on iOS: Despite calling Firebase.initializeApp() on a future builder, app is not initialized 类型错误:firebase.initializeApp 不是 function - TypeError: firebase.initializeApp is not a function firebase.initializeApp() 与 FirebaseApp.initializeApp() - firebase.initializeApp() vs FirebaseApp.initializeApp() FirebaseError:firebase.initializeApp (NextJS) 中未提供“projectId” - FirebaseError: "projectId" not provided in firebase.initializeApp (NextJS) 没有创建 Firebase App - 调用 Firebase.initializeApp() - No Firebase App has been created - call Firebase.initializeApp() 在 WidgetsFlutterBinding.ensureInitialized() 中给出异常; 等待 Firebase.initializeApp(); - giving exception in WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); Firebase.initializeApp名称参数在移动端需要,web不需要 - Firebase.initializeApp name parameter is needed in mobile but not in web 没有创建 Firebase App '[DEFAULT]' - 尽管正在初始化 Firebase,但调用 Firebase.initializeApp()) - No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()) although Firebase is being initialized 没有创建 Firebase App '[DEFAULT]' - 在 Flutter 和 Firebase 调用 Firebase.initializeApp() - No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp() in Flutter and Firebase 没有创建 Firebase App '[DEFAULT]' - 调用 Firebase.initializeApp() -Flutter - No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp() -Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM