无限滚动listview-在构建过程中调用setState()或markNeedsBuild [英] Infinite Scroll listview - setState() or markNeedsBuild called during build

查看:146
本文介绍了无限滚动listview-在构建过程中调用setState()或markNeedsBuild的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个无限滚动listView,我想使用<$ c中的setState将 _loadingState 字符串从'loading ...'更改为'loaded' $ c> _loadNames 函数,但是当从 itemBuilder 调用_loadNames时,我得到了在构建错误期间被调用的setState。使用RefreshIndicator可以正常工作并更新项目,但滚动到底部会导致错误。我如何能够从ListViews构建器中调用_loadNames而不会出现错误或可以使用其他什么方法。
注意:不想使用redux或bloc。

I'm Creating an infinite scroll listView and i want to change the _loadingState String from 'loading...' to 'loaded' using setState in the _loadNames function, but when _loadNames is called from itemBuilder, I get the 'setState called during build error'.Using the RefreshIndicator works fine and updates the items, but scrolling to the bottom causes the error. How can i be able to call _loadNames from the ListViews builder without getting an error or what other approach can i use. NB: Dont want to use redux or bloc.

class _HomePageState extends State<HomePage> {
     List<String> names = [];
     List<String> _shownNames = [];
     int currentPage = 0;
     int limit = 20;
     String _loadingState = 'loading';
     bool loading = true;

     @override
     void initState() {
       super.initState();
       for (int i = 0; i < 200; i++) {
         names.add("hello $i");
       }
       _loadNames();
     }

     @override
     Widget build(BuildContext context) {
       // TODO: implement build
       return new Scaffold(
         appBar: new AppBar(title: new Text('User')),
         body: Column(children: <Widget>[
           Text(_loadingState),
           Expanded(child:_getListViewWidget()),
         ],)
       );
     }

     Widget _getListViewWidget(){
       ListView lv =  new ListView.builder(
         itemCount: _shownNames.length,
         itemBuilder: (context, index){
         if(index >= _shownNames.length - 5 && !loading){
           _loadNames(); // Getting error when this is called
         }
         return  ListTile(
           title: Text(_shownNames[index]),
         );
       });

       RefreshIndicator refreshIndicator = new RefreshIndicator(
         key: _refreshIndicatorKey,
         onRefresh: (){
           _loadNames();
           return null;
         },
         child: lv
       );
       return refreshIndicator;
     }

    _loadNames(){
       loading = true;
       setState(() {
         _loadingState = 'loading...';
       });

       new Timer(const Duration(seconds: 5), () {
          setState(() {
            _shownNames.addAll(names.getRange(currentPage, currentPage + limit));
           currentPage += limit;
           _loadingState = 'loaded';
         });
         loading = false;
       });
     }
  }


推荐答案

更改 _loadNames(){

_loadNames(){
   loading = true;
   // setState(() {
     _loadingState = 'loading...';
   // });

     onRefresh: (){
       _loadNames();
       return null;
     },

     onRefresh: (){
       setState(() => _loadNames());
     },

更新

_loadNames(){
   loading = true;

   new Future.delayed(Duration.zero, () => setState(() {
     _loadingState = 'loading...';
   }));

这篇关于无限滚动listview-在构建过程中调用setState()或markNeedsBuild的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆