adplus-dvertising

Convert List from api to List<Map<String, dynamic>> and List<Map<String, String>> in flutter

Asked 5 months ago
Viewed 153 times

I have an api result in this format

{
    "response": true,
    "message": "success",
    "data": {
        "message": "Values fetched",
        "content": [
            {
                "id": 5,
                "name": "Car 5",
                "make": "Make 5",
                "created_at": "2022-06-11T12:13:48.000000Z",
                "updated_at": "2022-06-11T12:13:48.000000Z"
            },
            {
                "id": 6,
                "name": "Car 6",
                "make": "Make 6",
                "created_at": "2022-06-11T12:13:48.000000Z",
                "updated_at": "2022-06-11T12:13:48.000000Z"
            }
        ]
    }
}

I am trying to make them into a list option for smart_select package and I need to convert the result to List<Map<String, String>>
So far, I get these error Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<Map<String, dynamic>>' and this when I hot refresh Unhandled Exception: type 'List<Map<String, dynamic>>' is not a subtype of type 'List<Map<String, String>>' and lastly Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Map<String, String>' in type cast when I change dynamic to string with this code and I don't know how to convert to List<Map<String, dynamic>>

This is my code below from api

if (response.statusCode == 200) {
  List<Map<String, String>> values = (convert.jsonDecode(reply)['data']['content'] as List).map((e) => e as Map<String, dynamic>)?.toList();
}

asked 5 months ago

Correct Answer

Try to use this model

import 'dart:convert';

CarsDataModel carsDataModelFromJson(String str) => CarsDataModel.fromJson(json.decode(str));

String carsDataModelToJson(CarsDataModel data) => json.encode(data.toJson());

class CarsDataModel {
    CarsDataModel({
        this.response,
        this.message,
        this.data,
    });

    bool response;
    String message;
    Data data;

    factory CarsDataModel.fromJson(Map<String, dynamic> json) => CarsDataModel(
        response: json["response"],
        message: json["message"],
        data: Data.fromJson(json["data"]),
    );

    Map<String, dynamic> toJson() => {
        "response": response,
        "message": message,
        "data": data.toJson(),
    };
}

class Data {
    Data({
        this.message,
        this.content,
    });

    String message;
    List<Content> content;

    factory Data.fromJson(Map<String, dynamic> json) => Data(
        message: json["message"],
        content: List<Content>.from(json["content"].map((x) => Content.fromJson(x))),
    );

    Map<String, dynamic> toJson() => {
        "message": message,
        "content": List<dynamic>.from(content.map((x) => x.toJson())),
    };
}

class Content {
    Content({
        this.id,
        this.name,
        this.make,
        this.createdAt,
        this.updatedAt,
    });

    int id;
    String name;
    String make;
    DateTime createdAt;
    DateTime updatedAt;

    factory Content.fromJson(Map<String, dynamic> json) => Content(
        id: json["id"],
        name: json["name"],
        make: json["make"],
        createdAt: DateTime.parse(json["created_at"]),
        updatedAt: DateTime.parse(json["updated_at"]),
    );

    Map<String, dynamic> toJson() => {
        "id": id,
        "name": name,
        "make": make,
        "created_at": createdAt.toIso8601String(),
        "updated_at": updatedAt.toIso8601String(),
    };
}
answered 5 months ago