Adeeb Mark
Adeeb Mark

Reputation: 141

Save List Texts of data using flutter_secure_storage... is it possible?

I am using flutter_secure_storage in my Flutter app...

I am saving data as a key-value pair with it. It takes a value of type String like this:

 await storage.write(key: 'currentUserID', value: 'userid');

and I am reading it like this:

currentUserID = (await storage.read(key: 'currentUserID'));

but currentUserID it just a String!

What if I have a List of Strings... is there any way to save them all in the storage?

What I need is like if I have:

List<String> listViewerBuilderString = ['a1','a2','a3']

What I need to do is like call the array to = the Saved array in the storage something like this:

List<String> listViewerBuilderString = (await storage.read(key: 'listOfItems'));

Is this even possible? Or is there a better way to do this?

Upvotes: 2

Views: 3691

Answers (1)

Will Hlas
Will Hlas

Reputation: 1351

This can be achieved by using JSON serialization like so:

void myFunction() async {
  const storage = FlutterSecureStorage();
  List<String> listViewerBuilderString = ['a1','a2','a3'];
  await storage.write(key: 'listOfItems', value: jsonEncode(listViewerBuilderString));
  String? stringOfItems = await storage.read(key: 'listOfItems');
  List<dynamic> listOfItems = jsonDecode(stringOfItems!);
  print(listOfItems);    
}

Upvotes: 8

Related Questions