jeudi 12 avril 2018

issues with shared preference and checkbox in flutter

I have un checkbox that I want save and load after change. It's work except when I remove the app and rebuild. Here is the message :enter image description here

here is my code : void initState() { super.initState();

   _loadswitchValue();  // when I remove "_loadswitchValue();", refresh app, 
                        // rewrite "_loadswitchValue();" refresh app, it's 
                        // work. but when I remove app and reinstall app 
                        // with _loadswitchValue();  I have a tristate issue
}


_loadswitchValue() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(() {

    _checkboxValueB = (prefs.getBool('counter10'));
   });
}

 _savenswitchValue() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(() {
  prefs.setBool('counter10', _checkboxValueB);

 });
}


 bool _checkboxValueB = false;

 @override
 Widget build(BuildContext context) {

return new Scaffold(
  body: new Container(
    padding: new EdgeInsets.all(2.0),
    child: new Column(
      children: <Widget>[

    new Checkbox(

      value: _checkboxValueB,
      onChanged: (bool value) {
        setState(() {
          _checkboxValueB = value;

          _savenswitchValue();

...




Aucun commentaire:

Enregistrer un commentaire