Home > Software design >  updating a value between widgets in flutter
updating a value between widgets in flutter

Time:07-25

I am getting a PageView information from an API, and I want to update a text widget with one of its values, but it is null. In the Card widget the value is updating but it is not changing for the Text widget after getting values from the API, it remains null. Could anyone help with this?

 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();
                      },},),],),),

CodePudding user response:

Put the Text widget that doesn't change in the FutureBuilder. It's already built in the current implementation, and the widgets just can't rebuild without being told the state is "dirty".

Edit: Consider wrap the Column in the FutureBuilder, that might make more sense because all the widgets in the column are dependent on the future.

Like this:

        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();
                      },},),);```

CodePudding user response:

Move the text widget inside future builder. Wrap the expanded with a column. Add the actual pet text inside the column widget just above the expanded. You might need index so you can do it like this

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