Flutter/Firebase - List<dynamic> 没有实例 getter 'documents'
Posted
技术标签:
【中文标题】Flutter/Firebase - List<dynamic> 没有实例 getter \'documents\'【英文标题】:Flutter/Firebase - List<dynamic> has no instance getter 'documents'Flutter/Firebase - List<dynamic> 没有实例 getter 'documents' 【发布时间】:2020-12-21 09:24:55 【问题描述】:我正在尝试在我的 Flutter 应用程序中组合两个 Firestore 流。但是,当我尝试实现合并流时,出现以下错误:
The following NoSuchMethodError was thrown building StreamBuilder<dynamic>(dirty, state: _StreamBuilderBaseState<dynamic, AsyncSnapshot<dynamic>>#25b40):
Class 'List<dynamic>' has no instance getter 'documents'.
Receiver: _List len:2
Tried calling: documents
我假设在建立我的流之前我需要执行另一个步骤,然后才能在页面浏览构建器中使用它?这是我的直播:
Stream<QuerySnapshot> getDefaultOccasions(BuildContext context) async*
yield* Firestore.instance.collection('datestoremember').document('default').collection('Dates_to_Remember').snapshots();
Stream<QuerySnapshot> getPersonalOccasions(BuildContext context) async*
final uid = await Provider.of(context).auth.getCurrentUID();
yield* Firestore.instance.collection('datestoremember').document(uid).collection('Dates_to_Remember').snapshots();
getData()
Stream stream1 = Firestore.instance.collection('datestoremember').document('default').collection('Dates_to_Remember').snapshots();
Stream stream2 = Firestore.instance.collection('datestoremember').document('Ngrx54m84JbsL0tGvrBeKCBlEnm2').collection('Dates_to_Remember').snapshots();
return StreamZip([stream1, stream2]);
然后我去这里实现这个:
child: StreamBuilder(
stream: getData(),
builder: (context, snapshot)
if(!snapshot.hasData) return const Text("Loading...");
return new PageView.builder(
itemCount: snapshot.data.documents.length,
controller: PageController(viewportFraction: 0.5),
onPageChanged: (int index) => setState(() => _index = index),
itemBuilder: (_, i)
return Transform.scale(
scale: i == _index ? 1 : 0.5,
child: Card(
elevation: 6,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(snapshot.data.documents[i]['Date'].toDate().day.toString()),
Text(DateFormat.MMMM()
.format(
formatter.parse(snapshot.data.documents[i]['Date'].toDate().toString()))
.toString()),
Padding(
padding: const EdgeInsets.only(
left: 8.0, right: 8.0),
child: FittedBox(
fit: BoxFit.contain,
child: Text(
snapshot.data.documents[i]['Title'],
overflow: TextOverflow.ellipsis,
),
),
)
],
),
),
);
,
);,
),
有什么想法吗?干杯
【问题讨论】:
【参考方案1】:试试这个:
getData()
Stream stream1 = Firestore.instance.collection('datestoremember').document('default').collection('Dates_to_Remember').snapshots();
Stream stream2 = Firestore.instance.collection('datestoremember').document('Ngrx54m84JbsL0tGvrBeKCBlEnm2').collection('Dates_to_Remember').snapshots();
return StreamGroup.merge([stream1, stream2]).asBroadcastStream();
使用 StreamZip 编辑:
Stream<List<QuerySnapshot>> getData()
Stream stream1 = Firestore.instance.collection('datestoremember').document('default').collection('Dates_to_Remember').snapshots();
Stream stream2 = Firestore.instance.collection('datestoremember').document('Ngrx54m84JbsL0tGvrBeKCBlEnm2').collection('Dates_to_Remember').snapshots();
return StreamZip<List<QuerySnapshot>>([stream1, stream2]);
在 StreamBuilder 中:
child: StreamBuilder(
stream: getData(),
builder: (BuildContext context, AsyncSnapshot<List<QuerySnapshot>> snapshot)
List<QuerySnapshot> combinedSnapshot = snapshot.data.toList();
combinedSnapshot[0].documents.addAll(combinedSnapshot[1].documents);
if(!combinedSnapshot[0].hasData) return const Text("Loading...");
return new PageView.builder(
itemCount: combinedSnapshot[0].data.documents.length,
controller: PageController(viewportFraction: 0.5),
onPageChanged: (int index) => setState(() => _index = index),
itemBuilder: (_, i)
return Transform.scale(
scale: i == _index ? 1 : 0.5,
child: Card(
elevation: 6,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(combinedSnapshot[0].data.documents[i]['Date'].toDate().day.toString()),
Text(DateFormat.MMMM()
.format(
formatter.parse(combinedSnapshot[0].data.documents[i]['Date'].toDate().toString()))
.toString()),
Padding(
padding: const EdgeInsets.only(
left: 8.0, right: 8.0),
child: FittedBox(
fit: BoxFit.contain,
child: Text(
combinedSnapshot[0].data.documents[i]['Title'],
overflow: TextOverflow.ellipsis,
),
),
)
],
),
),
);
,
);,
),
【讨论】:
感谢鲁本,这消除了我的错误,但合并后似乎一次只显示一个原始流,而不是像我希望的那样合并。任何想法如何解决这个问题? 那么我建议像你以前一样使用 StreamZip 并尝试投射这个 StreamZip。我更新了我的答案。 谢谢,我已经实现了更改,但我现在遇到了一个新错误:type '_MapStream以上是关于Flutter/Firebase - List<dynamic> 没有实例 getter 'documents'的主要内容,如果未能解决你的问题,请参考以下文章
Flutter firebase, List<dynamic> 不是 List<String> 类型
将 Future<List> 从 Firestore 转换为 List<String> - Flutter Firebase [重复]