Flutter 无法连接到 Firebase Firestore 数据库

Posted

技术标签:

【中文标题】Flutter 无法连接到 Firebase Firestore 数据库【英文标题】:Flutter Cant Connect to firebase firestore database 【发布时间】:2020-12-30 22:13:27 【问题描述】:
Launching lib\main.dart on android SDK built for x86 in debug mode...
Running Gradle task 'assembleDebug'...
Parameter format not correct -
✓ Built build\app\outputs\flutter-apk\app-debug.apk.
Installing build\app\outputs\flutter-apk\app.apk...
Waiting for Android SDK built for x86 to report its views...
Debug service listening on ws://127.0.0.1:50523/i2wsZclk56g=/ws
Syncing files to device Android SDK built for x86...
D/EGL_emulation(18666): eglMakeCurrent: 0xe7f04400: ver 2 0 (tinfo 0xdfef9730)
D/eglCodecCommon(18666): setVertexArrayObject: set vao to 0 (0) 1 0
W/DynamiteModule(18666): Local module descriptor class for providerinstaller not found.
I/DynamiteModule(18666): Considering local module providerinstaller:0 and remote module providerinstaller:0
W/ProviderInstaller(18666): Failed to load providerinstaller module: No acceptable module found. Local version is 0 and remote

版本为 0。 W/xample.news_ap(18666): 访问隐藏方法 Lsun/misc/Unsafe;->getLong(Ljava/lang/Object;J)J (灰名单、核心平台 API、链接、允许) W/xample.news_ap(18666): 访问隐藏方法 Lsun/misc/Unsafe;->arrayBaseOffset(Ljava/lang/Class;)I (灰名单、核心平台 API、链接、允许) W/xample.news_ap(18666):访问隐藏方法 Lsun/misc/Unsafe;->copyMemory(JJJ)V(灰名单、链接、允许) W/xample.news_ap(18666): 访问隐藏方法 Lsun/misc/Unsafe;->objectFieldOffset(Ljava/lang/reflect/Field;)J (灰名单、核心平台 API、链接、允许) W/xample.news_ap(18666): 访问隐藏方法 Lsun/misc/Unsafe;->getByte(J)B (greylist,core-platform-api, linking, 允许) W/xample.news_ap(18666): 访问隐藏方法 Lsun/misc/Unsafe;->getByte(Ljava/lang/Object;J)B (灰名单、核心平台 API、链接、允许) W/xample.news_ap(18666): 访问隐藏方法 Lsun/misc/Unsafe;->getLong(J)J (greylist,core-platform-api, linking, 允许) W/xample.news_ap(18666): 访问隐藏方法 Lsun/misc/Unsafe;->putByte(JB)V (greylist,core-platform-api, linking, 允许) W/xample.news_ap(18666): 访问隐藏方法 Lsun/misc/Unsafe;->putByte(Ljava/lang/Object;JB)V (灰名单、核心平台 API、链接、允许) W/xample.news_ap(18666): 访问隐藏方法 Lsun/misc/Unsafe;->getLong(Ljava/lang/Object;J)J (灰名单、核心平台 API、反射、允许) W/xample.news_ap(18666): 访问隐藏方法 Lsun/misc/Unsafe;->getLong(Ljava/lang/Object;J)J (灰名单、核心平台 API、反射、允许) W/xample.news_ap(18666):访问隐藏字段 Ljava/nio/Buffer;->address:J(灰名单,反射,允许) I/xample.news_ap(18666):ClassLoaderContext 是一个特殊的共享库。 I/chatty (18666): uid=10143(com.example.news_app) AsyncTask #1 相同的 1 行 I/xample.news_ap(18666):ClassLoaderContext 是一个特殊的共享库。 V/NativeCrypto(18666):注册com/google/android/gms/org/conscrypt/NativeCrypto的286原生 方法... W/xample.news_ap(18666): 访问隐藏方法 Ljava/security/spec/ECParameterSpec;->getCurveName()Ljava/lang/String; (灰名单,反射,允许) D/NetworkSecurityConfig(18666):未指定网络安全配置,使用平台默认值 I/ProviderInstaller(18666): 已安装默认安全提供程序 GmsCore_OpenSSL

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following NoSuchMethodError was thrown building Home(dirty, state: _HomeState#f839b):
The getter 'length' was called on null.
Receiver: null
Tried calling: length

The relevant error-causing widget was:
  Home file:///E:/major%20project/newssapp/news_app/lib/main.dart:17:15

When the exception was thrown, this was the stack:
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1      _HomeState.build (package:news_app/home.dart:151:44)
#2      StatefulElement.build (package:flutter/src/widgets/framework.dart:4663:28)
#3      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4546:15)
#4      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4719:11)
#5      Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5)
#6      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4525:5)
#7      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4710:11)
#8      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4520:5)
...     Normal element mounting (132 frames)
#140    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3490:14)
#141    MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5991:32)
...     Normal element mounting (267 frames)
#408    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3490:14)
#409    Element.updateChild (package:flutter/src/widgets/framework.dart:3258:18)
#410    RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1174:16)
#411    RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1145:5)
#412    RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:1087:17)
#413    BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2620:19)
#414    RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1086:13)
#415    WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:927:7)
#416    WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:908:7)
(elided 11 frames from class _RawReceivePortImpl, class _Timer, dart:async, and dart:async-patch)

════════════════════════════════════════════════════════════════════════════════════════════════════
W/xample.news_ap(18666): Accessing hidden field Ljava/net/Socket;->impl:Ljava/net/SocketImpl; (greylist, reflection,

允许) W/xample.news_ap(18666): 访问隐藏方法 Ldalvik/system/CloseGuard;->get()Ldalvik/system/CloseGuard; (灰名单、核心平台 API、链接、允许) W/xample.news_ap(18666): 访问隐藏方法 Ldalvik/system/CloseGuard;->open(Ljava/lang/String;)V (灰名单、核心平台 API、链接、允许) W/xample.news_ap(18666): 访问隐藏字段 Ljava/io/FileDescriptor;->descriptor:I (greylist, JNI, allowed) W/xample.news_ap(18666): 访问隐藏方法 Ljava/security/spec/ECParameterSpec;->setCurveName(Ljava/lang/String;)V (灰名单,反射,允许) W/xample.news_ap(18666): 访问隐藏方法 Ldalvik/system/BlockGuard;->getThreadPolicy()Ldalvik/system/BlockGuard$Policy; (灰名单、核心平台 API、链接、允许) W/xample.news_ap(18666): 访问隐藏方法 Ldalvik/system/BlockGuard$Policy;->onNetwork()V (greylist, linking, 允许) W/Firestore(18666):(21.4.3) [OnlineStateTracker]:无法访问 Cloud Firestore 后端。连接失败 1 次。最近的错误: Statuscode=NOT_FOUND, description=项目 newsapp-aa434 没有 存在或不包含活动的 Cloud Datastore 或 Cloud Firestore 数据库。请访问http://console.cloud.google.com 创建一个项目或 https://console.cloud.google.com/datastore/setup?project=newsapp-aa434 添加 Cloud Datastore 或 Cloud Firestore 数据库。请注意,云 Datastore 或 Cloud Firestore 始终具有关联的 App Engine 应用 并且这个应用程序不能被禁用。,cause=null W/Firestore(18666):这通常表示您的设备目前没有健康的 Internet 连接。客户将 在离线模式下运行,直到它能够成功连接到 后端。

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following NoSuchMethodError was thrown building Home(dirty, state: _HomeState#f839b):
The getter 'length' was called on null.
Receiver: null
Tried calling: length

The relevant error-causing widget was: 
  Home file:///E:/major%20project/newssapp/news_app/lib/main.dart:17:15
When the exception was thrown, this was the stack: 
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1      _HomeState.build (package:news_app/home.dart:151:44)
#2      StatefulElement.bu``ild (package:flutter/src/widgets/framework.dart:4663:28)
#3      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4546:15)
#4      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4719:11)
...
════════════════════════════════════════════════════════════════════════════════════════════════════

【问题讨论】:

您似乎刚刚发布了您的错误。请参阅How do I ask a good question?。在错误周围使用代码块会使其更具可读性。 【参考方案1】:

如果您使用的是最新的 Firebase 依赖项,则必须将 Firebase Core 添加到 pubspec.yaml 中的依赖项中;

firebase_core: "0.5.0"

集成核心依赖后,您必须在应用中初始化 Firebase 以访问其功能,例如 Firestore;

void main() 
     runApp(MyApp());


class MyApp extends StatelessWidget 
  final Future<FirebaseApp> _initialization = Firebase.initializeApp();

  @override
  Widget build(BuildContext context) 
    return FutureBuilder(
      // Initialize Firebase:
      future: _initialization,
      builder: (context, snapshot) 
        if (snapshot.connectionState == ConnectionState.done) 
          return YourApplication();
        
      ,
    );
  

在Flutter中安装和使用Firebase工具的必要文档可以在FlutterFire page找到。

或者,如果您确定您的依赖关系,您可以在终端中运行以下命令;

flutter pub get

【讨论】:

以上是关于Flutter 无法连接到 Firebase Firestore 数据库的主要内容,如果未能解决你的问题,请参考以下文章

将 Flutter App 的 iOS 部分连接到 Firebase 的困惑

从 Flutter 应用程序连接到本地 Firebase 函数模拟器时出错

firebase 项目所有者更改后未反映在 Flutter Web 部署上的更改

无法从 NodeMCU 板连接到 Wi-Fi 网络

Wi-fi无法连接到Android Studio模拟器

最近的 Android Studio 无法连接到 Firebase?