Flutter 序列化支持泛型的坑

请求接口得到的数据

{
    "servegeneralist": [
        {
            "fwdlname": "健康服务",
            "list": [
                {
                    "fwxlid": "2c908084690da43b01690ed9e3b30004",
                    "fwxlname": "韩式SPA水疗",
                    "price": 100.0
                },
                {
                    "fwxlid": "402881d86914312b0169147271a10029",
                    "fwxlname": "泰式全身按摩",
                    "price": 300.0
                },
                {
                    "fwxlid": "402881d86931b22b0169331c4b790046",
                    "fwxlname": "医疗护理",
                    "price": 10.0
                },
                {
                    "fwxlid": "402881d86931b22b0169331dad710047",
                    "fwxlname": "康复保健",
                    "price": 10.0
                },
                {
                    "fwxlid": "402881d86931b22b0169331ea2970048",
                    "fwxlname": "健身养生",
                    "price": 10.0
                }
            ]
        },
        {
            "fwdlname": "家政服务",
            "list": [
                {
                    "fwxlid": "402881d86914312b01691470111b0026",
                    "fwxlname": "清洗衣物",
                    "price": 100.0
                },
                {
                    "fwxlid": "402881d86914312b0169147076f90027",
                    "fwxlname": "打扫卫生",
                    "price": 200.0
                },
                {
                    "fwxlid": "402881d86931b22b01693322a6bf0049",
                    "fwxlname": "职业保姆",
                    "price": 10.0
                },
                {
                    "fwxlid": "402881d86931b22b01693323cdc8004e",
                    "fwxlname": "育婴早教",
                    "price": 10.0
                },
                {
                    "fwxlid": "402881d86931b22b016933288a9c0055",
                    "fwxlname": "玻璃清洗",
                    "price": 10.0
                },
                {
                    "fwxlid": "402881d86931b22b01693328dc7a0056",
                    "fwxlname": "地毯清洗",
                    "price": 10.0
                }
            ]
        }
    ],
    "serverPackageList": [
        {
            "list": [
                {
                    "intid": "40289fbe692268e7016922849000000b",
                    "serveritemname": "韩式SPA水疗,泰式全身按"
                }
            ],
            "serverPackageAmount": 120.0,
            "serverpackagename": "养生套餐A"
        },
        {
            "list": [
                {
                    "intid": "40289fbe692268e701692285e71a000c",
                    "serveritemname": "清洗衣物,打扫卫生"
                }
            ],
            "serverPackageAmount": 50.0,
            "serverpackagename": "家居卫生"
        },
        {
            "list": [
                {
                    "intid": "40289fbe6922a47d016922cfa420004c",
                    "serveritemname": "韩式SPA水疗,泰式全身按摩,清洗衣物,打扫卫生"
                }
            ],
            "serverPackageAmount": 510.0,
            "serverpackagename": "套餐1"
        }
    ]
}

书写darBean

先看官网的序列化列子
flutter序列化官网
pubspec.yaml 添加依赖

dependencies:
  # Your other regular dependencies here
  json_annotation: ^2.0.0

dev_dependencies:
  # Your other dev_dependencies here
  build_runner: ^1.0.0
  json_serializable: ^2.0.0

模仿列子写 darBean

import 'package:json_annotation/json_annotation.dart';
part 'ServerBusiness.g.dart';
//服务项目 (商家bean)
//知道通用否
@JsonSerializable()
class ServerSusiness{
  List servegeneralist;
  List serverPackageList;
  ServerSusiness(this.servegeneralist,this.serverPackageList);
//  factory ServerSusiness.fromJson(Map json)=>_$ServerSusiness.fromJson(json);
  factory ServerSusiness.fromJson(Map json)=>_$ServerSusinessFromJson(json);
  Map toJson()=>_$ServerSusinessToJson(this);
}
@JsonSerializable()
class ServerPageBean {
  double serverPackageAmount;
  String serverpackagename;
  List list;
  ServerPageBean(this.serverPackageAmount,this.serverpackagename,this.list);
//  factory ServerPageBean.fromJson(Map json)=>_ServerPageBeanFromJson(json);
  factory ServerPageBean.fromJson(Map json)=>_$ServerPageBeanFromJson(json);
  Map toJson()=>_$ServerPageBeanToJson(this);
}
@JsonSerializable()
class SerpageBean_bean {
  String intid;
  String serveritemname ;
  SerpageBean_bean(this.intid,this.serveritemname);
  factory SerpageBean_bean.fromJson(Map json)=>_$SerpageBean_beanFromJson(json);
  Map toJson()=>_$SerpageBean_beanToJson(this);
}
@JsonSerializable()
class  ServerGenBean {
  String fwdlname;
   Listlist;
  ServerGenBean(this.fwdlname,this.list);
  factory ServerGenBean.fromJson(Map json)=>_$ServerGenBeanFromJson(json);
  Map toJson()=>_$ServerGenBeanToJson(this);

}
@JsonSerializable()
class Servergean_bean_bean{
  String fwxlid;
  String fwxlname;
  double price; //拿到数据是什么类型这里就定义什么样的类型
  Servergean_bean_bean(this.fwxlid,this.fwxlname,this.price);
  factory Servergean_bean_bean.fromJson(Map json)=>_$Servergean_bean_beanFromJson(json);
  Map toJson()=>_$Servergean_bean_beanToJson(this);

}

注意事项

上面还是要模仿 官网的列子
1、导包

import 'package:json_annotation/json_annotation.dart';
part 'ServerBusiness.g.dart'; //这个是执行命令生成的,我们要写上

2、我们格式化一下 我们的json字符串看一下结构
Flutter 序列化支持泛型的坑_第1张图片
Flutter 序列化支持泛型的坑_第2张图片
Flutter 序列化支持泛型的坑_第3张图片

主Bean

class ServerSusiness{
  List servegeneralist;
  List serverPackageList;
  ServerSusiness(this.servegeneralist,this.serverPackageList);
//  factory ServerSusiness.fromJson(Map json)=>_$ServerSusiness.fromJson(json);
  factory ServerSusiness.fromJson(Map json)=>_$ServerSusinessFromJson(json);
  Map toJson()=>_$ServerSusinessToJson(this);
}

两个次Bean

@JsonSerializable()
class  ServerGenBean {
  String fwdlname;
   Listlist;
  ServerGenBean(this.fwdlname,this.list);
  factory ServerGenBean.fromJson(Map json)=>_$ServerGenBeanFromJson(json);
  Map toJson()=>_$ServerGenBeanToJson(this);

}
@JsonSerializable()
class ServerPageBean {
  double serverPackageAmount;
  String serverpackagename;
  List list;
  ServerPageBean(this.serverPackageAmount,this.serverpackagename,this.list);
//  factory ServerPageBean.fromJson(Map json)=>_ServerPageBeanFromJson(json);
  factory ServerPageBean.fromJson(Map json)=>_$ServerPageBeanFromJson(json);
  Map toJson()=>_$ServerPageBeanToJson(this);
}

次次Bean 两个

@JsonSerializable()
class Servergean_bean_bean{
  String fwxlid;
  String fwxlname;
  double price; //拿到数据是什么类型这里就定义什么样的类型
  Servergean_bean_bean(this.fwxlid,this.fwxlname,this.price);
  factory Servergean_bean_bean.fromJson(Map json)=>_$Servergean_bean_beanFromJson(json);
  Map toJson()=>_$Servergean_bean_beanToJson(this);

}
@JsonSerializable()
class SerpageBean_bean {
  String intid;
  String serveritemname ;
  SerpageBean_bean(this.intid,this.serveritemname);
  factory SerpageBean_bean.fromJson(Map json)=>_$SerpageBean_beanFromJson(json);
  Map toJson()=>_$SerpageBean_beanToJson(this);
}

书写完成后执行命令

flutter packages pub run build_runner build

这个时候会生成一个类名.g.dart的文件
Flutter 序列化支持泛型的坑_第4张图片

最的要注意的时候

一般生成后不会报错,但如果我们的bean如果报错话 则需要修改一下 一般是这个两个地方会报错
Flutter 序列化支持泛型的坑_第5张图片
会部没有报错后如何使用呢 ?

在代码里使用

Flutter 序列化支持泛型的坑_第6张图片
打断点看映射到bean上的数据
Flutter 序列化支持泛型的坑_第7张图片
至此我们就可以像 android开发一样 直接类名. 调用了 但唯一是没有set方法 因为有的需要我们需要填充假数据 去请求接口的,但这样的bean确实没有办法办到啊。

你可能感兴趣的:(Flutter)