SharedPreferences save multikey with list string flutter
- User
SharedPreferences save multikey with list string flutter
- Assistant
In Flutter, you can use the shared_preferences package to save multiple key-value pairs with a list of strings in SharedPreferences. Here's an example of how you can do it:
First, make sure to add the shared_preferences package to your pubspec.yaml file:
dependencies: flutter: sdk: flutter shared_preferences: ^2.0.1Then, import the shared_preferences package in your Dart file:
import 'package:shared_preferences/shared_preferences.dart';Now, you can save multiple key-value pairs with a list of strings in SharedPreferences using the following code:
void saveData(List<String> dataList) async { SharedPreferences prefs = await SharedPreferences.getInstance(); // Clear the previous saved list await prefs.remove('dataList'); // Save the new list await prefs.setStringList('dataList', dataList); } Future<List<String>> getData() async { SharedPreferences prefs = await SharedPreferences.getInstance(); // Retrieve the saved list List<String> dataList = prefs.getStringList('dataList') ?? []; return dataList; }In the above code, the
saveDatafunction saves a list of strings with the key 'dataList' in SharedPreferences. It first removes any previous list saved with the same key and then saves the new list using thesetStringListmethod.The
getDatafunction retrieves the saved list using thegetStringListmethod. If no list is found, it returns an empty list.You can call the
saveDatafunction to save a list of strings, and then call thegetDatafunction to retrieve the saved list as follows:List<String> myList = ['Item 1', 'Item 2', 'Item 3']; // Save the list saveData(myList); // Retrieve the saved list List<String> retrievedList = await getData(); print(retrievedList); // Output: [Item 1, Item 2, Item 3]By using the shared_preferences package, you can easily save multiple key-value pairs with a list of strings in SharedPreferences in your Flutter application.
- User
if i use dataList1 and dataList2
- Assistant
If you want to save two different lists with different keys, you can modify the code as follows:
void saveData(List<String> dataList1, List<String> dataList2) async { SharedPreferences prefs = await SharedPreferences.getInstance(); // Clear the previous saved lists await prefs.remove('dataList1'); await prefs.remove('dataList2'); // Save the new lists await prefs.setStringList('dataList1', dataList1); await prefs.setStringList('dataList2', dataList2); } Future<List<String>> getData(String key) async { SharedPreferences prefs = await SharedPreferences.getInstance(); // Retrieve the saved list based on the given key List<String> dataList = prefs.getStringList(key) ?? []; return dataList; }With the modified code, you can save and retrieve two different lists using the respective keys. Here's an example of how you can use it:
List<String> myList1 = ['Item 1', 'Item 2', 'Item 3']; List<String> myList2 = ['Item A', 'Item B', 'Item C']; // Save the lists saveData(myList1, myList2); // Retrieve the saved lists List<String> retrievedList1 = await getData('dataList1'); List<String> retrievedList2 = await getData('dataList2'); print(retrievedList1); // Output: [Item 1, Item 2, Item 3] print(retrievedList2); // Output: [Item A, Item B, Item C]In this example,
dataList1anddataList2are saved and retrieved separately using their respective keys ('dataList1' and 'dataList2').