Flutter cloud_firestore(0.9.0) 包在打开应用时崩溃

Posted

技术标签:

【中文标题】Flutter cloud_firestore(0.9.0) 包在打开应用时崩溃【英文标题】:Flutter cloud_firestore(0.9.0) package crashes when opening the app 【发布时间】:2019-06-28 14:17:43 【问题描述】:

Flutter firebase firestore 包在打开应用时崩溃,这里是日志文件,

E/MethodChannel#plugins.flutter.io/cloud_firestore(28101): Failed to handlemethod call
E/MethodChannel#plugins.flutter.io/cloud_firestore(28101): 
java.lang.NullPointerException: Firestore component is not present.
E/MethodChannel#plugins.flutter.io/cloud_firestore(28101):  at 
com.google.common.base.Preconditions.checkNotNull(Preconditions.java:787)
E/MethodChannel#plugins.flutter.io/cloud_firestore(28101):  at com.google.firebase.firestore.FirebaseFirestore.zza(com.google.firebase:firebase-firestore@@17.1.1:86)
E/MethodChannel#plugins.flutter.io/cloud_firestore(28101):  at com.google.firebase.firestore.FirebaseFirestore.getInstance(com.google.firebase:firebase-firestore@@17.1.1:78)
E/MethodChannel#plugins.flutter.io/cloud_firestore(28101):  at io.flutter.plugins.firebase.cloudfirestore.CloudFirestorePlugin.getFirestore(CloudFirestorePlugin.java:82)
E/MethodChannel#plugins.flutter.io/cloud_firestore(28101):  at io.flutter.plugins.firebase.cloudfirestore.CloudFirestorePlugin.getCollectionReference(CloudFirestorePlugin.java:87)
E/MethodChannel#plugins.flutter.io/cloud_firestore(28101):  at io.flutter.plugins.firebase.cloudfirestore.CloudFirestorePlugin.getQuery(CloudFirestorePlugin.java:139)
E/MethodChannel#plugins.flutter.io/cloud_firestore(28101):  at io.flutter.plugins.firebase.cloudfirestore.CloudFirestorePlugin.onMethodCall(CloudFirestorePlugin.java:463)
E/MethodChannel#plugins.flutter.io/cloud_firestore(28101):  at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:200)
E/MethodChannel#plugins.flutter.io/cloud_firestore(28101):  at io.flutter.view.FlutterNativeView$PlatformMessageHandlerImpl.handlePlatformMessage(FlutterNativeView.java:188)
E/MethodChannel#plugins.flutter.io/cloud_firestore(28101):  at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:202)
E/MethodChannel#plugins.flutter.io/cloud_firestore(28101):  at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#plugins.flutter.io/cloud_firestore(28101):  at android.os.MessageQueue.next(MessageQueue.java:326)
E/MethodChannel#plugins.flutter.io/cloud_firestore(28101):  at android.os.Looper.loop(Looper.java:160)E/MethodChannel#plugins.flutter.io/cloud_firestore(28101):    at android.app.ActivityThread.main(ActivityThread.java:6718) 
E/MethodChannel#plugins.flutter.io/cloud_firestore(28101):  at java.lang.reflect.Method.invoke(Native Method 
E/MethodChannel#plugins.flutter.io/cloud_firestore(28101):  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)E/MethodChannel#plugins.flutter.io/cloud_firestore(28101):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

这里还有一些日志,

E/flutter (28101): [ERROR:flutter/shell/common/shell.cc(186)] Dart Error: Unhandled exception:
E/flutter (28101): PlatformException(error, Firestore component is not present., null)
E/flutter (28101): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:564:7)
E/flutter (28101): #1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:292:18)
E/flutter (28101): <asynchronous suspension>
E/flutter (28101): #2      Query.snapshots.<anonymous closure> (file:///Users/chathura/Flutter/flutter_sdk/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-0.8.2+3/lib/src/query.dart:56:37)
E/flutter (28101): #3      _runGuarded (dart:async/stream_controller.dart:805:24)
E/flutter (28101): #4      _BroadcastStreamController._subscribe (dart:async/broadcast_stream_controller.dart:213:7)
E/flutter (28101): #5      _ControllerStream._createSubscription (dart:async/stream_controller.dart:818:19)
E/flutter (28101): #6      _StreamImpl.listen (dart:async/stream_impl.dart:472:9)
E/flutter (28101): #7      _StreamBuilderBaseState._subscribe (package:flutter/src/widgets/async.dart:135:37)
E/flutter (28101): #8      _StreamBuilderBaseState.initState (package:flutter/src/widgets/async.dart:109:5)
E/flutter (28101): #9      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3830:58)
E/flutter (28101): #10     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
E/flutter (28101): #11     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
E/flutter (28101): #12     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
E/flutter (28101): #13     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
E/flutter (28101): #14     Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
E/flutter (28101): #15     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
E/flutter (28101): #16     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
E/flutter (28101): #17     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
E/flutter (28101): #18     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
E/flutter (28101): #19     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
E/flutter (28101): #20     Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
E/flutter (28101): #21     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
E/flutter (28101): #22     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
E/flutter (28101): #23     ParentDataElement.mount (package:flutter/src/widgets/framework.dart:4047:11)
E/flutter (28101): #24     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
E/flutter (28101): #25     MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4965:32)
E/flutter (28101): #26     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
E/flutter (28101): #27     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
E/flutter (28101): #28     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
E/flutter (28101): #29     Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
E/flutter (28101): #30     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
E/flutter (28101): #31     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3848:11)
E/flutter (28101): #32     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
E/flutter (28101): #33     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
E/flutter (28101): #34     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
E/flutter (28101): #35     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
E/flutter (28101): #36     Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
E/flutter (28101): #37     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
E/flutter (28101): #38     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
E/flutter (28101): #39     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
E/flutter (28101): #40     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
E/flutter (28101): #41     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
E/flutter (28101): #42     Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
E/flutter (28101): #43     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
E/flutter (28101): #44     StatefulElement._firstBuild (package:flutter/src/wi

这是我的应用级 gradle 文件,

buildscript 
    repositories 
        google()
        jcenter()


dependencies 
    classpath 'com.android.tools.build:gradle:3.3.0'
    classpath 'com.google.gms:google-services:4.2.0'


allprojects 
  repositories 
     google()
     jcenter()

这是模块级的gradle文件,

dependencies 
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
    implementation 'com.google.firebase:firebase-core:16.0.7'

firestore 包的版本,

cloud_firestore: ^0.9.0

是gardle问题还是插件问题?

欢迎提出任何建议。

【问题讨论】:

***.com/a/54460559/10269042 @anmol.majhail 我试过了,这个不行 您的 Complie SDK 版本是多少? 改成 - minSdkVersion 21 & targetSdkVersion 28 然后试试。 确保在构建之前先运行 flutter clean 【参考方案1】:

几件事:

将这些行粘贴到gradle.properties 文件的末尾。

android.useAndroidX=true
android.enableJetifier=true

文件app\build.gradle的更改

android 
    compileSdkVersion 28

...

defaultConfig 
    minSdkVersion 21
    targetSdkVersion 28
    multiDexEnabled true
    ...

转到终端并启动命令

flutter clean

然后再次构建。

正如CHANGELOG of cloud_firestore 0.9.0 中提到的:

0.9.0 # 重大变化。从已弃用的原始 Android 支持库迁移到 AndroidX。这不应该导致任何功能 更改,但它要求任何使用此插件的 Android 应用程序也 如果他们使用原始支持库,请迁移。

【讨论】:

是什么让你说将 min sdk 改为 21 并将 target 改为 28? 我在 Flutter github 问题的某个地方读到了它。加上它的最新版本——flutter 进一步的更新也会添加它。稳定频道尚未更新。

以上是关于Flutter cloud_firestore(0.9.0) 包在打开应用时崩溃的主要内容,如果未能解决你的问题,请参考以下文章

添加依赖项 cloud_firestore 时,新的 Flutter 应用程序失败:^1.0.7

Flutter cloud_firestore 系统日期对象的设置/警告

如何修复颤动的 cloud_firestore 依赖项?

Flutter - 任务':cloud_firestore:compileDebugJavaWithJavac'的执行失败

Flutter:[cloud_firestore/unknown] NoSuchMethodError:null 上的无效成员:'includeMetadataChanges'(Flutter Web

Flutter (iOS) - 在 GeneratedPluginRegistrant.m 中找不到模块“cloud_firestore”