栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用streambuilder扑扑Firestore分页

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用streambuilder扑扑Firestore分页

请尝试以下代码:

class ProductList extends StatefulWidget {  @override  _ProductListState createState() => _ProductListState();}class _ProductListState extends State<ProductList> {  StreamController<List<documentSnapshot>> _streamController =  StreamController<List<documentSnapshot>>();  List<documentSnapshot> _products = [];  bool _isRequesting = false;  bool _isFinish = false;  void onChangeData(List<documentChange> documentChanges) {    var isChange = false;    documentChanges.forEach((productChange) {      if (productChange.type == documentChangeType.removed) {        _products.removeWhere((product) {          return productChange.document.documentID == product.documentID;        });        isChange = true;      } else {        if (productChange.type == documentChangeType.modified) {          int indexWhere = _products.indexWhere((product) { return productChange.document.documentID == product.documentID;          });          if (indexWhere >= 0) { _products[indexWhere] = productChange.document;          }          isChange = true;        }      }    });    if(isChange) {      _streamController.add(_products);    }  }  @override  void initState() {    Firestore.instance        .collection('products')        .snapshots()        .listen((data) => onChangeData(data.documentChanges));    requestNextPage();    super.initState();  }  @override  void dispose() {    _streamController.close();    super.dispose();  }  @override  Widget build(BuildContext context) {    return NotificationListener<ScrollNotification>(        onNotification: (ScrollNotification scrollInfo) {          if (scrollInfo.metrics.maxScrollExtent == scrollInfo.metrics.pixels) { requestNextPage();          }          return true;        },        child: StreamBuilder<List<documentSnapshot>>(          stream: _streamController.stream,          builder: (BuildContext context,   AsyncSnapshot<List<documentSnapshot>> snapshot) { if (snapshot.hasError) return new Text('Error: ${snapshot.error}'); switch (snapshot.connectionState) {   case ConnectionState.waiting:     return new Text('Loading...');   default:     log("Items: " + snapshot.data.length.toString());     return ListView.separated(       separatorBuilder: (context, index) => Divider(         color: Colors.black,       ),       itemCount: snapshot.data.length,       itemBuilder: (context, index) => Padding(         padding: const EdgeInsets.symmetric(vertical: 32),         child: new ListTile(title: new Text(snapshot.data[index]['name']),subtitle: new Text(snapshot.data[index]['description']),         ),       ),     ); }          },        ));  }  void requestNextPage() async {    if (!_isRequesting && !_isFinish) {      QuerySnapshot querySnapshot;      _isRequesting = true;      if (_products.isEmpty) {        querySnapshot = await Firestore.instance .collection('products') .orderBy('index') .limit(5) .getdocuments();      } else {        querySnapshot = await Firestore.instance .collection('products') .orderBy('index') .startAfterdocument(_products[_products.length - 1]) .limit(5) .getdocuments();      }      if (querySnapshot != null) {        int oldSize = _products.length;        _products.addAll(querySnapshot.documents);        int newSize = _products.length;        if (oldSize != newSize) {          _streamController.add(_products);        } else {          _isFinish = true;        }      }      _isRequesting = false;    }  }}

产品项的JSON:

{  "index": 1,  "name": "Pork",  "description": "Thịt heo/lợn"}

链接Github示例: https : //github.com/simplesoft-
duongdt3/flutter_firestore_paging



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/370966.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号