简体   繁体   中英

How to add item to Dart Flutter Secure Storage stringList?

My goal is to save a list with Secure Storage and add new items to this list. There will be a list. There will be items in it. I want to add an item to that list later. In the methods I tried, it did delete the entire list. I couldn't add an item to the already existing list.

How can I do it?

I am currently stuck here:

    Future<void> listUpload() async {
    final prefences = await SharedPreferences.getInstance();
    prefences.setStringList("values", ["Value 1"]);
    var printList = prefences.getStringList("testler");
    debugPrint(printList.toString());
    setState() {
    
    }
  }

Give this a go:

Future<void> listUpload() async {
    final prefences = await SharedPreferences.getInstance();
    // The debug print is unnecessary but there if you want it.
    // var printList = prefences.getStringList("values");
    // debugPrint(printList.toString());

    setState(() {
      prefences.setStringList("values", prefences.getStringList("values")! + ["Your value"]);
    });
  }

Couple things about your set state:

Firstly, it needed to be wrapped in one more set of brackets. ((){}) instead of (){} .

Also, as we can't exactly edit the list, let's just grab the list and whatever value we need to the end of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM