类 'List<DocumentSnapshot>' 没有实例 getter 'documents'
Posted
技术标签:
【中文标题】类 \'List<DocumentSnapshot>\' 没有实例 getter \'documents\'【英文标题】:Class 'List<DocumentSnapshot>' has no instance getter 'documents'类 'List<DocumentSnapshot>' 没有实例 getter 'documents' 【发布时间】:2020-10-19 03:58:12 【问题描述】:我正在尝试实现应用程序在网格视图中显示除当前用户之外的所有用户详细信息的功能。我一直在尝试将snapshots
分配给_stream
,然后在StreamBuilder()
中应用_stream
值。但它会引发错误。
Database _database = Database();
Stream _stream;
String currentUserId;
@override
void initState()
getCurrentUserId(); //currentUserId gets its value here
getAllUsers();
super.initState();
getAllUsers() async
Stream<QuerySnapshot> snapshots = await _database.getAllUsers();
_stream = snapshots.map((querySnapshot) => querySnapshot.documents.where((documentSnapshot)
=> documentSnapshot.data["userId"] != currentUserId
).toList())
//..
StreamBuilder(
stream: _stream,
builder: (context, snapshot)
if (snapshot.data != null)
//..
//..
)
新:如果我添加as QuerySnapshot
,StreamBuilder
中的snapshot
将为空,而不是引发异常。
// NEW
_stream = snapshots.map((querySnapshot) => querySnapshot.documents.where((documentSnapshot)
=> documentSnapshot.data["userId"] != currentUserId
).toList() as QuerySnapshot);
数据库类
getAllUsers() async
return await _firestore.collection("users").snapshots();
例外
════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following NoSuchMethodError was thrown building StreamBuilder<dynamic>(dirty, state: _StreamBuilderBaseState<dynamic, AsyncSnapshot<dynamic>>#6e31f):
Class 'List<DocumentSnapshot>' has no instance getter 'documents'.
Receiver: Instance(length:2) of '_GrowableList'
Tried calling: documents
The relevant error-causing widget was:
StreamBuilder<dynamic> file:///Users/suriantosurianto/androidStudioProjects/apui/lib/fragments/home_fragment.dart:66:12
When the exception was thrown, this was the stack:
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
#1 _HomeFragmentState.build.<anonymous closure> (package:apui/fragments/home_fragment.dart:80:42)
#2 StreamBuilder.build (package:flutter/src/widgets/async.dart:509:81)
#3 _StreamBuilderBaseState.build (package:flutter/src/widgets/async.dart:127:48)
#4 StatefulElement.build (package:flutter/src/widgets/framework.dart:4619:28)
...
════════════════════════════════════════════════════════════════════════════════════════════════════
【问题讨论】:
错误告诉您querySnapshot
是List<QuerySnapshot>
,而不是QuerySnapshot
。所以,你必须把它当作一个列表。
你能详细说明一下吗?
【参考方案1】:
我们不知道该错误指向哪一行,但是如果我理解正确,原因可能是 StreamBuilder
属性 stream
的类型应该属于 Stream
类 (reference)。
似乎_stream
是从stream
使用toList
方法(reference)创建的list
。我会尝试删除这个toList
方法,看看会发生什么。
希望对你有帮助!
【讨论】:
_stream
不是列表,根据_stream.runtimeType
,不管toList()
方法,它都是_MapStream<QuerySnapshot, dynamic>
【参考方案2】:
// NEW
_stream = snapshots.map((querySnapshot) => querySnapshot.documents.where((documentSnapshot)
=> documentSnapshot.data["userId"] != currentUserId
).toList() as QuerySnapshot);
这里的querySnapshot
是List<DocumentSnapshot>
!!!
你可能不得不使用querySnapshot.where()
【讨论】:
它不起作用。为什么 querySnapshot 是 List正如其他人指出的那样,querySnapshot 是List<DocumentSnapshot>
而不是DocumentSnapshot
,因此您不能使用getter .documents
(该getter 仅用于DocumentSnapshot
,不适用于List
),这是因为正在检索除当前用户之外的每个用户的DocumentSnapshot
(每个用户的DocumentSnapshot
,所以List<DocumentSnapshot>
)。
如果你想返回 List<DocumentSnapshot>
类型的流,那么
_stream = snapshots
.map<List<DocumentSnapshot>>((querySnapshot) =>
querySnapshot.where((documentSnapshot) =>
documentSnapshot.data["userId"] != currentUserId)
);
【讨论】:
以上是关于类 'List<DocumentSnapshot>' 没有实例 getter 'documents'的主要内容,如果未能解决你的问题,请参考以下文章
类 'List<DocumentSnapshot>' 没有实例 getter 'documents'