【发布时间】:2019-10-18 02:09:58
【问题描述】:
注意:我已经看到关于 ListView 的延迟加载的答案,但这些答案是针对自定义 API 而不是 firestore 数据库!
我有一个书籍摘要应用程序,应用程序从我的 Firebase/Firestore 数据库中获取数据,然后使用包裹在 StreamBuilder 中的 ListView.builder 显示它。
现在,我想延迟获取数据,我的意思是当用户滚动列表时,所需的数据会被加载,而不是一次加载数据然后延迟显示。
//The Widget used to display data:
Widget feed() {
return Container(
width: deviceWidth,
height: deviceHeight / 3,
child: StreamBuilder(
stream: Firestore.instance
.collection('feedItem')
.orderBy('feedId', descending: true)
.snapshots(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
int totalLength = snapshot.data.documents.length;
return ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: totalLength > 10 ? 10 : totalLength,
itemBuilder: (BuildContext context, int index) {
return Container(
width: deviceWidth / 2.5,
child: GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => FeedIntro(
snapshot.data.documents[
((totalLength - 1) - index)]['feedId'])));
},
child: Card(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Container(
// width: 150,
height: 150,
foregroundDecoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(
snapshot.data.documents[index]['feedImage'],
),
fit: BoxFit.fill)),
),
Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(snapshot.data.documents[index]['title']),
)),
],
)),
),
);
},
);
} else if (snapshot.hasError) {
return Center(child: Text('Sorry Something went wrong!'));
} else {
return Center(
child: SizedBox(
child: CircularProgressIndicator(),
width: 50,
height: 50,
),
);
}
}),
);
}
【问题讨论】:
-
你所谓的“延迟加载”,很多人称之为“分页”。 Firestore 具有这种能力。 firebase.google.com/docs/firestore/query-data/query-cursors
-
要添加到@DougStevenson 评论,您可以将其与flutter firestore API(我认为firebase 团队尚未添加Dart 示例)从这里配对:pub.dev/documentation/cloud_firestore/latest/cloud_firestore/… 此外,这些方法是最近添加的到 Dart firestore 插件(dart 实现的不完整 API 可能是为什么团队尚未添加 dart 示例)。 :)
-
感谢您抽出宝贵时间!我知道了,我可以使用 limit 或 startAfter 来分段获取数据,但是对于我应该使用什么方法来集成,这与我当前的代码(StreamBuilder 和 ListView.Builder)有什么建议吗?
-
@RajDhakad 有任何解决方案吗?
-
@ShrutiRamnandanSharma 据我所知,firestore 中的所有数据都已缓存,因此在第一次(未缓存时)后,它会自动延迟获取。
标签: firebase asynchronous flutter dart google-cloud-firestore