I already followed a few examples on YT and also an example on pub.dev and I still have a problem with saving the Switch value from false to true. Regarding documentation, it looks like everything is ok but still, it is not working. The question is what am I missing?
It is a very easy example with only this Switch. I wanted to check that in a simple way to learn it properly but it's not working. Can someone check the code and see what is going on?
It should Switch after a click from false to true and save it with shared preferences. After reloading or closing and running the app again it is still on false value.
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() {
runApp(
MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const HomePage(),
),
);
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
bool isChecked = false;
class _HomePageState extends State<HomePage> {
@override
void initState() {
loadBool();
super.initState();
}
loadBool() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
isChecked = preferences.getBool('isChecked') ?? false;
}
@override
Widget build(BuildContext context) {
@override
saveBool() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
await preferences.setBool('isChecked', isChecked);
}
return Scaffold(
appBar: AppBar(
title: const Text('Home Page'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('Something'),
Switch(
value: isChecked,
activeColor: Colors.green,
onChanged: (value) {
setState(() {
isChecked = value;
});
saveBool();
},
)
],
),
),
);
}
}
CodePudding user response:
i really encourage you to learn some state management way to get through this situations.
riverpod
, bloc
and provider
are all some approaches for state management.
also learn how null safety works in flutter .
i have reproduced your problem and cam up with the following solution :
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() {
runApp(
MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const HomePage(),
),
);
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
bool? isChecked;
@override
void initState() {
loadBool();
super.initState();
}
loadBool() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
setState(() {
isChecked = preferences.getBool('isChecked');
});
}
@override
Widget build(BuildContext context) {
@override
saveBool() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
await preferences.setBool('isChecked', isChecked!);
print(isChecked);
}
return Scaffold(
appBar: AppBar(
title: const Text('Home Page'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('Something'),
Switch(
value: isChecked!=null?isChecked!:false,
activeColor: Colors.green,
onChanged: (value) {
setState(() {
isChecked = value;
});
saveBool();
},
)
],
),
),
);
}
}
CodePudding user response:
Try the following code:
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() {
runApp(
MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const HomePage(),
),
);
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
bool isChecked = false;
@override
void initState() {
loadBool();
super.initState();
}
loadBool() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
isChecked = preferences.getBool('isChecked') ?? false;
}
@override
Widget build(BuildContext context) {
@override
saveBool() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
await preferences.setBool('isChecked', isChecked);
}
return Scaffold(
appBar: AppBar(
title: const Text('Home Page'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('Something'),
Switch(
value: isChecked,
activeColor: Colors.green,
onChanged: (value) {
setState(() {
isChecked = value;
});
saveBool();
},
)
],
),
),
);
}
}