簡體   English   中英

Flutter Web Firestore 錯誤:預期類型為“((對象?)=>對象)?”,但得到類型“(對象)=> Z497031794414A552435F90151AC3?”之一

[英]Flutter Web Firestore error: Expected a value of type '((Object?) => Object)?', but got one of type '(Object) => Object?'

我簡化了重現此錯誤的代碼。 它適用於 Android 模擬器,但在 Web 上運行時從 Firestore 獲取收集數據時失敗。

錯誤信息:

Error: [cloud_firestore/unknown] Expected a value of type '((Object?) => Object)?', but got one of type '(Object) => Object?'
    at Object.throw_ [as throw] (http://localhost:58066/dart_sdk.js:5028:11)
    at collection_reference_web.CollectionReferenceWeb.new.get (http://localhost:58066/packages/cloud_firestore_web/src/write_batch_web.dart.lib.js:370:23)
    at get.next (<anonymous>)
    at http://localhost:58066/dart_sdk.js:37206:33
    at _RootZone.runUnary (http://localhost:58066/dart_sdk.js:37077:59)
    at _FutureListener.thenAwait.handleValue (http://localhost:58066/dart_sdk.js:32333:29)
    at handleValueCallback (http://localhost:58066/dart_sdk.js:32860:49)
    at Function._propagateToListeners (http://localhost:58066/dart_sdk.js:32898:17)
    at _Future.new.[_completeWithValue] (http://localhost:58066/dart_sdk.js:32746:23)
    at async._AsyncCallbackEntry.new.callback (http://localhost:58066/dart_sdk.js:32767:35)
    at Object._microtaskLoop (http://localhost:58066/dart_sdk.js:37329:13)
    at _startMicrotaskLoop (http://localhost:58066/dart_sdk.js:37335:13)
    at http://localhost:58066/dart_sdk.js:33106:9


Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel beta, 1.26.0-17.6.pre, on Microsoft Windows [Version 10.0.18363.1379], locale ru-RU)
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.0)
[√] Chrome - develop for the web
[√] Android Studio (version 4.1.0)
[√] IntelliJ IDEA Ultimate Edition (version 2020.1)
[√] VS Code (version 1.53.2)
[√] Connected device (2 available)

main.dart:(MyApp() 只是一個空頁面)

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_auth/firebase_auth.dart';

List<String> departments = List.empty(growable: true);

void main() async{
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  await FirebaseAuth.instance.signInWithEmailAndPassword(email: "user1@gmail.com", password: "111111");
  CollectionReference departmentsRef = FirebaseFirestore.instance.collection("Departments");
  QuerySnapshot qsDepartments = await departmentsRef.get(); //HERE I GET AN ERROR
  qsDepartments.docs.forEach((element) {
    Map<String, dynamic>? map = element.data();
    print(map!['name']);
    departments.add(map['name']);
  });
  runApp(MyApp());
}

網頁/索引.html:

<!DOCTYPE html>
<html>
<head>
  <base href="/">

  <meta charset="UTF-8">
  <meta content="IE=Edge" http-equiv="X-UA-Compatible">
  <meta name="description" content="A new Flutter application.">

  <!-- iOS meta tags & icons -->
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="apple-mobile-web-app-title" content="test_web_firestore">
  <link rel="apple-touch-icon" href="icons/Icon-192.png">

  <!-- Favicon -->
  <link rel="icon" type="image/png" href="favicon.png"/>

  <title>test_web_firestore</title>
  <link rel="manifest" href="manifest.json">
</head>
<body>
  <script>
    if ('serviceWorker' in navigator) {
      window.addEventListener('flutter-first-frame', function () {
        navigator.serviceWorker.register('flutter_service_worker.js');
      });
    }
  </script>
  <script src="main.dart.js" type="application/javascript"></script>
  <!-- The core Firebase JS SDK is always required and must be listed first -->
  <script src="https://www.gstatic.com/firebasejs/8.2.9/firebase-app.js"></script>
  <script src="https://www.gstatic.com/firebasejs/8.2.9/firebase-auth.js"></script>
  <script src="https://www.gstatic.com/firebasejs/8.2.9/firebase-firestore.js"></script>

  <script>
  // Your web app's Firebase configuration
  var firebaseConfig = {
    apiKey: "AIzaSyA6BARqtxEwWAvYG9etXXz9iknq6PviJCU",
    authDomain: "testwebfirestore.firebaseapp.com",
    projectId: "testwebfirestore",
    storageBucket: "testwebfirestore.appspot.com",
    messagingSenderId: "604806481003",
    appId: "1:604806481003:web:9fe9c74feaceb71ee91d2e"
  };
  // Initialize Firebase
  firebase.initializeApp(firebaseConfig);
</script>
</body>
</html>

pubspec.yaml:

name: test_web_firestore
description: A new Flutter application.

publish_to: 'none' # Remove this line if you wish to publish to pub.dev

version: 1.0.0+1

environment:
  sdk: ">=2.12.0-0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  firebase_core: ^0.8.0-1.0.nullsafety.1
  firebase_auth: ^0.21.0-1.1.nullsafety.1
  cloud_firestore: ^0.17.0-1.0.nullsafety.0



  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^1.0.2

dev_dependencies:
  flutter_test:
    sdk: flutter

# The following section is specific to Flutter.
flutter:

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

使用 flutter run --no-sound-null-safety 運行時,我收到另一個錯誤:

Error: [cloud_firestore/unknown] NoSuchMethodError: invalid member on null: 'includeMetadataChanges'
    at Object.throw_ [as throw] (http://localhost:62031/dart_sdk.js:5331:11)
    at collection_reference_web.CollectionReferenceWeb.new.get (http://localhost:62031/packages/cloud_firestore_web/src/write_batch_web.dart.lib.js:370:23)
    at get.next (<anonymous>)
    at http://localhost:62031/dart_sdk.js:39029:33
    at _RootZone.runUnary (http://localhost:62031/dart_sdk.js:38886:58)
    at _FutureListener.thenAwait.handleValue (http://localhost:62031/dart_sdk.js:33872:29)
    at handleValueCallback (http://localhost:62031/dart_sdk.js:34432:49)
    at Function._propagateToListeners (http://localhost:62031/dart_sdk.js:34470:17)
    at _Future.new.[_completeWithValue] (http://localhost:62031/dart_sdk.js:34312:23)
    at async._AsyncCallbackEntry.new.callback (http://localhost:62031/dart_sdk.js:34335:35)
    at Object._microtaskLoop (http://localhost:62031/dart_sdk.js:39173:13)
    at _startMicrotaskLoop (http://localhost:62031/dart_sdk.js:39179:13)
    at http://localhost:62031/dart_sdk.js:34686:9

我認為這是 Firebase 中的一個錯誤。 在他們修復它之前,您可以嘗試從以下位置更改 firebase_core/firebase_core_web/lib/src/interop/utils/utils.dart 的本地副本:

dynamic dartify(
  Object? jsObject, [
  Object Function(Object? object)? customDartify,
])

dynamic dartify(
  Object? jsObject, [
  Object? Function(Object object)? customDartify,
])

您還需要從 cloud_firestore/cloud_firestore_web/lib/src/interop/utils/utils.dart 中刪除as Object Function(Object?)?)

至少這為我解決了這個問題(目前)。

謝謝你,約翰·邁克爾·格里利奧。 將 firebase-fire.js 版本從 8.2.9 降級到 8.0.1 后,我可以在沒有 null 安全的情況下運行應用程序。

<script src="https://www.gstatic.com/firebasejs/8.0.1/firebase-firestore.js"></script>

使用 null 安全運行時出現相同的錯誤,但目前這不是問題

類型'列表的 FLutter dart 錯誤期望值<object> ' 但得到的類型之一是 'List<dynamic> '<div id="text_translate"><p> 我有一個像這樣的C類:</p><pre> class C { int? first; int? second; C({ this.first, this.second, }); }</pre><p> 我有兩個類,其中一個從另一個擴展:</p><pre> class A { int? id; List&lt;C&gt;? list; A({ this.id, this.list, }); } class B extends A{ String? title; B({ this.title, list, ):super( list: list, ); }</pre><p> 在我的手肘中,我創建了 B 類的實例,如下所示,但出現此錯誤:</p><pre> B b = B(title: '', list: []); expected value of Type List&lt;C&gt; but got one of type List&lt;dynamic&gt;</pre><p> 我的問題在哪里?</p></div></dynamic></object>

[英]FLutter dart error expected value of Type 'List<Object>' but got one of type 'List<dynamic>'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 類型'列表的 FLutter dart 錯誤期望值<object> ' 但得到的類型之一是 'List<dynamic> '<div id="text_translate"><p> 我有一個像這樣的C類:</p><pre> class C { int? first; int? second; C({ this.first, this.second, }); }</pre><p> 我有兩個類,其中一個從另一個擴展:</p><pre> class A { int? id; List&lt;C&gt;? list; A({ this.id, this.list, }); } class B extends A{ String? title; B({ this.title, list, ):super( list: list, ); }</pre><p> 在我的手肘中,我創建了 B 類的實例,如下所示,但出現此錯誤:</p><pre> B b = B(title: '', list: []); expected value of Type List&lt;C&gt; but got one of type List&lt;dynamic&gt;</pre><p> 我的問題在哪里?</p></div></dynamic></object> 期望一個“用戶”類型的值,但在嘗試將 Firestore 數據獲取到 flutter web 上的 PaginatedDataTable 時得到一個“Null”類型的值? flutter 錯誤:需要一個“int”類型的值,但得到一個“Null”類型的值 錯誤:需要一個“int”類型的值,但得到一個“String”類型的值; flutter Flutter 錯誤:應為“File”類型的值,但得到“FilePickerResult”類型的值 Flutter Web - 需要一個&#39;Map&#39;類型的值<String, dynamic> &#39;,但得到了“空”類型之一 如何修復“FirebaseError”類型的預期值,但在 Flutter web 中獲得“DomException”類型之一設置推送通知? Flutter | Navigator.pushNamed 我收到一個錯誤,沒有為類型“對象”定義運算符“[]”。 嘗試定義運算符'[]' Flutter 需要一個“int”類型的值,但得到一個“String”類型的值 需要一個“int”類型的值,但在 Flutter DataTable 中得到一個“String”類型的值
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM