StreamBuild:流,调用 returns Firestore Stream 在更新之前不检索数据的方法
StreamBuild: stream, calling a method that returns a Firestore Stream does not retrieve data until update
我希望有人可以解释为什么显式调用 Firestore.instance.collection('data').snapshots() 与调用时行为不同一个包含 Firestore.instance.collection('data').snapshots() 和 returns 流的方法。
调用包含 Firestore.instance.collection('data').snapshots() 的方法仅在对 firestore 数据进行更改后抓取数据快照.
class GlobalAppModel extends Model{
......//other global data elements
Stream getDataStreamSnapshots(){
Firestore.instance.collection('data').snapshots();
}
}
class DataDetailWidget2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
dataStream = AppModel.of(context);
....
body: Container(
child: StreamBuilder(
stream: Firestore.instance.collection('data').snapshots(), //This works great.
//stream: dataStream.getDataStreamSnapshots(), //This does not grab a snapshot until firestore is updated.
builder: (context, snapshot) {
.....
}
....
}
}
您只是在 getDataStreamSnapshots 方法中缺少 return 关键字。
class GlobalAppModel extends Model{
......//other global data elements
Stream getDataStreamSnapshots(){
return Firestore.instance.collection('data').snapshots(); // return added
}
}
我希望有人可以解释为什么显式调用 Firestore.instance.collection('data').snapshots() 与调用时行为不同一个包含 Firestore.instance.collection('data').snapshots() 和 returns 流的方法。
调用包含 Firestore.instance.collection('data').snapshots() 的方法仅在对 firestore 数据进行更改后抓取数据快照.
class GlobalAppModel extends Model{
......//other global data elements
Stream getDataStreamSnapshots(){
Firestore.instance.collection('data').snapshots();
}
}
class DataDetailWidget2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
dataStream = AppModel.of(context);
....
body: Container(
child: StreamBuilder(
stream: Firestore.instance.collection('data').snapshots(), //This works great.
//stream: dataStream.getDataStreamSnapshots(), //This does not grab a snapshot until firestore is updated.
builder: (context, snapshot) {
.....
}
....
}
}
您只是在 getDataStreamSnapshots 方法中缺少 return 关键字。
class GlobalAppModel extends Model{
......//other global data elements
Stream getDataStreamSnapshots(){
return Firestore.instance.collection('data').snapshots(); // return added
}
}