未处理的异常:PlatformException(null-error,主机平台为非空返回值返回空值

Fre*_*lfe 3 android firebase android-gradle-plugin flutter

我看到了很多关于以下错误的问题:\n"[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(null-error, Host platform returned null value for non-null return value ., null, null)”但所有解决方案都不能解决我的问题\n我已按照同上的所有说明进行操作,添加依赖项等等。我的 build.gradles 没问题,但当我运行时,我仍然收到错误。一个月前这对我有用,但现在我不知道这个月发生了什么。

\n
Launching lib\\main.dart on AOSP on IA Emulator in debug mode...\nRunning Gradle task 'assembleDebug'...\nParameter format not correct -\n\xe2\x88\x9a  Built build\\app\\outputs\\flutter-apk\\app-debug.apk.\nInstalling build\\app\\outputs\\flutter-apk\\app-debug.apk...\nDebug service listening on ws://127.0.0.1:50498/Z0uovONnF5A=/ws\nSyncing files to device AOSP on IA Emulator...\nE/flutter ( 8435): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(null-error, Host platform returned null value for non-null return value., null, null)\nE/flutter ( 8435): #0      FirebaseCoreHostApi.optionsFromResource (package:firebase_core_platform_interface/src/pigeon/messages.pigeon.dart:248:7)\nE/flutter ( 8435): <asynchronous suspension>\nE/flutter ( 8435): #1      MethodChannelFirebase.initializeApp (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:89:25)\nE/flutter ( 8435): <asynchronous suspension>\nE/flutter ( 8435): #2      Firebase.initializeApp (package:firebase_core/src/firebase.dart:43:31)\nE/flutter ( 8435): <asynchronous suspension>\nE/flutter ( 8435): #3      main (package:kp_app/main.dart:8:3)\nE/flutter ( 8435): <asynchronous suspension>\nE/flutter ( 8435): \n
Run Code Online (Sandbox Code Playgroud)\n

这是我的 main.dart,你可以看到我已按照说明进行操作,我上个月这样做了,没问题,这个月我再试一次,问题

\n
import 'package:easy_localization/easy_localization.dart';\nimport 'package:firebase_core/firebase_core.dart';\nimport 'package:flutter/material.dart';\nimport 'package:flutter/services.dart';\n\nvoid main() async {\n  WidgetsFlutterBinding.ensureInitialized();\n  await Firebase.initializeApp();\n  await EasyLocalization.ensureInitialized();\n  SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(\n      statusBarColor: Colors.transparent,\n      statusBarIconBrightness: Brightness.dark));\n\n\n  runApp(EasyLocalization(\n    supportedLocales: const [\n      Locale('en'),\n      Locale('ar'),\n      Locale('es'),\n      Locale('fr'),\n      Locale('de'),\n      Locale('it'),\n      Locale('pt'),\n      Locale('ru'),\n      Locale('zh'),\n      Locale('ja'),\n      Locale('hi'),\n      Locale('sw')\n    ],\n    path: 'assets/translations',\n    fallbackLocale: const Locale('en'),\n\n    //Default language\n    startLocale: const Locale('en'),\n    useOnlyLangCode: true,\n    child: const NewApp(),\n  ));\n}\n\nclass NewApp extends StatelessWidget {\n  const NewApp({super.key});\n\n  @override\n  Widget build(BuildContext context) {\n    return Container(\n      color: Colors.blue,\n      child: const Center(child: Text('My New APp')),\n    );\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

这是我的应用程序级别 build.gradle,如您所见,它完好无损

\n
//plugins {\n//    id "com.android.application"\n//    id "kotlin-android"\n//    id "dev.flutter.flutter-gradle-plugin"\n//    id 'com.google.gms.google-services'\n//}\n\ndef localProperties = new Properties()\ndef localPropertiesFile = rootProject.file('local.properties')\nif (localPropertiesFile.exists()) {\n    localPropertiesFile.withReader('UTF-8') { reader ->\n        localProperties.load(reader)\n    }\n}\n\ndef flutterRoot = localProperties.getProperty('flutter.sdk')\nif (flutterRoot == null) {\n    throw  GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")\n}\n\ndef flutterVersionCode = localProperties.getProperty('flutter.versionCode')\nif (flutterVersionCode == null) {\n    flutterVersionCode = '1'\n}\n\ndef flutterVersionName = localProperties.getProperty('flutter.versionName')\nif (flutterVersionName == null) {\n    flutterVersionName = '1.0'\n}\n\napply plugin: 'com.android.application'\napply plugin: 'kotlin-android'\napply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"\napply plugin: 'com.google.gms.google-services'\n\n//apply plugin: 'dev.flutter.flutter-gradle-plugin'\n\nandroid {\n    namespace "com.prowebsolutions.kp_app"\n    compileSdkVersion 33\n    ndkVersion flutter.ndkVersion\n\n    compileOptions {\n        sourceCompatibility JavaVersion.VERSION_1_8\n        targetCompatibility JavaVersion.VERSION_1_8\n    }\n\n    kotlinOptions {\n        jvmTarget = '1.8'\n    }\n\n    sourceSets {\n        main.java.srcDirs += 'src/main/kotlin'\n    }\n\n    defaultConfig {\n        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).\n        applicationId "com.prowebsolutions.kp_app"\n        // You can update the following values to match your application needs.\n        // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.\n        minSdkVersion 28\n        //noinspection OldTargetApi\n        targetSdkVersion 31\n//        targetSdkVersion flutter.targetSdkVersion\n        multiDexEnabled true\n        versionCode flutterVersionCode.toInteger()\n        versionName flutterVersionName\n    }\n\n    buildTypes {\n        release {\n            // TODO: Add your own signing config for the release build.\n            // Signing with the debug keys for now, so `flutter run --release` works.\n            signingConfig signingConfigs.debug\n        }\n    }\n}\n\nflutter {\n    source '../..'\n}\n\ndependencies {\n    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"\n    implementation platform('com.google.firebase:firebase-bom:32.3.1')\n    implementation 'com.google.firebase:firebase-analytics'\n}\n
Run Code Online (Sandbox Code Playgroud)\n

这也是我的项目级别build.gradle完好无损

\n
buildscript {\n    ext.kotlin_version = '1.7.10'\n    repositories {\n        google()\n        mavenCentral()\n    }\n\n    dependencies {\n        classpath 'com.android.tools.build:gradle:7.3.1'\n        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"\n        classpath 'com.google.gms:google-services:4.4.0'\n    }\n}\n\nallprojects {\n    repositories {\n        google()\n        mavenCentral()\n    }\n}\n\nrootProject.buildDir = '../build'\nsubprojects {\n    project.buildDir = "${rootProject.buildDir}/${project.name}"\n}\nsubprojects {\n    project.evaluationDependsOn(':app')\n}\n\ntasks.register("clean", Delete) {\n    delete rootProject.buildDir\n}\n
Run Code Online (Sandbox Code Playgroud)\n

Ale*_*lex 6

您好,Google 服务版本尝试降级到 4.3.15 其他解决方案是迁移到 buildscript 到插件块,检查下一个:

文件 -> android/settings.gradle

pluginManagement {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
    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
    }
    settings.ext.flutterSdkPath = flutterSdkPath()

    includeBuild("${settings.ext.flutterSdkPath}/packages/flutter_tools/gradle")

    plugins {
        id "dev.flutter.flutter-gradle-plugin" version "1.0.0" apply false
    }
}

include ":app"

apply from: "${settings.ext.flutterSdkPath}/packages/flutter_tools/gradle/app_plugin_loader.gradle"
Run Code Online (Sandbox Code Playgroud)

文件 -> android/build.gradle

plugins {
    id 'com.android.application' version '7.3.0' apply false
    id 'org.jetbrains.kotlin.android' version '1.7.10' apply false
    id 'com.google.gms.google-services' version '4.3.15' apply false
    id 'com.google.firebase.crashlytics' version '2.9.9' apply false
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.buildDir = '../build'

subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
    project.evaluationDependsOn(':app')
}

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}
Run Code Online (Sandbox Code Playgroud)

文件 -> android/app/build.gradle

plugins {
    id "com.android.application"
    id "kotlin-android"
    id "dev.flutter.flutter-gradle-plugin"
    id 'com.google.gms.google-services'    
    id 'com.google.firebase.crashlytics'
}

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.test_app"
    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.test_app"
        // 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 localProperties.getProperty('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 {
    implementation platform('com.google.firebase:firebase-bom:32.3.1')
    implementation 'com.google.firebase:firebase-crashlytics'
    implementation 'com.google.firebase:firebase-analytics'
}
Run Code Online (Sandbox Code Playgroud)

文件 -> android/local.properties

#Add your Android SDK path    
sdk.dir=D:\\Users\\Alex\\Sdk
#Add your Flutter path SDK  
flutter.sdk=D:\\Users\\Alex\\Flutter 
flutter.minSdkVersion=24
flutter.buildMode=debug
flutter.versionName=1.0.0
flutter.versionCode=1
Run Code Online (Sandbox Code Playgroud)

最后执行flutter cleanflutter pub get --no-example

并检查google-services.json文件

在此输入图像描述