Flutter 拨动开关和共享首选项
Flutter toggle switch and shared preferences
我需要在我的应用程序中保存切换开关的状态并在启动时加载它。为此,我使用 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;
}
每当我更改切换值时,saveSwitchState() 都会运行。
问题出在应用程序启动时。
我创建了一个布尔值:bool isSwitchedFT = false;
我用 false 初始化,因为 null 给我错误。
我将如何设置 isSwitchedFT = getSwitchState;
对于 isSwitchedFT 的空值,它可以编译,但我的模拟器出现红色错误:
'package:flutter/src/material/toggleable.dart': Failed assertion: line 45 pos 15: 'tristate || value
!= null': is not true.
当使用一个值编译时,开关可以正常工作并保存更改的值。
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],
),
我想要的是用开关的最后一个值加载应用程序。
谢谢。
检查示例代码
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],
),
),
),
),
);
}
}
您需要做的是加载 SharedPreferences 并将您的 isSwitchedFT 设置为初始状态。但是因为你需要一个异步和等待,你需要在一个单独的方法上完成它(我也建议你只实例化 SharedPreferences 一次然后像我在下面所做的那样重新使用它):
SharedPreferences prefs;
@override
void initState() {
loadSharedPreferencesAndSwitchState();
super.initState();
}
void loadSharedPreferencesAndSwitchState() async {
prefs = await SharedPreferences.getInstance();
isSwitchedFT = prefs.getBool("switchState");
}
谢谢大家。现在还不能试用。
项目的结构由:
- main.dart
- switch.dart
- jsonLogic.dart
main.dart 有一个有状态的和有状态的小部件。
在有状态小部件中,我调用 switch.dart 和 json 部分。
isSwitchedFT 的初始化发生在主文件中。
看起来它应该被带到 switch.dart 文件中
这是完整的 switch.dart 文件 w/out 导入:
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;
}
也许解决方案已经发布了。现在查不到。但想法是将 isSwitchedFT 带到 switch.dart 并使用 initState() 将其值设置为 shared_preferences.
我需要在我的应用程序中保存切换开关的状态并在启动时加载它。为此,我使用 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;
}
每当我更改切换值时,saveSwitchState() 都会运行。
问题出在应用程序启动时。
我创建了一个布尔值:bool isSwitchedFT = false;
我用 false 初始化,因为 null 给我错误。
我将如何设置 isSwitchedFT = getSwitchState;
对于 isSwitchedFT 的空值,它可以编译,但我的模拟器出现红色错误:
'package:flutter/src/material/toggleable.dart': Failed assertion: line 45 pos 15: 'tristate || value
!= null': is not true.
当使用一个值编译时,开关可以正常工作并保存更改的值。
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],
),
我想要的是用开关的最后一个值加载应用程序。 谢谢。
检查示例代码
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],
),
),
),
),
);
}
}
您需要做的是加载 SharedPreferences 并将您的 isSwitchedFT 设置为初始状态。但是因为你需要一个异步和等待,你需要在一个单独的方法上完成它(我也建议你只实例化 SharedPreferences 一次然后像我在下面所做的那样重新使用它):
SharedPreferences prefs;
@override
void initState() {
loadSharedPreferencesAndSwitchState();
super.initState();
}
void loadSharedPreferencesAndSwitchState() async {
prefs = await SharedPreferences.getInstance();
isSwitchedFT = prefs.getBool("switchState");
}
谢谢大家。现在还不能试用。 项目的结构由:
- main.dart
- switch.dart
- jsonLogic.dart
main.dart 有一个有状态的和有状态的小部件。 在有状态小部件中,我调用 switch.dart 和 json 部分。
isSwitchedFT 的初始化发生在主文件中。 看起来它应该被带到 switch.dart 文件中 这是完整的 switch.dart 文件 w/out 导入:
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;
}
也许解决方案已经发布了。现在查不到。但想法是将 isSwitchedFT 带到 switch.dart 并使用 initState() 将其值设置为 shared_preferences.