支持的[field]类型有[String]和[FieldPath]Flutterfire



所以我试图从firebase中的文档中获取一些信息。我已经做了很多次了,但这次我得到了一个奇怪的错误

Error: Assertion failed: file:///E:/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore_platform_interface-2.2.1/lib/src/platform_interface/platform_interface_document_snapshot.dart:69:12
field is String || field is FieldPath
"Supported [field] types are [String] and [FieldPath]"
at Object.throw_ [as throw] (http://localhost:61463/dart_sdk.js:4351:11)
at Object.assertFailed (http://localhost:61463/dart_sdk.js:4294:15)
at platform_interface_document_snapshot.DocumentSnapshotPlatform.new.get (http://localhost:61463/packages/cloud_firestore_platform_interface/src/platform_interface/platform_interface_write_batch.dart.lib.js:651:66)
at cloud_firestore.QueryDocumentSnapshot.__.get (http://localhost:61463/packages/cloud_firestore/cloud_firestore.dart.lib.js:713:73)
at cloud_firestore.QueryDocumentSnapshot.__._get (http://localhost:61463/packages/cloud_firestore/cloud_firestore.dart.lib.js:716:19)
at emailLogIn (http://localhost:61463/packages/xspectre/Login/DarkSignUp.dart.lib.js:35657:105)
at emailLogIn.next (<anonymous>)
at http://localhost:61463/dart_sdk.js:37968:33
at _RootZone.runUnary (http://localhost:61463/dart_sdk.js:37822:58)
at _FutureListener.thenAwait.handleValue (http://localhost:61463/dart_sdk.js:32783:29)
at handleValueCallback (http://localhost:61463/dart_sdk.js:33331:49)
at Function._propagateToListeners (http://localhost:61463/dart_sdk.js:33369:17)
at async._AsyncCallbackEntry.new.callback (http://localhost:61463/dart_sdk.js:33094:27)
at Object._microtaskLoop (http://localhost:61463/dart_sdk.js:38083:13)
at _startMicrotaskLoop (http://localhost:61463/dart_sdk.js:38089:13)
at http://localhost:61463/dart_sdk.js:33586:9

我已经将错误定位到这一行,它正在从文档中检索字段

await prefs.setString('nickname', documents[0]['nickname']);

我不知道为什么会出现这个错误。我正在使用cloud_firestore 14.4

这是我如何获得数据的完整代码

QuerySnapshot snapshot = await FirebaseFirestore.instance.collection('users').where('email',isEqualTo: user.email.toString()).get();
DocumentSnapshot documents = snapshot.docs[0];
prefs = await SharedPreferences.getInstance();
await prefs.setString('nickname', documents[0]['nickname'.toString()].toString());
await prefs.setString('email',  documents[0]['email'.toString()].toString());
await prefs.setString('photo',  documents[0]['photo'].toString());
Navigator.of(context).pushNamed(Home.route);

请注意,错误发生在所有类似的行中
我的主要目标是使用firebase电子邮件/密码身份验证登录用户,并从他们的firestore文档中获取他/她的数据

我不知道为什么要在documents上使用另一个整数索引。您已经使用snapshot.docs[0]从查询结果中提取了第一个文档。如果您想从一个文档中获取字段,只需使用所需的字段名称字符串对其进行索引即可。

DocumentSnapshot snapshot = snapshot.docs[0];
String nickname = snapshot["nickname"];

在开始对数组进行索引之前,还应该检查是否有任何文档。查询很可能返回0个文档。

最新更新