【问题标题】:setState() or markNeedsBuild() called during build Query Widget在构建查询小部件期间调用 setState() 或 markNeedsBuild()
【发布时间】:2021-02-03 13:08:55
【问题描述】:

我对 Graphql Flutter 有疑问。 我正在尝试显示使用 graphql 检索到的搜索列表。

但颤振告诉我:

这个小部件不能被标记为需要构建,因为框架已经在构建小部件的过程中。仅当其祖先之一当前正在构建时,小部件才能在构建阶段标记为需要构建。这个例外是允许的,因为框架在子组件之前构建父小部件,这意味着将始终构建脏后代。否则,框架可能不会在此构建阶段访问此小部件。

我添加了一些 Future Builder 来避免此类问题,但不幸的是它似乎不起作用

这是我的代码:

Column(
                            children: [
                              Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: <Widget>[
                                  Flexible(
                                    flex: 5,
                                    child: AnimatedContainer(
                                      duration: Duration(milliseconds: 500),
                                      padding: EdgeInsets.symmetric(horizontal: 25),
                                      height: 42.0,
                                      width: width,
                                      decoration: BoxDecoration(
                                        borderRadius:
                                        BorderRadius.circular(20.0),
                                        color: const Color(0xffffffff),
                                        boxShadow: [
                                          BoxShadow(
                                            color: const Color(0x29000000),
                                            offset: Offset(0, 0),
                                            blurRadius: 12,
                                          ),
                                        ],
                                      ),
                                      child: Form(
                                        child: Row(
                                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                                          children: [
                                            Flexible(
                                              flex: 1,
                                              child: Container(
                                                width: 17.0,
                                                height: 17.0,
                                                decoration: BoxDecoration(
                                                  image: DecorationImage(
                                                    image: const AssetImage(
                                                        'assets/images/XDSearch.png'),
                                                    fit: BoxFit.cover,
                                                  ),
                                                ),
                                              ),
                                            ),
                                            Flexible(
                                              flex: 6,
                                              child: TextFormField(
                                                onChanged: (value){
                                                  setState(() {
                                                    textSearch = value;
                                                  });
                                                },
                                                keyboardType: TextInputType.emailAddress,
                                                decoration: const InputDecoration(
                                                    focusedBorder: InputBorder.none,
                                                    enabledBorder: InputBorder.none,
                                                    errorBorder: InputBorder.none,
                                                    disabledBorder: InputBorder.none,
                                                    border: InputBorder.none,
                                                    hintText: 'Recherche'
                                                ),
                                                style: TextStyle(
                                                  fontFamily: 'Roboto',
                                                  fontSize: 16,
                                                  color: const Color(0x8c000000),
                                                  fontWeight: FontWeight.w700,
                                                ),
                                                textAlign: TextAlign.left,
                                                focusNode: _focus,
                                              ),
                                            )
                                          ],
                                        ),
                                      ),
                                    ),
                                  ),
                                  clickOnBar == false ? Flexible(
                                    flex: 1,
                                    child: Column(
                                      mainAxisAlignment: MainAxisAlignment.center,
                                      crossAxisAlignment: CrossAxisAlignment.end,
                                      children: [
                                        Container(
                                          width: 30.0,
                                          height: 30.0,
                                          margin: EdgeInsets.fromLTRB(0, 0, 0, 0),
                                          decoration: BoxDecoration(
                                            borderRadius: BorderRadius.circular(15.0),
                                            image: DecorationImage(
                                              image: const AssetImage('assets/images/XDProfil.png'),
                                              fit: BoxFit.fill,
                                            ),
                                          ),
                                        )
                                      ],
                                    ),
                                  ) : Container()
                                ],
                              ),
                              textSearch != null && textSearch != 'undefined' && textSearch.isNotEmpty  ? FutureBuilder(
                                  builder: (context, projectSnap) {
                                    return Query(
                                      options: QueryOptions(
                                          documentNode: gql("""
                                            query getListSearch(\$text : String!){
                                              listSearchProPages(text: \$text) {
                                                categorie {
                                                  idCat
                                                  title
                                                }
                                                subCat {
                                                  idSubCat
                                                  title
                                                }
                                                pagePro {
                                                  idPagesPro
                                                  title
                                                }
                                              }
                                            }
                                          """
                                          ),
                                          variables: <String, dynamic>{
                                            "text": textSearch,
                                          },
                                          pollInterval: 10
                                      ),
                                      builder: (QueryResult result, {VoidCallback refetch, FetchMore fetchMore}){
                                        print(result);

                                        if (result.hasException) {
                                          return Text(result.exception.toString());
                                        }
                                        if (result.loading) {
                                          return Text('Loading');
                                        }

                                        var tmpObject = {
                                          "categorie": result.data['listSearchProPages'].first['categorie'],
                                          "subCat":  result.data['listSearchProPages'].first['subCat'],
                                          "pagePro":  result.data['listSearchProPages'].first['pagePro'],
                                        };

                                        return FutureBuilder(
                                            future: updateListSearch(tmpObject),
                                            builder: (context, projectSnap){
                                              if(tmpObject.length > 0) {
                                                ListView.builder(
                                                    itemCount: tmpObject.length,
                                                    itemBuilder: (context, index) {
                                                      print(tmpObject);
                                                      final repository = tmpObject[index];
                                                      print(repository["categorie"]
                                                          .first['title']);
                                                      return Text(
                                                          repository["categorie"]
                                                              .first['title']);
                                                    }
                                                );
                                              }else {
                                                return Container();
                                              }
                                              return Container();
                                            }
                                        );
                                      },
                                    );
                                  }
                              ) : Container()
                            ],
                          ),

你有解决办法吗?

【问题讨论】:

  • 这与您的 updateListSearch() 方法有关。也请分享一下。
  • 谢谢@Akif,我的 updateListSearch 是一个 setState tmpObject -> listSearch。但我需要做一个 setstate 但我不知道在哪里

标签: flutter


【解决方案1】:

请不要像现在这样在每次调用 build 方法时都创建一个新的 future。

您需要创建一个未来一次(或者可能更频繁,但肯定不是在每个构建中),然后在构建方法中使用 那个 未来:

在你的状态类中创建一个你未来类型的变量。

在您的 initState 方法中,通过将 updateListSearch(tmpObject) 的结果分配给您的变量来创建未来。

在您的构建方法中,使用 变量 作为 FutureBuilder 中未来的参数。

【讨论】:

    猜你喜欢
    • 2018-05-15
    • 2022-08-22
    • 2020-10-05
    • 2020-12-12
    • 2020-12-28
    • 2021-01-06
    • 2021-06-06
    • 2021-05-31
    相关资源
    最近更新 更多