0

this is my test code under try to search but not working.. it will be something i missed.. Please help me to resolve this.

test('http test', () async {
        var uri = Uri.parse(
            'https:....posts.json');
        var response = await http.get(uri);
        expect(response.statusCode, 200);

        NewsInfo newsInfo = NewsInfo.fromJson(jsonDecode(json.encode(response.body)));

        expect(newsInfo.id, "dc523f0ed25c");
      }); 

and this id json to dart

class NewsInfo {
  String id;
  int imageId;
  int imageThumbId;
  Metadata metadata;
  List<Paragraphs> paragraphs;
  Publication publication;
  String subtitle;
  String title;
  String url;

  NewsInfo(
      {this.id,
        this.imageId,
        this.imageThumbId,
        this.metadata,
        this.paragraphs,
        this.publication,
        this.subtitle,
        this.title,
        this.url});
  NewsInfo.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    imageId = json['imageId'];
    imageThumbId = json['imageThumbId'];
    metadata = json['metadata'] != null
        ? new Metadata.fromJson(json['metadata'])
        : null;
    if (json['paragraphs'] != null) {
      paragraphs = new List<Paragraphs>();
      json['paragraphs'].forEach((v) {
        paragraphs.add(new Paragraphs.fromJson(v));
      });
    }
    publication = json['publication'] != null
        ? new Publication.fromJson(json['publication'])
        : null;
    subtitle = json['subtitle'];
    title = json['title'];
    url = json['url'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
..

json looks like this under

[
  {
    "id": "dc523f0ed25c",
    "imageId": 2131165292,
    "imageThumbId": 2131165293,
    "metadata": {
      "author": {
        "name": "Pietro Maggi",
        "url": "https://medium.com/@pmaggi"
      },
      "date": "August 02",
      "readTimeMinutes": 1
    },
    "paragraphs": [
      {
        "markups": [],
        "text": "Working to make our Android application more modular, I ended up with a sample that included a set of on-demand features grouped inside a folder:",
        "type": "Text"
      },
      

and when i run the test it shows test/widget_test.dart 23:47 main. type 'String' is not a subtype of type 'Map<String, dynamic>' try to search but not working.. it will be something i missed.. Please help me to resolve this.

1 Answer 1

1

Your json response looks like a list. Try

List<NewsInfo> news = json.decode(response.body).map((el) => NewsInfo.fromJson(el)).toList();
Sign up to request clarification or add additional context in comments.

Comments

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.