简体   繁体   English

如何将项目添加到 Dart Flutter 安全存储字符串列表?

[英]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:给这个 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:关于你的集合 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.此外,由于我们无法完全编辑列表,所以我们只获取列表以及我们需要的任何值到它的末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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