Persisting data in a flutter application

前端 未结 1 336
陌清茗
陌清茗 2021-01-27 05:31

I am building an app and in it, I have the names of people in a list from which I could add/delete, etc.. The problem is this list is not saved when I close the app, which is in

相关标签:
1条回答
  • 2021-01-27 06:07

    You could use this functions to persist and load data from shared preferences.

    Get SharedPreferences from here.

    To persist data to SharedPreferences, called after adding or deleting a new element to the list.

    _persistData() async {
        SharedPreferences preferences = await SharedPreferences.getInstance();
        await preferences.setStringList("persons", _people);
      }
    

    To load data from SharedPreferences, usually called in initState.

    _loadData() async {
        SharedPreferences preferences = await SharedPreferences.getInstance();
        setState(() {
          _people = preferences.getStringList("persons");
        });
      }
    
    0 讨论(0)
提交回复
热议问题