错误:[core/no-app] no Firebase App' [默认]'已创建 - 致电firebase.initializeapp()
我正在构建一个颤抖的应用程序,并且已经集成了Firebase,但是当我单击登录按钮时,我会遇到此错误。我遇到了有类似问题的人,但似乎没有人对我有用。我正在使用VS代码作为我的IDE。如何解决此问题?
这是我每个文件的代码...
main.dart文件
import 'login_screen.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
title: "FLUTTER-FIREBASE LOGIN APP",
home: LoginScreen(),
);
}
}
login_screen.dart文件
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'profile_screen.dart';
class LoginScreen extends StatefulWidget {
const LoginScreen({Key? key}) : super(key: key);
@override
State<LoginScreen> createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
// Initialize Firebase App
Future<FirebaseApp> _initializeFirebase() async {
FirebaseApp firebaseApp = await Firebase.initializeApp();
return firebaseApp;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder(
future: _initializeFirebase(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return const LoginForm();
}
return const Center(
child: CircularProgressIndicator(),
);
},
),
);
}
}
class LoginForm extends StatefulWidget {
const LoginForm({Key? key}) : super(key: key);
@override
State<LoginForm> createState() => _LoginFormState();
}
class _LoginFormState extends State<LoginForm> {
// Login Function
static Future<User?> loginUsingEmailPassword(
{required String email,
required String password,
required BuildContext context}) async {
FirebaseAuth auth = FirebaseAuth.instance;
User? user;
try {
UserCredential userCredential = await auth.signInWithEmailAndPassword(
email: email,
password: password,
);
user = userCredential.user;
} on FirebaseAuthException catch (e) {
if (e.code == "user-not-found") {
print("No user found for that email");
}
}
return user;
}
@override
Widget build(BuildContext context) {
// TextFielf Controller
TextEditingController _emailController = TextEditingController();
TextEditingController _passwordController = TextEditingController();
return Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
const Text(
"MyApp Title",
style: TextStyle(
color: Colors.black,
fontSize: 28,
fontWeight: FontWeight.bold,
),
),
const Text(
"Login to your App",
style: TextStyle(
color: Colors.black,
fontSize: 44.0,
fontWeight: FontWeight.bold,
),
),
const SizedBox(
height: 44.0,
),
TextField(
controller: _emailController,
keyboardType: TextInputType.emailAddress,
decoration: const InputDecoration(
hintText: "enter email...",
prefixIcon: Icon(
Icons.mail,
color: Colors.black,
)),
),
const SizedBox(
height: 26.0,
),
TextField(
controller: _passwordController,
obscureText: true,
decoration: const InputDecoration(
hintText: "enter password...",
prefixIcon: Icon(
Icons.lock,
color: Colors.black,
),
),
),
const SizedBox(
height: 12.0,
),
const Text(
"Forgot Password?",
style: TextStyle(
color: Colors.blue,
),
),
const SizedBox(
height: 88.0,
),
Container(
width: double.infinity,
child: RawMaterialButton(
fillColor: const Color(0xFF0069FE),
elevation: 0.0,
padding: const EdgeInsets.symmetric(vertical: 20.0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12.0),
),
onPressed: () async {
User? user = await loginUsingEmailPassword(
email: _emailController.text,
password: _passwordController.text,
context: context);
print(user);
if (user != null) {
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (context) => const ProfileScreen()));
}
},
child: const Text(
"Login",
style: TextStyle(
color: Colors.white,
fontSize: 18.0,
fontWeight: FontWeight.bold,
),
),
),
)
],
),
);
}
}
profile_screen.dart
class ProfileScreen extends StatefulWidget {
const ProfileScreen({ Key? key }) : super(key: key);
@override
State<ProfileScreen> createState() => _ProfileScreenState();
}
class _ProfileScreenState extends State<ProfileScreen> {
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(
child: Text("TO BE CONTINUED...!!!"),
),
);
}
}```
**This is the error message I get when I tap on the login button**
Error: [core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()
at Object.throw_ [as throw] (http://localhost:51555/dart_sdk.js:5067:11)
at firebase_core_web.FirebaseCoreWeb.new.app (http://localhost:51555/packages/firebase_core_web/firebase_core_web.dart.lib.js:285:23)
at Function.app (http://localhost:51555/packages/firebase_core/firebase_core.dart.lib.js:111:50)
at Function.get instance [as instance] (http://localhost:51555/packages/firebase_auth/firebase_auth.dart.lib.js:96:55)
at loginUsingEmailPassword (http://localhost:51555/packages/flutter_login_with_firebase_authentication/login_screen.dart.lib.js:1047:47)
at loginUsingEmailPassword.next (<anonymous>)
at runBody (http://localhost:51555/dart_sdk.js:40590:34)
at Object._async [as async] (http://localhost:51555/dart_sdk.js:40621:7)
at Function.loginUsingEmailPassword (http://localhost:51555/packages/flutter_login_with_firebase_authentication/login_screen.dart.lib.js:1046:20)
at http://localhost:51555/packages/flutter_login_with_firebase_authentication/login_screen.dart.lib.js:1068:66
at Generator.next (<anonymous>)
at runBody (http://localhost:51555/dart_sdk.js:40590:34)
at Object._async [as async] (http://localhost:51555/dart_sdk.js:40621:7)
at http://localhost:51555/packages/flutter_login_with_firebase_authentication/login_screen.dart.lib.js:1067:1029
at ink_well._InkResponseState.new.[_handleTap] (http://localhost:51555/packages/flutter/src/material/icon_button.dart.lib.js:40468:31)
at tap.TapGestureRecognizer.new.invokeCallback (http://localhost:51555/packages/flutter/src/gestures/recognizer.dart.lib.js:190:18)
at LinkedMap.new.forEach (http://localhost:51555/dart_sdk.js:27679:11)
at pointer_router.PointerRouter.new.[_dispatchEventToRoutes] (http://localhost:51555/packages/flutter/src/gestures/pointer_router.dart.lib.js:110:29)
at pointer_router.PointerRouter.new.route (http://localhost:51555/packages/flutter/src/gestures/pointer_router.dart.lib.js:105:37)
at binding$5.WidgetsFlutterBinding.new.handleEvent (http://localhost:51555/packages/flutter/src/gestures/binding.dart.lib.js:364:26)
at binding$5.WidgetsFlutterBinding.new.dispatchEvent (http://localhost:51555/packages/flutter/src/gestures/binding.dart.lib.js:352:24)
at binding$5.WidgetsFlutterBinding.new.dispatchEvent (http://localhost:51555/packages/flutter/src/rendering/layer.dart.lib.js:5427:13)
at binding$5.WidgetsFlutterBinding.new.[_handlePointerEventImmediately] (http://localhost:51555/packages/flutter/src/gestures/binding.dart.lib.js:328:14)
at binding$5.WidgetsFlutterBinding.new.handlePointerEvent (http://localhost:51555/packages/flutter/src/gestures/binding.dart.lib.js:302:43)
at binding$5.WidgetsFlutterBinding.new.[_flushPointerEventQueue] (http://localhost:51555/packages/flutter/src/gestures/binding.dart.lib.js:292:14)
at binding$5.WidgetsFlutterBinding.new.[_handlePointerDataPacket] (http://localhost:51555/packages/flutter/src/gestures/binding.dart.lib.js:283:54)
at Object.invoke1 (http://localhost:51555/dart_sdk.js:190405:7)
at _engine.EnginePlatformDispatcher.__.invokeOnPointerDataPacket (http://localhost:51555/dart_sdk.js:171081:15)
at _engine.PointerBinding.__.[_onPointerData] (http://localhost:51555/dart_sdk.js:171963:49)
at http://localhost:51555/dart_sdk.js:172401:28
at http://localhost:51555/dart_sdk.js:172357:16
at loggedHandler (http://localhost:51555/dart_sdk.js:172062:11)
I am building a Flutter application and I have integrated Firebase, but I keep getting this error when I click on the login button. I have come across people with similar problem, but none seems to work for me. I am using VS Code as my IDE. How can I fix this problem?
Here is my code for each file...
main.dart file
import 'login_screen.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
title: "FLUTTER-FIREBASE LOGIN APP",
home: LoginScreen(),
);
}
}
login_screen.dart file
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'profile_screen.dart';
class LoginScreen extends StatefulWidget {
const LoginScreen({Key? key}) : super(key: key);
@override
State<LoginScreen> createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
// Initialize Firebase App
Future<FirebaseApp> _initializeFirebase() async {
FirebaseApp firebaseApp = await Firebase.initializeApp();
return firebaseApp;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder(
future: _initializeFirebase(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return const LoginForm();
}
return const Center(
child: CircularProgressIndicator(),
);
},
),
);
}
}
class LoginForm extends StatefulWidget {
const LoginForm({Key? key}) : super(key: key);
@override
State<LoginForm> createState() => _LoginFormState();
}
class _LoginFormState extends State<LoginForm> {
// Login Function
static Future<User?> loginUsingEmailPassword(
{required String email,
required String password,
required BuildContext context}) async {
FirebaseAuth auth = FirebaseAuth.instance;
User? user;
try {
UserCredential userCredential = await auth.signInWithEmailAndPassword(
email: email,
password: password,
);
user = userCredential.user;
} on FirebaseAuthException catch (e) {
if (e.code == "user-not-found") {
print("No user found for that email");
}
}
return user;
}
@override
Widget build(BuildContext context) {
// TextFielf Controller
TextEditingController _emailController = TextEditingController();
TextEditingController _passwordController = TextEditingController();
return Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
const Text(
"MyApp Title",
style: TextStyle(
color: Colors.black,
fontSize: 28,
fontWeight: FontWeight.bold,
),
),
const Text(
"Login to your App",
style: TextStyle(
color: Colors.black,
fontSize: 44.0,
fontWeight: FontWeight.bold,
),
),
const SizedBox(
height: 44.0,
),
TextField(
controller: _emailController,
keyboardType: TextInputType.emailAddress,
decoration: const InputDecoration(
hintText: "enter email...",
prefixIcon: Icon(
Icons.mail,
color: Colors.black,
)),
),
const SizedBox(
height: 26.0,
),
TextField(
controller: _passwordController,
obscureText: true,
decoration: const InputDecoration(
hintText: "enter password...",
prefixIcon: Icon(
Icons.lock,
color: Colors.black,
),
),
),
const SizedBox(
height: 12.0,
),
const Text(
"Forgot Password?",
style: TextStyle(
color: Colors.blue,
),
),
const SizedBox(
height: 88.0,
),
Container(
width: double.infinity,
child: RawMaterialButton(
fillColor: const Color(0xFF0069FE),
elevation: 0.0,
padding: const EdgeInsets.symmetric(vertical: 20.0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12.0),
),
onPressed: () async {
User? user = await loginUsingEmailPassword(
email: _emailController.text,
password: _passwordController.text,
context: context);
print(user);
if (user != null) {
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (context) => const ProfileScreen()));
}
},
child: const Text(
"Login",
style: TextStyle(
color: Colors.white,
fontSize: 18.0,
fontWeight: FontWeight.bold,
),
),
),
)
],
),
);
}
}
profile_screen.dart
class ProfileScreen extends StatefulWidget {
const ProfileScreen({ Key? key }) : super(key: key);
@override
State<ProfileScreen> createState() => _ProfileScreenState();
}
class _ProfileScreenState extends State<ProfileScreen> {
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(
child: Text("TO BE CONTINUED...!!!"),
),
);
}
}```
**This is the error message I get when I tap on the login button**
Error: [core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()
at Object.throw_ [as throw] (http://localhost:51555/dart_sdk.js:5067:11)
at firebase_core_web.FirebaseCoreWeb.new.app (http://localhost:51555/packages/firebase_core_web/firebase_core_web.dart.lib.js:285:23)
at Function.app (http://localhost:51555/packages/firebase_core/firebase_core.dart.lib.js:111:50)
at Function.get instance [as instance] (http://localhost:51555/packages/firebase_auth/firebase_auth.dart.lib.js:96:55)
at loginUsingEmailPassword (http://localhost:51555/packages/flutter_login_with_firebase_authentication/login_screen.dart.lib.js:1047:47)
at loginUsingEmailPassword.next (<anonymous>)
at runBody (http://localhost:51555/dart_sdk.js:40590:34)
at Object._async [as async] (http://localhost:51555/dart_sdk.js:40621:7)
at Function.loginUsingEmailPassword (http://localhost:51555/packages/flutter_login_with_firebase_authentication/login_screen.dart.lib.js:1046:20)
at http://localhost:51555/packages/flutter_login_with_firebase_authentication/login_screen.dart.lib.js:1068:66
at Generator.next (<anonymous>)
at runBody (http://localhost:51555/dart_sdk.js:40590:34)
at Object._async [as async] (http://localhost:51555/dart_sdk.js:40621:7)
at http://localhost:51555/packages/flutter_login_with_firebase_authentication/login_screen.dart.lib.js:1067:1029
at ink_well._InkResponseState.new.[_handleTap] (http://localhost:51555/packages/flutter/src/material/icon_button.dart.lib.js:40468:31)
at tap.TapGestureRecognizer.new.invokeCallback (http://localhost:51555/packages/flutter/src/gestures/recognizer.dart.lib.js:190:18)
at LinkedMap.new.forEach (http://localhost:51555/dart_sdk.js:27679:11)
at pointer_router.PointerRouter.new.[_dispatchEventToRoutes] (http://localhost:51555/packages/flutter/src/gestures/pointer_router.dart.lib.js:110:29)
at pointer_router.PointerRouter.new.route (http://localhost:51555/packages/flutter/src/gestures/pointer_router.dart.lib.js:105:37)
at binding$5.WidgetsFlutterBinding.new.handleEvent (http://localhost:51555/packages/flutter/src/gestures/binding.dart.lib.js:364:26)
at binding$5.WidgetsFlutterBinding.new.dispatchEvent (http://localhost:51555/packages/flutter/src/gestures/binding.dart.lib.js:352:24)
at binding$5.WidgetsFlutterBinding.new.dispatchEvent (http://localhost:51555/packages/flutter/src/rendering/layer.dart.lib.js:5427:13)
at binding$5.WidgetsFlutterBinding.new.[_handlePointerEventImmediately] (http://localhost:51555/packages/flutter/src/gestures/binding.dart.lib.js:328:14)
at binding$5.WidgetsFlutterBinding.new.handlePointerEvent (http://localhost:51555/packages/flutter/src/gestures/binding.dart.lib.js:302:43)
at binding$5.WidgetsFlutterBinding.new.[_flushPointerEventQueue] (http://localhost:51555/packages/flutter/src/gestures/binding.dart.lib.js:292:14)
at binding$5.WidgetsFlutterBinding.new.[_handlePointerDataPacket] (http://localhost:51555/packages/flutter/src/gestures/binding.dart.lib.js:283:54)
at Object.invoke1 (http://localhost:51555/dart_sdk.js:190405:7)
at _engine.EnginePlatformDispatcher.__.invokeOnPointerDataPacket (http://localhost:51555/dart_sdk.js:171081:15)
at _engine.PointerBinding.__.[_onPointerData] (http://localhost:51555/dart_sdk.js:171963:49)
at http://localhost:51555/dart_sdk.js:172401:28
at http://localhost:51555/dart_sdk.js:172357:16
at loggedHandler (http://localhost:51555/dart_sdk.js:172062:11)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用最近的firebase版本,您必须使用 firebase cli 连接您的应用程序。
通过运行以下命令来安装CLI首先安装Firebase CLI
Firebase CLI CLI CLI:
npm install -G firebase -tools
接下来,通过运行以下命令安装FlutterFire CLI:
dart dart pub global gobl FlutterFire_Cli
安装后,FlutterFire命令将在全球上可用。
在应用程序的词根中,运行configure命令:
FlutterFire configure
配置命令将指导您完成许多过程:
完成后,您现在可以导入生成的文件并将其提供给InitializEapp方法:
lib/main.dart
//导入生成的文件
导入'firebase_options.dart';
然后,通过currentPlatform从defaultfirebaseoptions类提供当前平台选项:
With the recent firebase versions you have to use Firebase Cli to connect your app.
Installing the cli first install firebase cli
Firebase CLI via npm by running the following command:
npm install -g firebase-tools
Next, install the FlutterFire CLI by running the following command:
dart pub global activate flutterfire_cli
Once installed, the flutterfire command will be globally available.
In the root of your application, run the configure command:
flutterfire configure
The configuration command will guide you through a number of processes:
Once complete, you can now import the generated file and provide it to the initializeApp method:
lib/main.dart
// Import the generated file
import 'firebase_options.dart';
Then, provide the current platform options via the currentPlatform getter from the DefaultFirebaseOptions class: