飞镖:在Flutter应用程序中最小化对Firebase的访问



我有下面的小部件,它根据给定文档ID的任务的子集合构建待办事项列表。代码运行良好。

Widget buildFoodList() {
return SizedBox(
child: Container(
padding: const EdgeInsets.all(10.0),
child: StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('tasks').document(documentID).collection('todo')
.snapshots(),
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError)
return new Text('Error: ${snapshot.error}');
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return new Text('Loading...');
default:
return new ListView.builder(
shrinkWrap: true,
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) {
DocumentSnapshot ds = snapshot.data.documents[index];
return new Row(
children: <Widget>[
Expanded (child:Text(ds['deadline'].toString()) ),
Expanded (child:Text(ds['description']) ),
Expanded (child:Text("$"+ds['quantity'].toString()) ),
],
);
},
);
}
},
)
),
);

}

如您所见,我使用的是StreamBuilder。然而,我知道子集合不会改变。因此,问题是使用StreamBuilder是否是一种过度使用,因为使用流侦听可能会浪费资源和访问Firebase。更重要的是,使用Firebase的成本是以访问为基础计算的。

总之,问题在于是否有必要使用StreamBuilder。如果没有,有什么替代方法可以帮助避免不必要的访问Firebase。

谢谢。

StreamBuilder在需要获取firebase集合中的任何更新、插入或删除的应用程序中是必要的(在本例中(。另一种选择可以是FutureBuilder,它可以提取一次数据,然后你可以用Swipe来刷新(用户决定何时需要查看新数据(。

最新更新