在构建期间调用setState()或markNeedsBuild()-设置从API提取的变量的值时? [英] setState() or markNeedsBuild() called during build - When setting the value of a variable fetched from an API?

查看:83
本文介绍了在构建期间调用setState()或markNeedsBuild()-设置从API提取的变量的值时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有一些json对象-

I have a few json objects here -

{
    "error": "0",
    "message": "Got it!",
    "data": [
        {
            "status": false,
            "_id": "5e004fc92638d21e5f7e2ffc",
            "group_id": "5dfc7136790365f0955deb2b",
            "date": "2019-12-23T00:00:00.000Z",
            "title": "creating new task",
            "priority": 4,
            "description": "details",
            "tasks": [],
            "created_date": "2019-12-23T05:25:29.524Z",
            "__v": 0
        },
        {
            "status": false,
            "_id": "5e004ff798224784c87baff0",
            "group_id": "5dfc712d790365d5a55deb2a",
            "date": "2019-12-23T00:00:00.000Z",
            "title": "new task",
            "priority": 5,
            "description": "details",
            "tasks": [],
            "created_date": "2019-12-23T05:26:15.621Z",
            "__v": 0
        }
    ]
}

我正在以这种方法显示从这些json对象获取的数据-

I am displaying the fetched data from these json objects in this method -

  Widget listViewWidget(List<Post> data) {
....
}

我的应用程序使用PageView小部件,该小部件以这种方式基于创建日期("created_date"参数)显示json对象-

My application uses a PageView widget which displays the json objects based on the date it was created ("created_date" parameter) this way -

.我还定义了一个整数int numberOfTasksLeft;,该整数应保存data.length的值,但是每次导航到新页面(上一页或下一页)时,我都需要更新它.如果一天有两个json对象,则该整数的值应为2,以此类推.请问如何正确设置此整数的值的建议?

. I have also defined an integer int numberOfTasksLeft; which should hold the value of data.length But i need it to update everytime I am navigating to a new page (previous or next page). If there are two json objects for one day, the integer should have value of 2, etc. Could i get a suggestion on how to correctly set the value of this integer?

我试图以这种方式使用setState方法-

I have tried to use setState method this way -

  Widget listViewWidget(List<Post> data) {
//settervalue = data;

  setState(() {
    numberofTasks = data.length;
  });

...
}

但这会导致错误-setState() or markNeedsBuild() called during build.

推荐答案

Flutter就像JavaScript先完成同步代码,然后在下一个事件循环中完成异步代码一样. setState() or markNeedsBuild() called during build.,发生此错误的原因是,即使在构建同步完成之前,您仍在调用setState.因此,如果您在异步块中将调用更改为setState,则可以解决此问题.也许是一种怪异的方式.

Flutter just like JavaScript finishes the synchronous code first and then the asynchronous code in the next event loop. setState() or markNeedsBuild() called during build., this error occurs because even before the build finishes synchronously you are calling setState. So if you change the call to setState in an async block you solve this problem. Maybe in a hacky way though.

Widget listViewWidget(List<Post> data) {

  Timer(Duration(seconds: 0), () {
     setState(() {
       //settervalue = data;
       numberofTasks = data.length;
     });
  });


...
}

这篇关于在构建期间调用setState()或markNeedsBuild()-设置从API提取的变量的值时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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