问题描述
我尝试获取存储在存储中的单选按钮的默认值( SharedPreferences
)。
I try to get default value for the radio button that stored in Storage (SharedPreferences
).
但是单选按钮不会将保存的值指向选择列表
下面是我的代码:
app_interface_widget.dart
:
class AppInterfaceDialogWidget extends StatefulWidget {
@override
_AppInterfaceDialogWidgetState createState() => _AppInterfaceDialogWidgetState();
}
class _AppInterfaceDialogWidgetState extends State<AppInterfaceDialogWidget> {
String pickRadioValue;
@override
void initState() {
setState(() {
this.initRadio();
});
super.initState();
}
Future initRadio() async {
pickRadioValue = await app.getPreferredLanguage();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
InkWell(
onTap: (){
setState(() {
pickRadioValue = "en";
app.setNewLanguage("en");
});
},
child: Row(
children: <Widget>[
Expanded(
child: Text("Language 1",style: TextStyle(color: Colors.grey,fontSize: 20.0),),
),
Radio(
activeColor: Colors.green,
groupValue: pickRadioValue,
value: "en",
onChanged: (val) {
setState(() {
pickRadioValue = val;
app.setNewLanguage("$pickRadioValue");
});
},
)
],
),
),
InkWell(
onTap: (){
setState(() {
pickRadioValue = "id";
app.setNewLanguage("id");
});
},
child: Row(
children: <Widget>[
Expanded(
child: Text("Language 2",style: TextStyle(color: Colors.grey,fontSize: 20.0),),
),
Radio(
activeColor: Colors.green,
groupValue: pickRadioValue,
value: "id",
onChanged: (val) {
setState(() {
pickRadioValue = val;
app.setNewLanguage("$pickRadioValue");
});
} ,
)
],
),
),
],
);
}
}
app.dart
:
class app {
Future<Null> init([String language]) async {
if (_locale == null){
await setNewLanguage(language);
}
return null;
}
Future<String> getPreferredLanguage() async {
return _getApplicationSavedInformation('app');
}
Future<String> _getApplicationSavedInformation(String name) async {
final SharedPreferences prefs = await _prefs;
return prefs.getString(_storageKey + name) ?? '';
}
Future setPreferredLanguage(String lang) async {
return _setApplicationSavedInformation('app', lang);
}
get currentLanguage => getPreferredLanguage();
get locale => _locale;
...
特别是在这一部分 of app_interface_widget.dart
:
...
String pickRadioValue;
@override
void initState() {
setState(() {
this.initRadio();
});
super.initState();
}
Future initRadio() async {
pickRadioValue = await app.getPreferredLanguage();
print ("DEBUG = "+pickRadioValue);
}
...
我有打印调试值( pickRadioValue
),它会在控制台日志中显示正确的结果,但在单选按钮上根本没有选择。但是如果我设置 pickRadioValue = en
,那么我的单选按钮将显示已保存的值/指向选择列表之一。...
i have print debug value (pickRadioValue
) and it show correct result at console log, but it wont make no choice at all at Radio Button. but if i set pickRadioValue = "en"
then my Radio button will show the saved value / pointing to the one of choice list....
任何想法吗?
推荐答案
我找到了解决方案,我们可以执行通过使用
,如下所示: then()
以其他方式实现异步功能
i found a solution, we can execute async function
in other wayby using then()
like below :
String pickRadioValue;
@override
void initState() {
setState(() {
ads.getPreferredLanguage().then((result){
setState(() {
print (":: debug result >>>"+result.toString());
pickRadioValue = result;
});
});
});
super.initState();
}
通过使用此代码,我的单选按钮显示保存为<$的默认值c $ c> SharedPreferences 。
by using this code, my Radio Button Show default value that saved at SharedPreferences
.
这篇关于Flutter-单选按钮不会显示保存在存储中的默认值(SharedPreferences)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!