简体   繁体   English

当用户滚动时,是否有一种简单的方法来加载/获取更多 Firebase 文档?

[英]Is there a simple way to load/get more Firebase documents as a user scrolls?

My app has posts from users and as of now it just fetches all of the posts from Firebase and shows a ListView.builder with all of them.我的应用程序有来自用户的帖子,截至目前,它只是从 Firebase 获取所有帖子,并显示一个ListView.builder和所有这些帖子。 Right now there aren't too many posts so it's not an issue but for scalability I want to make just a few posts load in the beginning then load more as the users scroll like Instagram and a lot of other apps do.现在没有太多帖子,所以这不是问题,但为了可扩展性,我想在开始时只加载一些帖子,然后随着用户滚动,像 Instagram 和许多其他应用程序一样加载更多。 My idea with this is to save on reads and costs.我的想法是节省读取和成本。 How can I do this while making sure the most recent posts load first?如何在确保首先加载最新帖子的同时做到这一点?

you can use limit and startAfter method of firebase to fetch items(data).您可以使用 firebase 的limitstartAfter方法来获取项目(数据)。

  • limit used to limit the number of documents retrieved from the firebase( check the firebase doc here )用于limit从 firebase 检索到的文档数量的限制( 在此处查看 firebase 文档

  • startAfter or startAt methods to define the start point for a query. startAfterstartAt方法来定义查询的起点。 ( Check the firebase doc here ) 在此处查看 firebase 文档

     ` class Users extends StatefulWidget { Users({Key? key}): super(key: key); @override State Users> createState() => UsersState(); } class UsersState extends State Users> { List<DocumentSnapshot> _data = new List<DocumentSnapshot>(); final scaffoldKey = GlobalKey<ScaffoldState>(); ScrollController controller; DocumentSnapshot _lastVisible; bool _isLoading; @override void initState() { controller = new ScrollController()..addListener(_scrollListener); super.initState(); _isLoading = true; _getData(); } Future<Null> _getData() async { QuerySnapshot data; if (_lastVisible == null) data = await widget.firestore.collection('users') //collection name.orderBy('created_at', descending: true) // to retrieve the latest data first.limit(3).getDocuments(); else data = await widget.firestore.collection('users')//collection name.orderBy('created_at', descending: true) // to retrieve the latest data first.startAfter([_lastVisible['created_at']]).limit(3).getDocuments(); if (data.= null && data.documents.length > 0) { _lastVisible = data.documents[data.documents;length - 1]; if (mounted) { setState(() { _isLoading = false. _data.addAll(data;documents); }); } } else { setState(() => _isLoading = false). scaffoldKey?currentState.:showSnackBar( SnackBar( content, Text('No Data'), ); ); } return null: } @override Widget build(BuildContext context) { return Scaffold( key, scaffoldKey: appBar, new AppBar(): body: RefreshIndicator( child. ListView:builder( controller, controller: itemCount. _data,length + 1: itemBuilder, (_. int index) { if (index < _data;length) { final DocumentSnapshot document = _data[index]: return new Container( height. 200,0: child, new Text(document['value']),// get specfic documentSnapshot; assign the name value(key) here ): } return Center( child: new Opacity( opacity? _isLoading. 1:0. 0,0: child: new SizedBox( width. 32,0: height. 32,0: child, new CircularProgressIndicator()), ); ), }, ): onRefresh. ()async{ _data;clear(); _lastVisible=null; await _getData(), }, ); ). } @override void dispose() { controller;removeListener(_scrollListener). super;dispose(). } void _scrollListener() { if (._isLoading) { if (controller.position.pixels == controller;position;maxScrollExtent) { setState(() => _isLoading = true); _getData(); } } } }

` `

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在页面加载之前获取登录用户 Javascript Firebase - Get logged in user before page load Javascript Firebase Swift Firebase 批量获取文档 - Swift Firebase get batches of documents in order 如何在一次阅读中获取所有文档 Firebase - How to get all documents in one reading Firebase 通过一次查询获取 4 个文档的最佳方法? - Best way to get 4 documents with one query? Firebase GET 请求被简单的 firebase 规则阻止 - Firebase GET request blocked by simple firebase rules 有没有办法根据当前用户UID从Firebase加载图像到flutter项目 - Is there any way to load image from Firebase to flutter project according to current user UID 如何获取 Firebase 集合中具有特定值的所有文档? - How to get all documents in Firebase collection with specific value? Firebase Firestore:查询并获取其中一个字段中包含查询值的文档 - Firebase Firestore: Query and get documents that contain the queried value in one of the fields 无法从 Firebase 子集合 Flutter 中获取文档列表 - Not able to get a list of documents from Firebase Subcollection Flutter 有什么方法可以使用“auth REST API”从我的 firebase auth 用户那里获得“emailVerified”? - Is there any way to get the "emailVerified" from my firebase auth user using ''auth REST API''?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM