0

I have a page from which I need to save data locally as a list. To save, I use SharedPrefs, there, through the model, I save the data as a list. But I ran into a problem that I can not specify the format for receiving data List, tell me how can I save data as a list and receive data as a list?

class RecentlySearchedModel {
  String name;
  String address;

  RecentlySearchedModel({
    required this.name,
    required this.address,
  });

  factory RecentlySearchedModel.fromJson(Map<String, dynamic> json) {
    return RecentlySearchedModel(
      name: json['name'] as String,
      address: json['address'] as String,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'name': name,
      'address': address,
    };
  }
}

repository

 @override 
  Future setResentlySearched({required List<RecentlySearchedModel> searchedList}) async {
    final SharedPrefs prefs = SharedPrefs();
    await prefs.setString(_name, jsonEncode( ))
  }

  @override 
  Future<List<RecentlySearchedModel>?> getResentlySearched() async {
    final SharedPrefs prefs = SharedPrefs();
    final data = await prefs.getString(_name);

    if (data == null) return null;

    return List<RecentlySearchedModel>.fromJson(
      jsonDecode(data),
    );
  }
2
  • convert your list into String json with jsonEncode. have you try it? what error you got? Commented Oct 6, 2022 at 12:18
  • There is no error, I just don't know the solution Commented Oct 6, 2022 at 12:19

2 Answers 2

1

update your repository like this.

 @override 
  Future setResentlySearched({required List<RecentlySearchedModel> searchedList}) async {
    final SharedPrefs prefs = SharedPrefs();
    await prefs.setString(_name, jsonEncode(searchedList.toJson());
  }

  @override 
  Future<List<RecentlySearchedModel>?> getResentlySearched() async {
    final SharedPrefs prefs = SharedPrefs();
    final data = await prefs.getString(_name);


    if (data == null) return null;
    Iterable l = json.decode(data);
    List<RecentlySearchedModel> posts = List<RecentlySearchedModel>.from(l.map((model)=> RecentlySearchedModel.fromJson(model)));    
   return posts;
  }
Sign up to request clarification or add additional context in comments.

2 Comments

And how can I do something that does not constantly save a new list, but supplement it with new elements, so that new elements are constantly added and the old ones are not deleted?
simply fetch the list from getResentlySearched and add one more element to that list. then store the updated list using setResentlySearched
1

i havent try to complie, but its should be like this

Future setResentlySearched({required List<RecentlySearchedModel> searchedList}) async {

    List<Map<String,dynamic>> listItem = searchedList.map((e)=> e.toJson()).toList();

    String jsonString = jsonEncode(listItem);
    final SharedPrefs prefs = SharedPrefs();
    await prefs.setString(_name, jsonString)
  }

and get it back

 Future<List<RecentlySearchedModel>?> getResentlySearched() async {
    final SharedPrefs prefs = SharedPrefs();
    final data = await prefs.getString(_name);

    if (data == null) return null;
    return (jsonDecode(data) as List).map((e)=> RecentlySearchedModel.fromJson(e)).toList();
   
  }

1 Comment

And how can I do something that does not constantly save a new list, but supplement it with new elements, so that new elements are constantly added and the old ones are not deleted?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.