Reputation: 35
I need to save the state of the toggle switch in my app and load it at the start. For that I use SharedPreferences
:
Future<bool> saveSwitchState(bool value) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool("switchState", value);
return prefs.setBool("switchState", value);
}
Future<bool> getSwitchState() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
isSwitchedFT = prefs.getBool("switchState");
print(isSwitchedFT);
return isSwitchedFT;
}
The saveSwitchState() runs every time when I change the toggle value.
The problem is at the start of the app.
I created a bool value: bool isSwitchedFT = false;
I initialize with false because null gives me errors.
How would I set isSwitchedFT = getSwitchState;
On empty value for isSwitchedFT it compiles but I get a red error on my emulator:
'package:flutter/src/material/toggleable.dart': Failed assertion: line 45 pos 15: 'tristate || value
!= null': is not true.
when compiled with a value the switch works fine an saves the changing value.
Switch(
value: isSwitchedFT,
onChanged: (bool value) {
setState(() {
isSwitchedFT = value;
saveSwitchState(value);
print('Saved state is $isSwitchedFT');
//switch works
});
print(isSwitchedFT);
},
activeTrackColor: Color(0xFF1D1F33),
activeColor: Colors.purple[500],
),
The thing I want is to load the app with the last value of the switch. Thank you.
Upvotes: 2
Views: 4392
Reputation: 35
Thanks everybody. Can't try it out right now. The structure of the project is made up of:
main.dart has both a stateles and statefull widgets. In the statefull widget I call the switch.dart and the json parts.
The initialization of the isSwitchedFT happens in the main file. Looks like it should be brought to the switch.dart file here is the full switch.dart file w/out the imports:
class SwitchIt extends StatefulWidget {
@override
_SwitchItState createState() => _SwitchItState();
}
class _SwitchItState extends State<SwitchIt> {
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
Switch(
value: isSwitchedFT,
onChanged: (bool value) {
setState(() {
isSwitchedFT = value;
saveSwitchState(value);
print('Saved state is $isSwitchedFT');
//switch works
});
print(isSwitchedFT);
},
activeTrackColor: Color(0xFF1D1F33),
activeColor: Colors.purple[500],
),
],
);
}
Future<bool> saveSwitchState(bool value) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool("switchState", value);
return prefs.setBool("switchState", value);
}
Future<bool> getSwitchState() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
isSwitchedFT = prefs.getBool("switchState");
print(isSwitchedFT);
return isSwitchedFT;
}
Maybe the solution is already posted. Can't check now. But the idea is to bring isSwitchedFT to switch.dart and use initState() to set its value from shared_preferences.
Upvotes: 0
Reputation: 9625
What you need to do is load the SharedPreferences and set your isSwitchedFT on the init state. But because you need an async and await, you need to do it on a separate method (I would also advise you to instantiate SharedPreferences only once and then re-use it like I've done below):
SharedPreferences prefs;
@override
void initState() {
loadSharedPreferencesAndSwitchState();
super.initState();
}
void loadSharedPreferencesAndSwitchState() async {
prefs = await SharedPreferences.getInstance();
isSwitchedFT = prefs.getBool("switchState");
}
Upvotes: 0
Reputation: 3767
Check this code for Sample
class _MyAppState extends State<MyApp> {
bool isSwitchedFT = false;
@override
void initState() {
// TODO: implement initState
super.initState();
getSwitchValues();
}
getSwitchValues() async {
isSwitchedFT = await getSwitchState();
setState(() {});
}
Future<bool> saveSwitchState(bool value) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool("switchState", value);
print('Switch Value saved $value');
return prefs.setBool("switchState", value);
}
Future<bool> getSwitchState() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
bool isSwitchedFT = prefs.getBool("switchState");
print(isSwitchedFT);
return isSwitchedFT;
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Container(
child: Center(
child: Switch(
value: isSwitchedFT,
onChanged: (bool value) {
setState(() {
isSwitchedFT = value;
saveSwitchState(value);
print('Saved state is $isSwitchedFT');
//switch works
});
print(isSwitchedFT);
},
activeTrackColor: Color(0xFF1D1F33),
activeColor: Colors.purple[500],
),
),
),
),
);
}
}
Upvotes: 9