Home > Software engineering >  How can I get api Map data in my App SinglePage - Flutter
How can I get api Map data in my App SinglePage - Flutter

Time:02-19

I need some help , How can I get SinglePage data from Api .

When I trying to get data show me this error : type '(dynamic) => ProductOrderModel' is not a subtype of type '(String, dynamic) => MapEntry<dynamic, dynamic>' of 'transform'.

I hope I can find a solution from here. I am soo tired to get data.

Thanks...................

This is my controller :

class ProductOrderController extends GetxController {
      getProductOrderDetails() async {
        final sharedPreferences = await SharedPreferences.getInstance();
        var token = sharedPreferences.getString('accessToken');
        //
        final res = await dio.get(baseUrl   'customer/order/invoice/9',
            options: Options(
              headers: {'Authorization': 'Bearer $token'},
            ));
        final List<ProductOrderModel> orderDetailsData = res.data
            .map((json) => ProductOrderModel.fromJson(json))
            .toList()
            .cast<ProductOrderModel>();
    
            
        print('------------------------------>');
        print('Status Code : ${res.statusCode}');
        print('Headers : ${res.headers}');
        print('realUri : ${res.realUri}');
        print('statusMessage : ${res.statusMessage}');
        print('Category Data : $orderDetailsData');
        print('requestOptions : ${res.requestOptions}');
        // print(res.body);
        // print(res.unauthorized);
        print('------------------------------>');
    
        orderDetailsData.addAll(orderDetailsData);
        print(orderDetailsData);
      }}

My Api Is :

{
    "orderInfo": {
        "orderIdPrimary": 9,
        "customerId": "15",
        "shippingId": "9",
        "orderTotal": "930.00",
        "discount": "0",
        "bkashFee": "0",
        "trackingId": "215707",
        "orderStatus": "1",
        "deliveryman_id": null,
        "cancelRequest": "0",
        "point": "9.15",
        "pointamount": "0.00",
        "created_at": "17-02-2022",
        "updated_at": "2022-02-17T12:20:43.000000Z",
        "ordertype": {
            "id": 1,
            "name": "Accepted",
            "slug": "accepted",
            "created_at": null,
            "updated_at": null
        },
        "customer": {
            "id": 15,
            "fullName": "Fsd Ramjan",
            "phoneNumber": "01779565300",
            "address": null,
            "email": null,
            "verifyToken": "1",
            "passResetToken": null,
            "image": "public/uploads/avatar/avatar.png",
            "provider": null,
            "provider_id": null,
            "point": "0.00",
            "status": "1",
            "created_at": "2022-02-16T11:00:40.000000Z",
            "updated_at": "2022-02-19T03:23:33.000000Z"
        },
        "orderdetails": [
            {
                "orderDetails": "12",
                "orderId": "9",
                "ProductId": "2",
                "productName": "Nestle LACTOGEN 2 Follow up Formula With Iron (6 months ) TIN",
                "productSize": "",
                "productColor": "",
                "productPrice": "630.00",
                "proPurchaseprice": "500",
                "productQuantity": "1",
                "created_at": "2022-02-17T12:20:43.000000Z",
                "updated_at": "2022-02-17T12:20:43.000000Z",
                "image": {
                    "id": 5,
                    "product_id": "2",
                    "image": "public/uploads/product/1644645502-1644066476-0129980_khusboo-premium-olive-pickles-400-gm.jpeg",
                    "created_at": "2022-02-12T05:58:22.000000Z",
                    "updated_at": "2022-02-12T05:58:22.000000Z"
                }
            },
            {
                "orderDetails": "13",
                "orderId": "9",
                "ProductId": "5",
                "productName": "radhuni faluda mix",
                "productSize": "",
                "productColor": "",
                "productPrice": "95.00",
                "proPurchaseprice": "70",
                "productQuantity": "3",
                "created_at": "2022-02-17T12:20:43.000000Z",
                "updated_at": "2022-02-17T12:20:43.000000Z",
                "image": {
                    "id": 9,
                    "product_id": "5",
                    "image": "public/uploads/product/1644835159-pran-hot-tomato-sauce-340gm.jpg",
                    "created_at": "2022-02-14T10:39:19.000000Z",
                    "updated_at": "2022-02-14T10:39:19.000000Z"
                }
            }
        ],
        "payment": {
            "paymentIdPrimary": "9",
            "orderId": "9",
            "paymentType": "cod",
            "senderId": null,
            "transectionId": null,
            "paymentStatus": "pending",
            "bkashFee": null,
            "created_at": "2022-02-17T12:20:43.000000Z",
            "updated_at": "2022-02-17T12:20:43.000000Z"
        },
        "shipping": {
            "shippingPrimariId": "9",
            "customerId": "15",
            "name": "Fsd Ramjan",
            "phone": "01779565300",
            "address": "jksrfjsdf",
            "district": "9",
            "area": "Munshiganj Sadar Upazila",
            "shippingfee": "15",
            "note": "fdfdf",
            "created_at": "2022-02-17T12:20:43.000000Z",
            "updated_at": "2022-02-17T12:20:43.000000Z"
        }
    }
}

I want to show this data in my app.

This is my model class generate from https://javiercbk.github.io

Model class :

class OderDetailsModel {
  OrderInfo? orderInfo;

  OderDetailsModel({this.orderInfo});

  OderDetailsModel.fromJson(Map<String, dynamic> json) {
    orderInfo = json['orderInfo'] != null
        ? new OrderInfo.fromJson(json['orderInfo'])
        : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.orderInfo != null) {
      data['orderInfo'] = this.orderInfo!.toJson();
    }
    return data;
  }
}

class OrderInfo {
  int? orderIdPrimary;
  String? customerId;
  String? shippingId;
  String? orderTotal;
  String? discount;
  String? bkashFee;
  String? trackingId;
  String? orderStatus;
  Null? deliverymanId;
  String? cancelRequest;
  String? point;
  String? pointamount;
  String? createdAt;
  String? updatedAt;
  Ordertype? ordertype;
  Customer? customer;
  List<Orderdetails>? orderdetails;
  Payment? payment;
  Shipping? shipping;

  OrderInfo(
      {this.orderIdPrimary,
      this.customerId,
      this.shippingId,
      this.orderTotal,
      this.discount,
      this.bkashFee,
      this.trackingId,
      this.orderStatus,
      this.deliverymanId,
      this.cancelRequest,
      this.point,
      this.pointamount,
      this.createdAt,
      this.updatedAt,
      this.ordertype,
      this.customer,
      this.orderdetails,
      this.payment,
      this.shipping});

  OrderInfo.fromJson(Map<String, dynamic> json) {
    orderIdPrimary = json['orderIdPrimary'];
    customerId = json['customerId'];
    shippingId = json['shippingId'];
    orderTotal = json['orderTotal'];
    discount = json['discount'];
    bkashFee = json['bkashFee'];
    trackingId = json['trackingId'];
    orderStatus = json['orderStatus'];
    deliverymanId = json['deliveryman_id'];
    cancelRequest = json['cancelRequest'];
    point = json['point'];
    pointamount = json['pointamount'];
    createdAt = json['created_at'];
    updatedAt = json['updated_at'];
    ordertype = json['ordertype'] != null
        ? new Ordertype.fromJson(json['ordertype'])
        : null;
    customer = json['customer'] != null
        ? new Customer.fromJson(json['customer'])
        : null;
    if (json['orderdetails'] != null) {
      orderdetails = <Orderdetails>[];
      json['orderdetails'].forEach((v) {
        orderdetails!.add(new Orderdetails.fromJson(v));
      });
    }
    payment =
        json['payment'] != null ? new Payment.fromJson(json['payment']) : null;
    shipping = json['shipping'] != null
        ? new Shipping.fromJson(json['shipping'])
        : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['orderIdPrimary'] = this.orderIdPrimary;
    data['customerId'] = this.customerId;
    data['shippingId'] = this.shippingId;
    data['orderTotal'] = this.orderTotal;
    data['discount'] = this.discount;
    data['bkashFee'] = this.bkashFee;
    data['trackingId'] = this.trackingId;
    data['orderStatus'] = this.orderStatus;
    data['deliveryman_id'] = this.deliverymanId;
    data['cancelRequest'] = this.cancelRequest;
    data['point'] = this.point;
    data['pointamount'] = this.pointamount;
    data['created_at'] = this.createdAt;
    data['updated_at'] = this.updatedAt;
    if (this.ordertype != null) {
      data['ordertype'] = this.ordertype!.toJson();
    }
    if (this.customer != null) {
      data['customer'] = this.customer!.toJson();
    }
    if (this.orderdetails != null) {
      data['orderdetails'] = this.orderdetails!.map((v) => v.toJson()).toList();
    }
    if (this.payment != null) {
      data['payment'] = this.payment!.toJson();
    }
    if (this.shipping != null) {
      data['shipping'] = this.shipping!.toJson();
    }
    return data;
  }
}

class Ordertype {
  int? id;
  String? name;
  String? slug;
  Null? createdAt;
  Null? updatedAt;

  Ordertype({this.id, this.name, this.slug, this.createdAt, this.updatedAt});

  Ordertype.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['name'];
    slug = json['slug'];
    createdAt = json['created_at'];
    updatedAt = json['updated_at'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['name'] = this.name;
    data['slug'] = this.slug;
    data['created_at'] = this.createdAt;
    data['updated_at'] = this.updatedAt;
    return data;
  }
}

class Customer {
  int? id;
  String? fullName;
  String? phoneNumber;
  Null? address;
  Null? email;
  String? verifyToken;
  Null? passResetToken;
  String? image;
  Null? provider;
  Null? providerId;
  String? point;
  String? status;
  String? createdAt;
  String? updatedAt;

  Customer(
      {this.id,
      this.fullName,
      this.phoneNumber,
      this.address,
      this.email,
      this.verifyToken,
      this.passResetToken,
      this.image,
      this.provider,
      this.providerId,
      this.point,
      this.status,
      this.createdAt,
      this.updatedAt});

  Customer.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    fullName = json['fullName'];
    phoneNumber = json['phoneNumber'];
    address = json['address'];
    email = json['email'];
    verifyToken = json['verifyToken'];
    passResetToken = json['passResetToken'];
    image = json['image'];
    provider = json['provider'];
    providerId = json['provider_id'];
    point = json['point'];
    status = json['status'];
    createdAt = json['created_at'];
    updatedAt = json['updated_at'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['fullName'] = this.fullName;
    data['phoneNumber'] = this.phoneNumber;
    data['address'] = this.address;
    data['email'] = this.email;
    data['verifyToken'] = this.verifyToken;
    data['passResetToken'] = this.passResetToken;
    data['image'] = this.image;
    data['provider'] = this.provider;
    data['provider_id'] = this.providerId;
    data['point'] = this.point;
    data['status'] = this.status;
    data['created_at'] = this.createdAt;
    data['updated_at'] = this.updatedAt;
    return data;
  }
}

class Orderdetails {
  String? orderDetails;
  String? orderId;
  String? productId;
  String? productName;
  String? productSize;
  String? productColor;
  String? productPrice;
  String? proPurchaseprice;
  String? productQuantity;
  String? createdAt;
  String? updatedAt;
  Image? image;

  Orderdetails(
      {this.orderDetails,
      this.orderId,
      this.productId,
      this.productName,
      this.productSize,
      this.productColor,
      this.productPrice,
      this.proPurchaseprice,
      this.productQuantity,
      this.createdAt,
      this.updatedAt,
      this.image});

  Orderdetails.fromJson(Map<String, dynamic> json) {
    orderDetails = json['orderDetails'];
    orderId = json['orderId'];
    productId = json['ProductId'];
    productName = json['productName'];
    productSize = json['productSize'];
    productColor = json['productColor'];
    productPrice = json['productPrice'];
    proPurchaseprice = json['proPurchaseprice'];
    productQuantity = json['productQuantity'];
    createdAt = json['created_at'];
    updatedAt = json['updated_at'];
    image = json['image'] != null ? new Image.fromJson(json['image']) : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['orderDetails'] = this.orderDetails;
    data['orderId'] = this.orderId;
    data['ProductId'] = this.productId;
    data['productName'] = this.productName;
    data['productSize'] = this.productSize;
    data['productColor'] = this.productColor;
    data['productPrice'] = this.productPrice;
    data['proPurchaseprice'] = this.proPurchaseprice;
    data['productQuantity'] = this.productQuantity;
    data['created_at'] = this.createdAt;
    data['updated_at'] = this.updatedAt;
    if (this.image != null) {
      data['image'] = this.image!.toJson();
    }
    return data;
  }
}

class Image {
  int? id;
  String? productId;
  String? image;
  String? createdAt;
  String? updatedAt;

  Image({this.id, this.productId, this.image, this.createdAt, this.updatedAt});

  Image.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    productId = json['product_id'];
    image = json['image'];
    createdAt = json['created_at'];
    updatedAt = json['updated_at'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['product_id'] = this.productId;
    data['image'] = this.image;
    data['created_at'] = this.createdAt;
    data['updated_at'] = this.updatedAt;
    return data;
  }
}

class Payment {
  String? paymentIdPrimary;
  String? orderId;
  String? paymentType;
  Null? senderId;
  Null? transectionId;
  String? paymentStatus;
  Null? bkashFee;
  String? createdAt;
  String? updatedAt;

  Payment(
      {this.paymentIdPrimary,
      this.orderId,
      this.paymentType,
      this.senderId,
      this.transectionId,
      this.paymentStatus,
      this.bkashFee,
      this.createdAt,
      this.updatedAt});

  Payment.fromJson(Map<String, dynamic> json) {
    paymentIdPrimary = json['paymentIdPrimary'];
    orderId = json['orderId'];
    paymentType = json['paymentType'];
    senderId = json['senderId'];
    transectionId = json['transectionId'];
    paymentStatus = json['paymentStatus'];
    bkashFee = json['bkashFee'];
    createdAt = json['created_at'];
    updatedAt = json['updated_at'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['paymentIdPrimary'] = this.paymentIdPrimary;
    data['orderId'] = this.orderId;
    data['paymentType'] = this.paymentType;
    data['senderId'] = this.senderId;
    data['transectionId'] = this.transectionId;
    data['paymentStatus'] = this.paymentStatus;
    data['bkashFee'] = this.bkashFee;
    data['created_at'] = this.createdAt;
    data['updated_at'] = this.updatedAt;
    return data;
  }
}

class Shipping {
  String? shippingPrimariId;
  String? customerId;
  String? name;
  String? phone;
  String? address;
  String? district;
  String? area;
  String? shippingfee;
  String? note;
  String? createdAt;
  String? updatedAt;

  Shipping(
      {this.shippingPrimariId,
      this.customerId,
      this.name,
      this.phone,
      this.address,
      this.district,
      this.area,
      this.shippingfee,
      this.note,
      this.createdAt,
      this.updatedAt});

  Shipping.fromJson(Map<String, dynamic> json) {
    shippingPrimariId = json['shippingPrimariId'];
    customerId = json['customerId'];
    name = json['name'];
    phone = json['phone'];
    address = json['address'];
    district = json['district'];
    area = json['area'];
    shippingfee = json['shippingfee'];
    note = json['note'];
    createdAt = json['created_at'];
    updatedAt = json['updated_at'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['shippingPrimariId'] = this.shippingPrimariId;
    data['customerId'] = this.customerId;
    data['name'] = this.name;
    data['phone'] = this.phone;
    data['address'] = this.address;
    data['district'] = this.district;
    data['area'] = this.area;
    data['shippingfee'] = this.shippingfee;
    data['note'] = this.note;
    data['created_at'] = this.createdAt;
    data['updated_at'] = this.updatedAt;
    return data;
  }
}

CodePudding user response:

You don't need to cast it

final List<ProductOrderModel> orderDetailsData = res.data
            .map<ProductOrderModel>(ProductOrderModel.fromJson)
            .toList();

CodePudding user response:

Try this I am using this ===>

Api Service Class :

class ApiService {
  Future<OrderInfo> fetchApi(id) async {
    var url = baseUrl   'customer/order/invoice/$id';
    final sharedPreferences = await SharedPreferences.getInstance();
    var token = sharedPreferences.getString('accessToken');
    var response = await http.get(Uri.parse(url), headers: {
      'Authorization': 'Bearer $token',
    });

    if (response.statusCode == 200) {
      var dataResponse = jsonDecode(response.body)['orderInfo'];

      OrderInfo user = OrderInfo.fromJson(dataResponse);

      return user;
    } else {
      throw Exception('Failed Get API');
    }
  }
}

User this function:

Future<OrderInfo> fetchApi(id) async {
    var _orderlist = await ApiService().fetchApi(id);
    print(productOrderDetailsList);

    return productOrderDetailsList.value = _orderlist;

    // try {

    // } catch (e) {
    //   print(e);
    // }
  }
  • Related