Flutter-单选按钮不会显示保存在存储中的默认值(SharedPreferences)

时间:2019-07-15 13:32:12

标签: flutter dart

我尝试获取存储在存储(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;

...

特别是在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",那么我的单选按钮将显示保存的值/指向选择列表之一。...

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案,我们可以使用async function来通过其他方式执行then(),如下所示:

String pickRadioValue;
  @override
    void initState() {
      setState(() {
        ads.getPreferredLanguage().then((result){
          setState(() {
            print (":: debug result >>>"+result.toString());
            pickRadioValue = result;
          });
        });
      });
      super.initState();
}

使用此代码,我的单选按钮显示保存在SharedPreferences的默认值。