flutterdart

Why is GetStorage() not retaining value of multiple containers?


I have made a container where, normally, it is a grey color. However, if you longPress it, it will turn green. Now, I am using GetStorage(), and for this one container, it worked perfectly. It would write the data and save it, so that even if I quit the app and reopened it, my container would still be green. Now I would like to add this longPress feature to my other container, yet I am encountering an issue. I made 2 separate methods so that each container gets assigned a different method, and so that each container can act individually. Yet, GetStorage will not save the color data for this second container; why is this? I can longPress and change its color, but the color does not get saved when I quit & reopen the app. The code for the 2 containers is basically identical, so I don't understand why it only works for Container 1. Any tips on how to fix this are greatly appreciated!

Here is the code for initializing Container 1:

late Color colorContainer2 = Colors.white24;
  void initState() {
    colorContainer = Color(
      GetStorage().read<int>('colorContainer') ?? Colors.white24.value,
    );
  }

And here is the code for using GetStorage container 1:

        InkWell(
            onLongPress: () {
              setState(() {
                colorContainer = colorContainer == Colors.white24
                    ? Colors.green.shade600
                    : Colors.white24;
                GetStorage().write('colorContainer', colorContainer.value);
              });
            },
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => const Sune_CLL(),
                ),
              );
            },
            child: Container(
              width: 500,
              color: colorContainer,
              height: 125,
              

Initializing for Container 2:

late Color colorContainer2 = Colors.white24;

 void initState2() {
    colorContainer2 = Color(
      GetStorage().read<int>('colorContainer2') ?? Colors.white24.value,
    );
  }

-Using GetStorage on Container 2:

       InkWell(
            onLongPress: () {
              setState(() {
                colorContainer2 = colorContainer2 == Colors.white24
                    ? Colors.green.shade600
                    : Colors.white24;
                GetStorage().write('colorContainer2', colorContainer2.value);
              });
            },
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => const ASune_CLL(),
                ),
              );
            },
            child: Container(
              width: 500,
              color: colorContainer2,
              height: 125,

Solution

  • Are u initializing this containers in Statefull widget ? Statefull widgets have initState function, so I think your initState2 is not calling when widget created. ( Because initState function is widget's own function, but initState2 function is created by you its not calling when widget created )

    And also u have 2 different containers but u only defined 1 Color variable, i think u have to create 2 different color Container for initial color of containers like

    Color colorContainer = Colors.grey;
    Color colorContainer2 = Colors.grey;
    

    Ur initState function must be like this :

    void initState {
     colorContainer = Color(
          GetStorage().read<int>('colorContainer') ?? Colors.white24.value,
        );
    
    colorContainer2 = Color(
          GetStorage().read<int>('colorContainer2') ?? Colors.white24.value,
        );
       }