Flutter项目Firebase初始化失败求助(已调用initializeApp)
Firebase Auth 初始化错误排查与解决

我正在搭建Firebase Auth,已经完成相关步骤,但作为新手,发现YouTube上的教程大多基于旧版本,而build.gradle的应用级和项目级配置已有变更。我认为已完成大部分配置,但仍出现Firebase应用初始化错误,想知道问题可能出在哪里,以及当前该如何解决?
main.dart 代码
import 'package:ecom/signup.dart'; import 'package:firebase_core/firebase_core.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'provider/cart_provider.dart'; import 'provider/favourite_provider.dart'; // void main() async { // WidgetsFlutterBinding.ensureInitialized(); // await Firebase.initializeApp(); // runApp(const MyApp()); // } void main() async { WidgetsFlutterBinding.ensureInitialized(); try { print("Firebase initialized successfully"); await Firebase.initializeApp(); } catch (e) { print("Failed to initialize Firebase: $e"); } runApp(MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) => MultiProvider( providers: [ ChangeNotifierProvider(create: (_) => CartProvider()), ChangeNotifierProvider(create: (_) => FavoriteProvider()), ], child: MaterialApp( title: 'Flutter Demo', debugShowCheckedModeBanner: false, theme: ThemeData( colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), useMaterial3: true, ), home: const sign_up(), ), ); }
Gradle 相关配置
项目级 build.gradle
allprojects { repositories { google() mavenCentral() } } rootProject.buildDir = "../build" subprojects { project.buildDir = "${rootProject.buildDir}/${project.name}" } subprojects { project.evaluationDependsOn(":app") } tasks.register("clean", Delete) { delete rootProject.buildDir }
应用级 build.gradle
plugins { id "com.android.application" id "kotlin-android" id "dev.flutter.flutter-gradle-plugin" id 'com.google.gms.google-services' } def localProperties = new Properties() def localPropertiesFile = rootProject.file("local.properties") if (localPropertiesFile.exists()) { localPropertiesFile.withReader("UTF-8") { reader -> localProperties.load(reader) } } def flutterVersionCode = localProperties.getProperty("flutter.versionCode") if (flutterVersionCode == null) { flutterVersionCode = "1" } def flutterVersionName = localProperties.getProperty("flutter.versionName") if (flutterVersionName == null) { flutterVersionName = "1.0" } android { namespace = "com.example.ecom" compileSdk = flutter.compileSdkVersion ndkVersion = flutter.ndkVersion compileOptions { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 } defaultConfig { applicationId = "com.example.ecom" minSdk = 23 targetSdk = flutter.targetSdkVersion versionCode = flutterVersionCode.toInteger() versionName = flutterVersionName } buildTypes { release { signingConfig = signingConfigs.debug } } } flutter { source = "../../" } dependencies{ implementation platform('com.google.firebase:firebase-bom:33.1.2') implementation 'com.google.firebase:firebase-analytics' } apply plugin: 'com.google.gms.google-services'
settings.gradle
pluginManagement { def flutterSdkPath = { def properties = new Properties() file("local.properties").withInputStream { properties.load(it) } def flutterSdkPath = properties.getProperty("flutter.sdk") assert flutterSdkPath != null, "flutter.sdk not set in local.properties" return flutterSdkPath }() includeBuild("$flutterSdkPath/packages/flutter_tools/gradle") repositories { google() mavenCentral() gradlePluginPortal() } } plugins { id "dev.flutter.flutter-plugin-loader" version "1.0.0" id "com.android.application" version "7.3.0" apply false id "org.jetbrains.kotlin.android" version "2.0.10" apply false id 'com.google.gms.google-services' version '4.4.2' apply false } include ":app"
排查与解决步骤
1. 检查 google-services.json 文件
- 确认
android/app目录下存在该文件,且是从Firebase控制台下载的对应项目文件。 - 核对文件内的
package_name与应用级build.gradle中的applicationId(com.example.ecom)完全一致。
2. 修正Firebase初始化逻辑
当前代码的成功提示在初始化前打印,逻辑错误,调整为:
void main() async { WidgetsFlutterBinding.ensureInitialized(); try { await Firebase.initializeApp(); print("Firebase initialized successfully"); } catch (e) { print("Failed to initialize Firebase: $e"); } runApp(const MyApp()); }
3. 添加Firebase Auth依赖
- 在应用级build.gradle的
dependencies块中添加Auth依赖:implementation 'com.google.firebase:firebase-auth' - 在项目的
pubspec.yaml中添加Flutter端的Auth依赖:dependencies: flutter: sdk: flutter firebase_core: ^2.24.2 firebase_auth: ^4.16.0 provider: ^6.1.1 - 执行
flutter pub get同步依赖。
4. 清理并重构项目
依次执行以下命令:
flutter clean flutter pub get
重启开发工具后重新运行项目。
5. 验证版本兼容性
检查firebase_core、firebase_auth版本与当前Flutter版本是否兼容,可参考pub.dev上的版本说明调整。
内容的提问来源于stack exchange,提问作者M Tashkeel Pasha
相关产品推荐
相关产品推荐

