0

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

4
  • Whats the error? Commented Aug 27, 2021 at 17:40
  • updated the question to include the errors Commented Aug 27, 2021 at 19:38
  • Do you have the Json as a String or Map<String,dynamic>? Commented Aug 27, 2021 at 23:28
  • I am getting it from firebase as a string. How to parse the data received from firebase into a list of objects? Commented Aug 28, 2021 at 18:57

1 Answer 1

1

First: Your Json looks wrong, it's missing the double quotes:

[{"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}]

Second: In your Model shopId probably should be an int. If string is intended, you also need to put the id in double quotes.

Then use jsonDecode to convert the String into a List<Map<String,dynamic>> and convert it like that:

  List<HomeSlider> homeSliderList;
  homeSliderList= (jsonDecode(yourJsonString) as List)
      .map((i) => HomeSlider.fromJson(i))
      .toList();
Sign up to request clarification or add additional context in comments.

3 Comments

jsonDecode(jsonEncode(content)) I tried to encode the json but still not working. I will post the different errors
Dart Unhandled Exception: FormatException: Unexpected character (at character 2). I think there is a problem with the json returned from firebase. For some reason, firebase is not returning it with the proper format
I just encoded the json string before. Thanks

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.