读取 Firestore 中的最后一个条目时出现问题 - Class“_JsonQuerySnapshot”没有实例方法“[]”
Problem reading last entry in Firestore - Class '_JsonQuerySnapshot' has no instance method '[]'
我正在尝试像这样阅读来自 Firebase Friestore 的最后一个条目:
这是我的直播:
final getLastEmotion = _emotionsDbReference
.orderBy('timeStamp', descending: true)
.limit(1)
.snapshots();
这是我的流生成器:
StreamBuilder(
stream: getLastEmotion,
builder: (BuildContext context,
AsyncSnapshot lastEmotionSnapshot) {
if (lastEmotionSnapshot.data == null)
return CircularProgressIndicator();
if (lastEmotionSnapshot.hasError) {
return new Text(
'Error in receiving snapshot: ${lastEmotionSnapshot.error}');
}
if (!lastEmotionSnapshot.hasData) {
return Center(
child: CircularProgressIndicator(
backgroundColor: Theme.of(context).primaryColor,
),
);
}
final _lastEmotion = lastEmotionSnapshot.data!['emotion'];
return Text('Last emotion: $_lastEmotion');
}),
我收到以下错误:
Class '_JsonQuerySnapshot' has no instance method '[]'.
Receiver: Instance of '_JsonQuerySnapshot'
Tried calling: []("emotion")
有谁知道可能是什么问题?
请帮忙:)
您需要使用
snapshot.data!.data()
获取文档流的数据,这样你的代码就可以
final _lastEmotion = lastEmotionSnapshot.data!.data()['emotion'];
你可以这样使用
StreamBuilder(
stream: getLastEmotion,
builder: (BuildContext context, AsyncSnapshot lastEmotionSnapshot) {
if (lastEmotionSnapshot.data == null) {
return const CircularProgressIndicator();
}
if (lastEmotionSnapshot.hasError) {
return Text(
'Error in receiving snapshot: ${lastEmotionSnapshot.error}');
}
if (!lastEmotionSnapshot.hasData) {
return Center(
child: CircularProgressIndicator(
backgroundColor: Theme.of(context).primaryColor,
),
);
}
final _lastEmotion = lastEmotionSnapshot.data!.docs[0]['emotion'];
return Text('Last emotion: $_lastEmotion');
})
我正在尝试像这样阅读来自 Firebase Friestore 的最后一个条目:
这是我的直播:
final getLastEmotion = _emotionsDbReference
.orderBy('timeStamp', descending: true)
.limit(1)
.snapshots();
这是我的流生成器:
StreamBuilder(
stream: getLastEmotion,
builder: (BuildContext context,
AsyncSnapshot lastEmotionSnapshot) {
if (lastEmotionSnapshot.data == null)
return CircularProgressIndicator();
if (lastEmotionSnapshot.hasError) {
return new Text(
'Error in receiving snapshot: ${lastEmotionSnapshot.error}');
}
if (!lastEmotionSnapshot.hasData) {
return Center(
child: CircularProgressIndicator(
backgroundColor: Theme.of(context).primaryColor,
),
);
}
final _lastEmotion = lastEmotionSnapshot.data!['emotion'];
return Text('Last emotion: $_lastEmotion');
}),
我收到以下错误:
Class '_JsonQuerySnapshot' has no instance method '[]'.
Receiver: Instance of '_JsonQuerySnapshot'
Tried calling: []("emotion")
有谁知道可能是什么问题?
请帮忙:)
您需要使用
snapshot.data!.data()
获取文档流的数据,这样你的代码就可以
final _lastEmotion = lastEmotionSnapshot.data!.data()['emotion'];
你可以这样使用
StreamBuilder(
stream: getLastEmotion,
builder: (BuildContext context, AsyncSnapshot lastEmotionSnapshot) {
if (lastEmotionSnapshot.data == null) {
return const CircularProgressIndicator();
}
if (lastEmotionSnapshot.hasError) {
return Text(
'Error in receiving snapshot: ${lastEmotionSnapshot.error}');
}
if (!lastEmotionSnapshot.hasData) {
return Center(
child: CircularProgressIndicator(
backgroundColor: Theme.of(context).primaryColor,
),
);
}
final _lastEmotion = lastEmotionSnapshot.data!.docs[0]['emotion'];
return Text('Last emotion: $_lastEmotion');
})