美文网首页
flutter 使用json_serializable自动生成m

flutter 使用json_serializable自动生成m

作者: Liu_QT | 来源:发表于2022-11-14 10:36 被阅读0次

    使用json_serializable第三方库自动生成相应的实体类

    1. 添加依赖
    dev_dependencies:
      flutter_test:
        sdk: flutter
     
      build_runner: ^2.0.4
      json_serializable: ^4.1.3
    
    1. 手动创建model类并写入相关属性
    
    part 'brand_list_model.g.dart'; //这里报错没关系
    
    @JsonSerializable()
    class BrandListModel  {
      static const int pageSize = 10;
    
      int pageNo = 0;
      int totalCount = 0;
    
      @JsonKey(name: "data")
      List<BrandItemModel>? brandListModel = [];
    
      BrandListModel({
        this.pageNo = 0,
        this.totalCount = 0,
        this.brandListModel
      });
    }
    
    1. 在对应的目录下输入命令
    flutter packages pub run build_runner build
    

    注意:遇到冲突Conflicting outputs were detected and the build is unable to prompt for permission to需要再输入两个命令

    flutter packages pub run build_runner clean
    flutter packages pub run build_runner build --delete-conflicting-outputs
    

    相关文章

      网友评论

          本文标题:flutter 使用json_serializable自动生成m

          本文链接:https://www.haomeiwen.com/subject/cwhdxdtx.html