使用 firebase 和 flutter 执行身份验证时出错。 [错误:flutter/runtime/dart_vm_initializer.cc(41)] 异常:PlatformException

问题描述 投票:0回答:1

我正在使用 firebase 和 flutter 来执行身份验证。我已经使用 firebase cli 配置了 firebase。

现在,当我运行应用程序时,我收到异常

此外,当我导航到注册页面时,它会出现以下错误:

以下是代码

main.dart

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

  runApp(FlashChat());
}

注册.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
}

**
应用程序/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 {}
flutter firebase authentication exception configuration
1个回答
0
投票

Firebase.initializeApp
的返回类型是Future 所以如果你想等待Future类型对象完成,你必须在
await
前面设置
Firebase.initializeApp
关键字。

如果要在 Flutter 中使用

await
关键字,必须在 main 函数前面设置
async
关键字

你的代码的问题是flutter通过runApp()构造widget树,之前没有解析Future对象,Future。所以程序当时找不到 FirebaseApp 实例。

所以你应该将你的代码修改为此。

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  runApp(const MyApp());
}
© www.soinside.com 2019 - 2024. All rights reserved.