[英]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),
),
),
),
),
],
),
);
}
}
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目录下。
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.