Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

flutter

dart

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?

like image 510
Adeeb Mark Avatar asked Jan 24 '26 07:01

Adeeb Mark


1 Answers

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);    
}
like image 80
Will Hlas Avatar answered Jan 25 '26 22:01

Will Hlas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!