class Restaurant {
Restaurant({
required this.name,
required this.description,
required this.address,
required this.imageUrl,
});
Restaurant.fromJson(Map<String, Object?> json)
: this(
name: json['name']! as String,
description: json['description']! as String,
address: json['address']! as String,
imageUrl: json['imageUrl'] as String,
);
final String name;
final String description;
final String address;
final String imageUrl;
Map<String, Object?> toJson() {
return {
'name': name,
'description': description,
'address': address,
'imageUrl': imageUrl,
};
}
}
I get the list of restaurants from Firebase and when the user clicks Favorite Icon, I want to save that restaurant locally and retrieve the list of favorite restaurants and show it on another page. I know I can do it on Firebase directly but for now, I want it saved locally.
I am looking forward to hearing from all of you. Thank you.
Like this
List<Restaurant> restaturants = [Restuarant(...),Restuarant(...)];
List<String> encodedRestaturants = restaturants.map((res)=>json.encode(res.toJson())).toList();
//to write
prefs.setStringList("restaturants",encodedRestaturants);
//to read
List<String> decodedRestaturantsString = prefs.getStringList("restaturants");
List<Restaurant> decodedRestaturants = decodedRestaturantsString.map((res)=>Restaturant.fromJson(json.decode(res))).toList();