DolDurma
DolDurma

Reputation: 17330

Flutter return array from response from server

in this part of my code await webApi.getKeywords(); return array which that can get from server, now when i try to return that i get error:

type 'List<dynamic>' is not a subtype of type 'String'

enter image description here

get data from server code:

  Future<List<KeywordsResponse>> _getKeywords(BuildContext context) async {
    try {
      final webApi = Provider.of<WebApi>(context);
      final response = await webApi.getKeywords();

      List<KeywordsResponse> list = List();
      if (response.statusCode == 200) {
        list = (json.decode(response.body) as List)
            .map((data) => new KeywordsResponse.fromJson(data))
            .toList();

        return list;
      } else {
        throw Exception('Failed to load photos');
      }
    } catch (error) {

      print(error);

      return null;
    }
  }

KeywordsResponse class:

@JsonSerializable(nullable: true)
class KeywordsResponse{
  @JsonKey(name:'id')
  final int id;

  @JsonKey(name:'title')
  final String title;

  @JsonKey(name:'description')
  final String description;

  KeywordsResponse(this.id, this.title, this.description);

  factory KeywordsResponse.fromJson(Map<String, dynamic> json) => _$KeywordsResponseFromJson(json);

  Map<String, dynamic> toJson() => _$KeywordsResponseToJson(this);
}

return of response.body:

[
  {
    "id": 1,
    "user_id": 1,
    "title": "asdasdasd",
    "description": "asdasdasd",
    "type": "post",
    "slug": "asdasdad",
    "featured_images": {
      "images": {
        "300": "/uploads/post_images/2019/300_1573573784.png",
        "600": "/uploads/post_images/2019/600_1573573784.png",
        "900": "/uploads/post_images/2019/900_1573573784.png",
        "original": "/uploads/post_images/2019/1573573784.png"
      },
      "thumbnail": "/uploads/post_images/2019/300_1573573784.png"
    },
    "lang": "fa",
    "visit": 0,
    "categories": [
      {
        "id": 1,
        "title": "aaaaaaa",
        "lang": "fa",
        "parent": 0,
        "pivot": {
          "contents_id": 1,
          "content_categories_id": 1
        }
      }
    ]
  },
  {
    "id": 2,
    "user_id": 1,
    "title": "asdasdasd",
    "description": "asdadasd",
    "type": "post",
    "slug": "asdasdasda",
    "featured_images": {
      "images": {
        "300": "/uploads/post_images/2019/300_1573573846.png",
        "600": "/uploads/post_images/2019/600_1573573846.png",
        "900": "/uploads/post_images/2019/900_1573573846.png",
        "original": "/uploads/post_images/2019/1573573846.png"
      },
      "thumbnail": "/uploads/post_images/2019/300_1573573846.png"
    },
    "lang": "fa",
    "visit": 0,
    "categories": [
      {
        "id": 2,
        "title": "bbbbbbbb",
        "lang": "fa",
        "parent": 0,
        "pivot": {
          "contents_id": 2,
          "content_categories_id": 2
        }
      }
    ]
  }
]

problem is on this line of code:

json.decode(response.body)

Upvotes: 1

Views: 313

Answers (1)

hoangquyy
hoangquyy

Reputation: 2073

Try this:

list = List<KeywordsResponse>.from(response.body.map((x) => KeywordsResponse.fromJson(x)));

Upvotes: 1

Related Questions