Home > Enterprise >  How to add item to Dart Flutter Secure Storage stringList?
How to add item to Dart Flutter Secure Storage stringList?

Time:03-05

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

CodePudding user response:

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.

  • Related