在flutter中使用built_value创建不可变模型时出错

时间:2018-10-01 07:39:07

标签: json dart flutter

我遇到以下错误:

  

发生异常。反序列化'[{{id:22,date_created:   2018-06-05T06:58:43,date_created_gmt:2018-06-05T06:...'至   “客户”失败的原因是:类型“ _InternalLinkedHashMap”在类型转换中不是“字符串”类型的子类型

如何为以下JSON响应创建built_value模型。

[
    {
        "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": "prasangaahal42@gmail.com",
        "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": "prasangadahal42@gmail.com",
            "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"
                }
            ]
        }
    }
]

我为Customer模型编写了以下dart代码。

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;
}

1 个答案:

答案 0 :(得分:1)

您需要将字符串值转换为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;
  }
}