当前位置: 首页 > 知识库问答 >
问题:

错误:[核心/无应用程序]未创建Firebase应用程序“[默认]”-请致电Firebase。initializeApp()

陈弘厚
2023-03-14

我正在构建一个flatter应用程序,并且我已经集成了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(),
    );
  }
}

登录屏幕。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_屏幕。飞奔



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)

共有1个答案

贺光华
2023-03-14

对于最新的firebase版本,您必须使用firebase Cli来连接应用程序。

安装cli首先安装Firebase cli

通过运行以下命令,通过npm访问Firebase CLI:

npm安装-g firebase tools

接下来,通过运行以下命令安装FlatterFire CLI:

dart pub global activate FlatterFire\u cli

一旦安装,扑火命令将在全球范围内可用。

在应用程序的根目录中,运行configure命令:

flatterfire配置

配置命令将引导您完成许多过程:

  • 选择Firebase项目(基于.firebaserc文件或从Firebase控制台)

完成后,现在可以导入生成的文件并将其提供给initializeApp方法:lib/main。dart//导入生成的文件“firebase\u”选项。省道'

然后,通过DefaultFirebaseOptions类中的currentPlatform getter提供当前平台选项:


void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
await Firebase.initializeApp(
  options: DefaultFirebaseOptions.currentPlatform,
);
..

 类似资料:
  • 我需要使用带有Firebase的电话身份验证,但每次我单击将代码发送给用户的按钮时都会出现此错误,这可能很容易解决,但由于我对Firebase和反应本机的了解不是很深,我找不到解决方案。 这是我的代码: 我想我需要在代码的某个地方插入这个“firebase.initializeApp()”。请问,我如何解决此问题?

  • 我有一个连接到两个应用程序的firebase数据库,一个是iOS应用程序,另一个是在Node.js中编码的web应用程序,这是一个将数据设置到数据库的基本算法。当我运行我遇到的算法时- 错误:未创建Firebase应用程序“[DEFAULT]”-调用Firebase App.InitializeApp()。错误(本机)在R处(/users/dd/desktop/code/node/node_mod

  • 我正在尝试将Firebase(Firestore)添加到我的Nuxt项目中,但是在初始化Firebase中的常量时,我收到了以下错误。firestore()在我的索引中。vue文件: 未捕获的Firebase错误:Firebase:未创建Firebase应用程序[DEFAULT]-调用Firebase App.initializeApp()(app/no-app)。 我已经在我的项目中安装了Fir

  • 我有点明白这个错误,但不管我怎么说,它都不起作用...我试图从FireBase中只返回一个字符串和一个数字值。我也做了所有的要求。android文件夹中的build.graddle和android/App文件夹中的build.gradle。一切。除此之外,我还做了:cloud_firestore: 对pubspec.yaml的依赖关系。现在我的main.dart看起来是这样的,但是它给出了太多的错

  • 我正在构建一个Flutter应用程序,我已经集成了Firebase,但是当我点击一个按钮注册、登录或注销时,我总是得到这个错误。我见过其他人问过同样的问题,但似乎没有一个对我有效。我正在使用Flutter和Android Studio。如何修复此问题? 这是我的代码的节选 下面是引发的异常

  • 我有一个使用firebase显示广告的应用程序。问题是一些设备(似乎只有Android 4.4设备)崩溃。以下是坠机报告: 我猜这个错误可能与我的一些依赖项的版本有关。 以下是顶级渐变中的依赖项: 还有我的应用程序gradle: 任何提示都会有帮助!提前谢谢!