Flutter - Radio Button Wont Show Default Value tha

2019-08-26 19:57发布

问题:

I try to get default value for the radio button that stored in Storage (SharedPreferences).

but the radio button wont pointing the saved value to the choice list

below are my code :

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;

...

Especially in this part 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);
  }
...

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....

Any Idea ?

回答1:

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();
}

by using this code, my Radio Button Show default value that saved at SharedPreferences.



标签: flutter dart