中文乱码

来源:9-7 将一个中等复杂度的json转成model

慕斯8050152

2021-11-04

老师,9-7 将一个中等复杂度的json转成model 节,将本地的json字符串转model,出现中文乱码问题。而且debug运行到(Map<String, dynamic> map = json.decode(utf8decoder.convert(jsonStr.codeUnits));)崩溃了

ElevatedButton(
              onPressed: (){
                String jsonStr = '{"code": 0,"data": {"total": 6,"list": [{"id": "9","sticky": 1,"type": "recommend","title": "好课推荐","subtitle": "移动端普通工程师到架构师的全方位蜕变","url": "http://class.imooc.com/sale/mobilearchitect","cover": "https://img.mukewang.com/5f057a6a0001f4f918720764.jpg","createTime": "2020-12-03 16:39:24"}]},"msg": "SUCCESS."}';
                Utf8Decoder utf8decoder = Utf8Decoder();
                Map<String, dynamic> map = json.decode(utf8decoder.convert(jsonStr.codeUnits));
                CommonModel model = CommonModel.fromJson(map);
                print(model.toString());
              },
              child: Text('json解析', style: TextStyle(fontSize: 20),),
class CommonModel{
  final int? code;
  final CommonDataModel? data;
  final String? msg;

  CommonModel({this.code, this.data, this.msg});

  factory CommonModel.fromJson(Map<String, dynamic> json) {
    CommonDataModel data = CommonDataModel.fromJson(json['data']);
    return CommonModel(
      code: json['code'],
      data: data,
      msg: json['msg'],
    );
  }
}

class CommonDataModel{
  final int? total;
  final List<CommonDataListModel>? list;

  CommonDataModel({this.total, this.list});

  factory CommonDataModel.fromJson(Map<String, dynamic> json) {
    int total = json['total'];
    List<CommonDataListModel> list = (json['list'] as List).map((e) => CommonDataListModel.fromJson(e)).toList();
    return CommonDataModel(
      total: total,
      list: list,
    );
  }
}

class CommonDataListModel{
  final String? id;
  final int? sticky;
  final String? type;
  final String? title;
  final String? subtitle;
  final String? url;
  final String? cover;
  final String? createTime;

  CommonDataListModel({this.id, this.sticky, this.type, this.title, this.subtitle, this.url, this.cover, this.createTime});

  factory CommonDataListModel.fromJson(Map<String, dynamic> json) {
    return CommonDataListModel(
      id: json['id'],
      sticky: json['sticky'],
      type: json['type'],
      title: json['title'],
      subtitle: json['subtitle'],
      url: json['url'],
      cover: json['cover'],
      createTime: json['createTime'],
    );
  }
}
写回答

1回答

CrazyCodeBoy

2021-11-05

对照这块课程源码检查下你的代码实现看是否有出入的地方呢

0
1
慕斯8050152
老师,我的问题不是课程里面讲的从网络请求获得数据转model,是以下这种方式转model的时候崩溃了 String jsonStr = '{"code": 0,"data": {"total": 6,"list": [{"id": "9","sticky": 1,"type": "recommend","title": "好课推荐","subtitle": "移动端普通工程师到架构师的全方位蜕变","url": "http://class.imooc.com/sale/mobilearchitect","cover": "https://img.mukewang.com/5f057a6a0001f4f918720764.jpg","createTime": "2020-12-03 16:39:24"}]},"msg": "SUCCESS."}'; Utf8Decoder utf8decoder = Utf8Decoder(); Map map = json.decode(utf8decoder.convert(jsonStr.codeUnits)); CommonModel model = CommonModel.fromJson(map);
2021-11-05
共1条回复

Flutter从入门到进阶 实战携程网App 一网打尽核心技术

解锁Flutter开发新姿势,,系统掌握Flutter开发核心技术。

4788 学习 · 3270 问题

查看课程