You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter集成Firebase认证时触发PlatformException异常求助

Flutter Firebase认证PlatformException及注册页面错误排查

我正在用Flutter结合Firebase实现认证功能,已通过Firebase CLI完成配置,但运行应用时出现PlatformException异常,进入注册页面后还会触发额外错误。相关代码如下:

main.dart

void main()  {
  WidgetsFlutterBinding.ensureInitialized(); // Ensure that Flutter is initialized before calling Firebase.initializeApp()
   Firebase.initializeApp();

  runApp(FlashChat());
}

registration.dart

@override
  _RegistrationScreenState createState() => _RegistrationScreenState();
}

class _RegistrationScreenState extends State<RegistrationScreen> {
  late String email;
  late String password;
  final auth =FirebaseAuth.instance;

  @override
  Widget build(BuildContext context) {
}

android/build.gradle

buildscript {
    ext.kotlin_version = '1.7.10'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.3.0'
        // START: FlutterFire Configuration
        classpath 'com.google.gms:google-services:4.3.10'
        // END: FlutterFire Configuration
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

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
}

app/build.gradle

plugins {
    id "com.android.application"
    id "kotlin-android"
    id "dev.flutter.flutter-gradle-plugin"
}

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.flutter_chat"
    compileSdkVersion flutter.compileSdkVersion
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.example.flutter_chat"
        // You can update the following values to match your application needs.
        // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
        minSdkVersion flutter.minSdkVersion
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {}

问题分析与解决步骤

  1. Firebase初始化未等待完成
    Firebase.initializeApp()是异步操作,直接调用后立即启动应用会导致Firebase服务未就绪,引发PlatformException。修改main.dart:

    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await Firebase.initializeApp();
      runApp(FlashChat());
    }
    
  2. 缺少Google Services插件应用
    app/build.gradle中未应用Google Services插件,导致Firebase配置无法正确加载。在plugins块中添加:

    plugins {
        id "com.android.application"
        id "kotlin-android"
        id "dev.flutter.flutter-gradle-plugin"
        id "com.google.gms.google-services" // 添加这一行
    }
    
  3. 注册页面代码不完整
    registration.dart缺少核心注册逻辑及异常处理,补全注册方法:

    void registerUser() async {
      try {
        final userCredential = await auth.createUserWithEmailAndPassword(
          email: email,
          password: password,
        );
        // 注册成功后的逻辑,如导航到主页
      } on PlatformException catch (e) {
        // 捕获并处理异常,例如提示用户错误信息
        print(e.message);
      }
    }
    
  4. 额外检查项

    • 确认Firebase控制台已启用邮箱/密码认证方式;
    • 检查android/app/google-services.json文件存在且配置正确(Firebase CLI配置后自动生成);
    • 确保FlutterFire依赖版本与Firebase CLI版本兼容。

内容的提问来源于stack exchange,提问作者Atif Hussain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 21:13:23