Flutter settings switch is not turned on when I click on back button

95 views
Skip to first unread message

shahbaz shiekh

unread,
Jan 18, 2021, 5:02:21 PM1/18/21
to Flutter Development (flutter-dev)
Hi , 

I have a settings menu which I have used different methods for the Onchanged 
and I want my application to keep the switch checked once I click on it but when I click the back button and back into the settings menu it is back to false 

please help I have tried multiple avenues still can't get this to work: 
 through shared preference: 

activateIcyMistHighlighter(bool value) async {
setState(() {
// checkValue = true;
_icymistSwitch = true;
_icymistColor = true;
_icymistEnable = true;
_heartColor = false;
_heartSwitch = false;
// _heartEnable = !_heartEnable;
_heartEnable = true;
_deepoceanColor = false;
_deepoceanSwitch = false;
_deepoceanEnable = true;
// _deepoceanEnable = !_deepoceanEnable;
_flowerColor = false;
_flowerSwitch = false;
_flowerEnable = true;
// _flowerEnable = !_flowerEnable;
_gardenColor = false;
_gardenSwitch = false;
_gardenEnable = true;
// _gardenEnable = !_gardenEnable;
_paradiseColor = false;
_paradiseSwitch = false;
_paradiseEnable = true;
// _paradiseEnable = !_paradiseEnable;
_midnightparadiseColor = false;
_midnightparadiseSwitch = false;
_midnightparadisEnable = true;
// _midnightparadisEnable = !_midnightparadisEnable;
_midnightColor = false;
_midnightEnable = true;
// _midnightEnable = !_midnightEnable;
_midnightSwitch = false;
});
SharedPreferences prefs = await SharedPreferences.getInstance();
bool saved = prefs.getBool('switch') ?? false;
return saved;
}

activateHeartHighlighter(bool value) {
setState(() {
_heartColor = true;
_heartSwitch = true;
_heartEnable = true;
_icymistColor = false;
_icymistSwitch = false;
_icymistEnable = true;
_deepoceanColor = false;
_deepoceanSwitch = false;
_deepoceanEnable = true;
_flowerColor = false;
_flowerSwitch = false;
_flowerEnable = true;
_gardenColor = false;
_gardenSwitch = false;
_gardenEnable = true;
_paradiseColor = false;
_paradiseSwitch = false;
_paradiseEnable = true;
_midnightparadiseColor = false;
_midnightparadiseSwitch = false;
_midnightparadisEnable = true;
_midnightColor = false;
_midnightEnable = true;
_midnightSwitch = false;
// if (activateIcyMistHighlighter(value)) {
// setState(() {
// // _heartColor = false;
// });
}
// }
);
}
// }



Switch(
// contentPadding: const EdgeInsets.all(0),
// value: _icymistSwitch,
value: _icymistSwitch,
activeColor: Colors.cyan,
// title: Text("final one"),
// onChanged: (bool value) async =>
// activateIcyMistHighlighter(value)),

onChanged: (bool value) async =>
await activateIcyMistHighlighter(value)),


// back button
appBar: AppBar(
backgroundColor: Colors.cyan,
title: Text("Settings Menu",
style: TextStyle(color: styling.appBarTextcolor)),
leading: FlatButton(
textColor: Colors.white,
child: Icon(styling.appBarBackArrowIcon),
// onPressed: () async {
// bool valueishere = await _getvalue();
// Navigator.of(context).push(MaterialPageRoute(
// builder: (context) =>
// valueishere ? NavDrawer() : AliflaammeemPg2(),
onPressed: () => Navigator.pop(context, false),
// MaterialPageRoute(builder: (context) => NavDrawer()),
// setState
// }
// ),
// onPressed: () async {
// bool valueishere = await savedata();
// Navigator.of(context).push(MaterialPageRoute(
// builder: (context) =>
// valueishere ? NavDrawer() : AliflaammeemPg2(),
// }
// ));


Please help.... 

Suzuki Tomohiro

unread,
Jan 18, 2021, 8:08:48 PM1/18/21
to Flutter Development (flutter-dev)
How does _icymistSwitch get initialized using SharedPreferences?

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/2c1eed02-e741-451a-b793-fac3c4b517ben%40googlegroups.com.

shahbaz....@gmail.com

unread,
Jan 18, 2021, 9:08:49 PM1/18/21
to Suzuki Tomohiro, Flutter Development (flutter-dev)
Well I have 8 switches
And IcymistSwitch is false in the beginning but that of a user clicks on one of the switches it envokes a method 
Icymisthighlighter which has icymisswitch and all the other switches to show once that method set state is to on the rest will be off and vice versa .... 

Sent from my iPhone

On Jan 18, 2021, at 7:08 PM, Suzuki Tomohiro <suz...@gmail.com> wrote:



Suzuki Tomohiro

unread,
Jan 18, 2021, 9:16:43 PM1/18/21
to Flutter Development (flutter-dev)
Asking again; How does _icymistSwitch get initialized using SharedPreferences?

shahbaz....@gmail.com

unread,
Jan 18, 2021, 11:06:36 PM1/18/21
to Suzuki Tomohiro, Flutter Development (flutter-dev)
I don’t get it ... 

Icymistswitch is not getting initialized by shared preference it’s icymisthighlighter the one that has shared preference adding within its method ... is that where I am wrong?

Sent from my iPhone

On Jan 18, 2021, at 8:16 PM, Suzuki Tomohiro <suz...@gmail.com> wrote:



Suzuki Tomohiro

unread,
Jan 18, 2021, 11:21:36 PM1/18/21
to Flutter Development (flutter-dev)
The _icymistSwitch determines the Switch’s on/off state in your code:

Switch(
...
value: _icymistSwitch,


You want to remember its value by SharedPreferences.

shahbaz....@gmail.com

unread,
Jan 19, 2021, 6:53:39 AM1/19/21
to Suzuki Tomohiro, Flutter Development (flutter-dev)
I tried doing that 
Made a method and called it in the value space
But I am getting an error 
addBoolToSF() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool('boolValue', true);
} The error was saying this is not A bool value

Sent from my iPhone

On Jan 18, 2021, at 10:21 PM, Suzuki Tomohiro <suz...@gmail.com> wrote:



Lucas Correa

unread,
Jan 19, 2021, 7:53:11 AM1/19/21
to shahbaz....@gmail.com, Suzuki Tomohiro, Flutter Development (flutter-dev)
I think that problem is not in this line... can you show complete msg error and/or all this piece of code?



--
Lucas Correa

Suzuki Tomohiro

unread,
Jan 19, 2021, 12:11:00 PM1/19/21
to shahbaz....@gmail.com, Flutter Development (flutter-dev)
Hi Shahbaz,

Read the error message and the stack trace carefully. The “this is not a bool value” error didn’t occur at the line.
I guess the error occurred at an if-statement that tried to read the value through shared preference.

If you think your analysis is correct, copy-paste the entire error message including the stack trace.

shahbaz shiekh

unread,
Jan 19, 2021, 6:26:03 PM1/19/21
to Suzuki Tomohiro, Flutter Development (flutter-dev)
Hi Suzuki, 

Please see below this is the error I am getting when I add the shared preference method withn the value space 

Pleas help I have been stuck on this for a while now, I even gave away 50 rep points in stack overflow but no luck …..
The activateIcyMistHighlighter is the method that controls the unchanged so I thought I need to save that preference, 
But when I save the preference for just the switch I get an error. 







The settings page looks like this: 




Suzuki Tomohiro

unread,
Jan 19, 2021, 7:10:11 PM1/19/21
to Flutter Development (flutter-dev)
Where is the stack trace of the error?
Reply all
Reply to author
Forward
0 new messages