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

Flutter项目中Firebase初始化失败,类型转换异常求助

Flutter Firebase初始化失败:类型转换异常问题

问题背景

我正在用Flutter和Firebase实现手机号认证功能,已完成以下配置:

  • 在pubspec.yaml中添加了相关依赖
  • 将google-services.json放入android/app目录
  • 修改了Android层级和App层级的build.gradle文件

但调用Firebase.initializeApp时无法完成初始化,抛出类型转换异常。

错误日志

Syncing files to device M2102J20SG...
I/flutter ( 8254): initializing flutter widgets
I/flutter ( 8254):  flutter widgets initialized
I/flutter ( 8254):  ignoring SSL certifications
I/flutter ( 8254):  SSL certifications ignored
I/flutter ( 8254): initializing firebase........
E/flutter ( 8254): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type '_Map<Object?, Object?>' is not a subtype of type 'List<Object?>' in type cast
E/flutter ( 8254): #0      PigeonInitializeResponse.decode (package:firebase_core_platform_interface/src/pigeon/messages.pigeon.dart:126:12)
E/flutter ( 8254): #1      _FirebaseCoreHostApiCodec.readValueOfType (package:firebase_core_platform_interface/src/pigeon/messages.pigeon.dart:158:41)
E/flutter ( 8254): #2      StandardMessageCodec.readValue (package:flutter/src/services/message_codecs.dart:479:12)
E/flutter ( 8254): #3      StandardMessageCodec.readValueOfType (package:flutter/src/services/message_codecs.dart:524:23)
E/flutter ( 8254): #4      _FirebaseCoreHostApiCodec.readValueOfType (package:firebase_core_platform_interface/src/pigeon/messages.pigeon.dart:160:22)
E/flutter ( 8254): #5      StandardMessageCodec.readValue (package:flutter/src/services/message_codecs.dart:479:12)
E/flutter ( 8254): #6      StandardMessageCodec.readValueOfType (package:flutter/src/services/message_codecs.dart:531:39)
E/flutter ( 8254): #7      _FirebaseCoreHostApiCodec.readValueOfType (package:firebase_core_platform_interface/src/pigeon/messages.pigeon.dart:160:22)
E/flutter ( 8254): #8      StandardMessageCodec.readValue (package:flutter/src/services/message_codecs.dart:479:12)
E/flutter ( 8254): #9      StandardMessageCodec.decodeMessage (package:flutter/src/services/message_codecs.dart:343:28)
E/flutter ( 8254): #10     BasicMessageChannel.send (package:flutter/src/services/platform_channel.dart:197:18)
E/flutter ( 8254): <asynchronous suspension>
E/flutter ( 8254): #11     FirebaseCoreHostApi.initializeCore (package:firebase_core_platform_interface/src/pigeon/messages.pigeon.dart:208:38)
E/flutter ( 8254): <asynchronous suspension>
E/flutter ( 8254): #12     MethodChannelFirebase._initializeCore (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:29:44)
E/flutter ( 8254): <asynchronous suspension>
E/flutter ( 8254): #13     MethodChannelFirebase.initializeApp (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:73:7)
E/flutter ( 8254): <asynchronous suspension>
E/flutter ( 8254): #14     Firebase.initializeApp (package:firebase_core/src/firebase.dart:43:31)
E/flutter ( 8254): <asynchronous suspension>
E/flutter ( 8254): #15     main (package:chef/main.dart:13:3)
E/flutter ( 8254): <asynchronous suspension>
E/flutter ( 8254): 
D/ProfileInstaller( 8254): Installing profile for com.feyst.chef
W/FinalizerDaemon( 8254): type=1400 audit(0.0:582376): avc: denied { getopt } for path="/dev/socket/usap_pool_primary" scontext=u:r:untrusted_app_30:s0:c151,c257,c512,c768 tcontext=u:r:zygote:s0 tclass=unix_stream_socket permissive=0 app=com.feyst.chef
D/AppScoutStateMachine( 8254): 8254-ScoutStateMachinecreated
D/SurfaceView( 8254): UPDATE null, mIsCastMode = false
E/libEGL  ( 8254): pre_cache appList: ,,

main.dart代码

void main() async {
  // print("initializing flutter widgets");
  // WidgetsFlutterBinding.ensureInitialized();
  // print(" flutter widgets initialized");
  await configureDependencies();
  print("initializing firebase........");
  await Firebase.initializeApp(name: '[FEYST]');
  //await Firebase.initializeApp(name:"[Chef Android App]");
  print('firease initialized .......');
  // Firebase.initializeApp(name:"[DEFAULT]");
  // print('firease initialized .......');
  runApp(locateService<App>());
  print("run app function is running");
}

App层级build.gradle文件

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'com.google.gms.google-services'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"


def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

android {
    compileSdkVersion 33

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.feyst.chef"
        minSdkVersion 21
        targetSdkVersion 31
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    lintOptions {
        disable 'InvalidPackage'
        disable "Instantiatable"
        checkReleaseBuilds false
        abortOnError false
    }
    signingConfigs {
        release {
//            keyAlias keystoreProperties['keyAlias']
//            keyPassword keystoreProperties['keyPassword']
//            storeFile file(keystoreProperties['storeFile'])
//            storePassword keystoreProperties['storePassword']
//
//            keyAlias System.getenv("APPCENTER_KEY_ALIAS")
//            keyPassword System.getenv("APPCENTER_KEY_PASSWORD")
//            storeFile rootProject.file("connectapp.jks")
//            storePassword System.getenv("APPCENTER_KEYSTORE_PASSWORD")

            v1SigningEnabled true
            v2SigningEnabled true

        }
        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
                shrinkResources true
                minifyEnabled true
                signingConfig signingConfigs.debug
            }
            debug {
                applicationIdSuffix ""
                debuggable true
            }
        }
    }
    dependencies {
        // Import the BoM for the Firebase platform
        implementation platform('com.google.firebase:firebase-bom:32.1.0')
        implementation 'androidx.browser:browser:1.3.0'
        // Add the dependency for the Firebase Authentication library
        // When using the BoM, you don't specify versions in Firebase library dependencies
        implementation 'com.google.firebase:firebase-auth'
    }
}

flutter {
    source '../..'
}

Android层级build.gradle文件

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

    dependencies {
        classpath 'com.android.tools.build:gradle:7.1.2'
        classpath 'com.google.gms:google-services:4.3.15'
        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')
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

内容的提问来源于stack exchange,提问作者farhan khan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 04:02:03