【问题标题】:FutureBuilder - setState() or markNeedsBuild() called during build errorFutureBuilder - 在构建错误期间调用 setState() 或 markNeedsBuild()
【发布时间】:2021-04-21 15:56:39
【问题描述】:

我有以下 FutureBuilder 条目。

FutureBuilder(
                future: _checkConn,
                builder: (BuildContext context, AsyncSnapshot snapshot) {
                  switch (snapshot.connectionState){
                    case ConnectionState.none:
                      Navigator.pushReplacementNamed(context, NoConnView);
                      break;
                    case ConnectionState.active:
                    case ConnectionState.waiting:
                    case ConnectionState.done:
                      if(snapshot.data=='OK'){
                        initialfbget();
                        break;
                      } else {
                        Navigator.pushReplacementNamed(context, NoConnView);
                        break;
                      }
                  }
                  return SizedBox.expand(
                    child: FittedBox(
                      fit: BoxFit.fill,
                      child: SizedBox(
                        width: _vcontroller.value.size?.width ?? (MediaQuery.of(context).size.width),
                        height: _vcontroller.value.size?.height ?? (MediaQuery.of(context).size.height),
                        child: VideoPlayer(_vcontroller),
                      ),
                    ),
                  );

                }
            ),

以下是完整的 initstate 部分:

void initState()  {
    super.initState ();

    _vcontroller = VideoPlayerController.asset("assets/testimages/sukiitestanimation.mp4")
      ..initialize().then((_) {
        // Once the video has been loaded we play the video and set looping to true.
        _vcontroller.play();
        _vcontroller.setLooping(false);
        // Ensure the first frame is shown after the video is initialized.
      });

    _checkConn = checkConn();
    Firebase.initializeApp();
  }

下面是checkconn段:

Future<String> checkConn() async {
    var connresponse = await http.get(connurl).timeout(const Duration(seconds: 10));
    log('connresponse is: ${connresponse.statusCode}');
    if(connresponse.statusCode!=200) {
      return "BAD";
    } else {
      return "OK";
    }
  }

不断收到以下错误。

在构建期间调用 setState() 或 markNeedsBuild()。

在这方面的任何帮助都将不胜感激。

提前致谢。

【问题讨论】:

  • 您能分享您的checkConn() 和完整的initState() 方法吗?它将帮助另一个志愿者回答这个问题
  • ```Future checkConn() async { var connresponse = await http.get(connurl).timeout(const Duration(seconds: 10)); log('connresponse 是:${connresponse.statusCode}'); if(connresponse.statusCode!=200) { return "BAD"; } 其他 { 返回 "OK"; } }
  • @GilangPratama,重新粘贴请求的信息。 Future&lt;String&gt; checkConn() async { var connresponse = await http.get(connurl).timeout(const Duration(seconds: 10)); log('connresponse is: ${connresponse.statusCode}'); if(connresponse.statusCode!=200) { return "BAD"; } else { return "OK"; } }.
  • void initState() { super.initState (); _vcontroller = VideoPlayerController.asset("assets/testimages/sukiitestanimation.mp4") ..initialize().then((_) { // Once the video has been loaded we play the video and set looping to true. _vcontroller.play(); _vcontroller.setLooping(false); // Ensure the first frame is shown after the video is initialized. }); _checkConn = checkConn(); Firebase.initializeApp(); }

标签: flutter flutter-futurebuilder


【解决方案1】:

实际上,我不知道您的initState() 中的_checkConn = checkConn(); 是用来做什么的,因为您已经在futureBuilder 中声明了您的checkConn()。 我只能建议您使用以下 3 个选项来解决您的问题:

1。删除您在 initState 中的等待

只需删除您的_checkConn = checkConn();

2。在构建小部件期间删除您的 setState()

根据我的经验,当您在构建小部件期间调用 setState() 时,可能会发生如下所示的错误。

在构建期间调用 setState() 或 markNeedsBuild()。

所以,我假设您的构建中有一些 setState() 没有任何事件。

3。安装后使用

await 语法在initState() 中构建时可能会出错,您可以使用一些StatefulWidget() 生命周期 之一来构建它,即mounted

【讨论】:

    猜你喜欢
    • 2021-12-12
    • 2020-03-26
    • 2021-11-14
    • 2021-06-30
    • 2021-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-15
    相关资源
    最近更新 更多