I have this json that I am fetching from firebase realtime database
[{image: https://cdn.searchenginejournal.com/wp-content/uploads/2019/07/the-essential-guide-to-using-images-legally-online-1520x800.png, shopId: 1},
{image: https://cdn.searchenginejournal.com/wp-content/uploads/2019/07/the-essential-guide-to-using-images-legally-online-1520x800.png, shopId: 2}]
When I try to parse it, I am receiving different errors: First way:
List<HomeSlider> posts = List<HomeSlider>.from(l.map((model)=> HomeSlider.fromJson(model)));
imgList.addAll(List<HomeSlider>.from(data.value) => HomeSlider.HomeSlider.fromJson(json));
Second way:
Map<dynamic, dynamic> yearMap = data.value;
yearMap.forEach((key, value) {
imgList.add(HomeSlider.fromJson(value));
});
I have the home slider object:
import 'package:json_annotation/json_annotation.dart';
/// This allows the `User` class to access private members in
/// the generated file. The value for this is *.g.dart, where
/// the star denotes the source file name.
part 'HomeSlider.g.dart';
@JsonSerializable()
class HomeSlider{
HomeSlider(this.image, this.shopId);
String image="";
String shopId="";
/// A necessary factory constructor for creating a new User instance
/// from a map. Pass the map to the generated `_$UserFromJson()` constructor.
/// The constructor is named after the source class, in this case, User.
factory HomeSlider.fromJson(Map<String, dynamic> json) => _$HomeSliderFromJson(json);
/// `toJson` is the convention for a class to declare support for serialization
/// to JSON. The implementation simply calls the private, generated
/// helper method `_$UserToJson`.
Map<String, dynamic> toJson() => _$HomeSliderToJson(this);
}
Errors are: 'String' is not a subtype of type 'Map<String, dynamic>' or w json exception if I try to encode it
I have generated the class as by the documentation using flutter CLI Any suggestions? Thanks