Sagar Bahadur Tamang
Sagar Bahadur Tamang

Reputation: 2709

Error while creating immutable model using built_value in flutter

I am getting the following error :

Exception has occurred. Deserializing '[{id: 22, date_created: 2018-06-05T06:58:43, date_created_gmt: 2018-06-05T06:...' to 'Customer' failed due to: type '_InternalLinkedHashMap' is not a subtype of type 'String' in type cast

How to create the built_value model for the following JSON response.

[
    {
        "id": 22,
        "date_created": "2018-06-05T06:58:43",
        "date_created_gmt": "2018-06-05T06:58:43",
        "date_modified": "2018-06-22T13:18:45",
        "date_modified_gmt": "2018-06-22T13:18:45",
        "email": "[email protected]",
        "first_name": "Prasanga",
        "last_name": "Dahal",
        "role": "customer",
        "username": "_prasanga",
        "billing": {
            "first_name": "Prasanga",
            "last_name": "Dahal",
            "company": "",
            "address_1": "Binayak Marg, Biratnagar 56613, Nepal",
            "address_2": "",
            "city": "Biratnagar",
            "state": "KOS",
            "postcode": "",
            "country": "NP",
            "email": "[email protected]",
            "phone": "9862316136"
        },
        "shipping": {
            "first_name": "Prsanga",
            "last_name": "Dahal",
            "company": "",
            "address_1": "Binyak Marg, Biratnagar 5613, Nepal",
            "address_2": "",
            "city": "Biratnagar",
            "state": "KOS",
            "postcode": "",
            "country": "NP"
        },
        "is_paying_customer": true,
        "orders_count": 2,
        "total_spent": "920.00",
        "avatar_url": "https://secure.gravatar.com/avatar/1a733bfdb72af0ee8f64b15435dd84a5?s=96&d=retro&r=g",
        "meta_data": [
            {
                "id": 475,
                "key": "wc_last_active",
                "value": "1534982400"
            },
            {
                "id": 2888,
                "key": "shipping_method",
                "value": [
                    "flat_rate:2"
                ]
            }
        ],
        "_links": {
            "self": [
                {
                    "href": "https://mamham.com/wp-json/wc/v2/customers/22"
                }
            ],
            "collection": [
                {
                    "href": "https://mamham.com/wp-json/wc/v2/customers"
                }
            ]
        }
    }
]

I have wrote following dart code for the Customer model.

import 'package:built_collection/built_collection.dart';

import 'package:built_value/built_value.dart';

import 'package:built_value/serializer.dart';

part 'Customer.g.dart';

abstract class Customer implements Built <Customer, CustomerBuilder> {

  int get id;
  DateTime get date_created;
  DateTime get date_created_gmt;
  DateTime get date_modified;
  DateTime get date_modified_gmt;
  String get email;
  String get first_name;
  String get last_name;
  String get role;
  String get username;

  Billing get billing;

  Shipping get shipping;

  Customer._();

  static Serializer<Customer> get serializer => _$customerSerializer;

  factory Customer([updates(CustomerBuilder b)]) = _$Customer;

}

abstract class Billing implements Built <Billing, BillingBuilder> {

  String get first_name;
  String get last_name;
  String get company;
  String get address_1;
  String get address_2;
  String get city;
  String get state;
  String get postcode;
  String get country;
  String get email;
  String get phone;

  Billing._();

  static Serializer<Billing> get serializer => _$billingSerializer;

  factory Billing([updates(BillingBuilder b)]) = _$Billing;

}

abstract class Shipping implements Built <Shipping, ShippingBuilder> {

  String get first_name;
  String get last_name;
  String get company;
  String get address_1;
  String get address_2;
  String get city;
  String get state;
  String get postcode;
  String get country;
  String get email;
  String get phone;

  Shipping._();

  static Serializer<Shipping> get serializer => _$shippingSerializer;

  factory Shipping([updates(ShippingBuilder b)]) = _$Shipping;
}

Upvotes: 0

Views: 349

Answers (1)

anmol.majhail
anmol.majhail

Reputation: 51286

You need to Convert the String value To DateTime:

"date_created": "2018-06-05T06:58:43"

DateTime convertToDate(String input) {
  try {
    var d = DateFormat.yMMMd().parseStrict(input);
    return d;
  } catch (e) {
    return null;
  }
}

Upvotes: 1

Related Questions