0

I need help managing state between pages.. In the first page I update my map to change a value. When I print this value it works but in my other page the value is still the first value I gave.. How can I do to keep this value even in my second page?

Setting the state : I update the value of color into a map depending on the user input

if (country.containsKey(guess)) {
                    setState(() {
                      _countryName.add(guess);
                      countries_info[guess]!["color"] = "Colors.green";

Second page : Here I want to change the color of the text depending on the value I changed in the first file

 Widget build(BuildContext context) {
    return Expanded(
      child: GridView.builder(
          itemCount: continentCountry.length,
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 3,
              crossAxisSpacing: 3,
              mainAxisSpacing: 3,
              childAspectRatio: 5),
          itemBuilder: (BuildContext context, int index) {
            return Container(
              decoration: BoxDecoration(
                border: Border.all(width: 3, color: splashBackground),
                color: purpleList,
              ),
              alignment: Alignment.center,
              height: 10,
              child: Text(continentCountry[index], style: setColors()),
            );
          }),
    );
  }
}

setColors() {
  for (final value in countries_info.values) {
    print(value["color"]);
    if (value["color"] == "Colors.green") {
      return TextStyle(color: Colors.green);
    } else {
      return TextStyle(color: Colors.red);
    }
  }
}
Coca95
  • 31
  • 5

2 Answers2

1

Read about, and pick one of all state management approaches.

https://docs.flutter.dev/development/data-and-backend/state-mgmt/options

Robert Sandberg
  • 6,832
  • 2
  • 12
  • 30
  • I'm using setState and I don't get why it doesn't work.. That's why I'm asking for help.. Read the docs already.. – Coca95 Mar 29 '23 at 17:43
  • "setState" is widget specific, so to manage a shared state between routes you need something else. You could e.g. go for flutter_bloc or riverpod or similar. – Robert Sandberg Mar 29 '23 at 17:51
  • I use the setState tu change the value of a key in my map, can I use a Provider to do so? I don't see how.. – Coca95 Mar 29 '23 at 18:04
  • If Provider is your choice, then you can expose the object using the Provider widget, then watch and adjust the object via the BuildContext. A small clarification, you can manage the state via "setState" as well by passing the value and a callback to e.g. child widgets as well. But that will quickly spiral out of control. – Robert Sandberg Mar 29 '23 at 18:15
  • I'm still new to this an I'm not sur I understand what you mean.. Do you have an example I can look into? – Coca95 Mar 29 '23 at 18:34
  • It's all in the link in my answer. Strictly opinionated, but I suggest that you go for either Riverpod or Bloc Library (flutter_bloc). There are other options on the link as well. – Robert Sandberg Mar 29 '23 at 18:46
0

One solution is when you push to second page there you need to pass your list as arguments and just do on second page with whatever operation you can do because you will get updated list there. As beginner level you can use setState but if your app have large scale then you need to move any State management. But at this time you see that how you can pass arguments from one page to another and pass your list.

AJJ
  • 46
  • 3