代码之家  ›  专栏  ›  技术社区  ›  Tindona

在flatter中更新小部件之间的值

  •  0
  • Tindona  · 技术社区  · 2 年前

    我从API获取页面视图信息,我想用它的一个值更新一个文本小部件,但它是空的。 在卡片小部件中,值正在更新,但在从API获取值后,文本小部件的值没有改变,它保持为空。有人能帮忙吗?

     Center(
            child: Column(
              children: [
                Text("Actual Pet: " + category.toString()),    /// THIS VALUE DOESN'T CHANGE
                FutureBuilder<List<Pet>>(
                        future: API.get_pets(),
                        builder: (context, snapshot) {
                          if (snapshot.hasData) {
                              return Expanded(
                                child: PageView.builder(
                                  itemCount: number_of_parameters,
                                  itemBuilder: (context, index) {
                                    Pet pet = snapshot.data![index];
                                    category = pet.category.toString();
                                    return Card(
                                      child: Container(
                                        decoration: BoxDecoration(
                                                borderRadius:
                                                    BorderRadius.circular(15),
                                                image: DecorationImage(
                                                    image: image(photoURL).image,
                                                    fit: BoxFit.fitWidth),
                                              ),
                                        child: Column(children: [
                                          Text ("": category)
                                        ]),),);},),);
                          return const CircularProgressIndicator();
                          },},),],),),
    
    2 回复  |  直到 2 年前
        1
  •  0
  •   Jacob Miller    2 年前

    将不变的文本小部件放入FutureBuilder。它已经在当前的实现中构建,如果没有被告知状态是“脏的”,小部件就无法重建。

    编辑:考虑在FutureBuilder中包装该列,这可能更有意义,因为该列中的所有小部件都依赖于未来。

    这样地:

            child: 
                FutureBuilder<List<Pet>>(
                        future: API.get_pets(),
                        builder: (context, snapshot) {
                          if (snapshot.hasData) {
                              return Column(
                                children: [
                                  Text("Actual Pet: " + category.toString()),   
                                  Expanded(
                                    child: PageView.builder(
                                      itemCount: number_of_parameters,
                                      itemBuilder: (context, index) {
                                        Pet pet = snapshot.data![index];
                                        category = pet.category.toString();
                                        return Card(
                                          child: Container(
                                            decoration: BoxDecoration(
                                                    borderRadius:
                                                        BorderRadius.circular(15),
                                                    image: DecorationImage(
                                                        image: image(photoURL).image,
                                                        fit: BoxFit.fitWidth),
                                                  ),
                                            child: Column(children: [
                                              Text ("": category)
                                            ]),),);},),),
                                ],
                              );
                          return const CircularProgressIndicator();
                          },},),);```
    
        2
  •  0
  •   Kaushik Chandru    2 年前

    if(snapshot.data[0].category != null)Text("actual pet : ${snapshot.data[0].category}),