Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json序列化没有生成抽象类了 #88

Closed
silent-tan opened this issue Oct 12, 2018 · 3 comments
Closed

json序列化没有生成抽象类了 #88

silent-tan opened this issue Oct 12, 2018 · 3 comments

Comments

@silent-tan
Copy link

大大好,flutter白,跟着学习遇到了一个问题,如标题。不知道是不是因为依赖的问题还是有另外的配置啊

import 'package:json_annotation/json_annotation.dart';

///关联文件、允许Template访问 Template.g.dart 中的私有方法
///Template.g.dart 是通过命令生成的文件。名称为 xx.g.dart,其中 xx 为当前 dart 文件名称
///Template.g.dart中创建了抽象类_$TemplateSerializerMixin,实现了_$TemplateFromJson方法
part 'Template.g.dart';

///标志class需要实现json序列化功能
@JsonSerializable()

///'xx.g.dart'文件中,默认会根据当前类名如 AA 生成 _$AASerializerMixin
///所以当前类名为Template,生成的抽象类为 _$TemplateSerializerMixin
class Template extends Object with _$TemplateSerializerMixin {

  String name;

  int id;

  ///通过JsonKey重新定义参数名
  @JsonKey(name: "push_id")
  int pushId;

  Template(this.name, this.id, this.pushId);

  ///'xx.g.dart'文件中,默认会根据当前类名如 AA 生成 _$AAeFromJson方法
  ///所以当前类名为Template,生成的抽象类为 _$TemplateFromJson
  factory Template.fromJson(Map<String, dynamic> json) => _$TemplateFromJson(json);
}
// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'Template.dart';

// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************

Template _$TemplateFromJson(Map<String, dynamic> json) {
  return Template(
      json['name'] as String, json['id'] as int, json['push_id'] as int);
}

Map<String, dynamic> _$TemplateToJson(Template instance) => <String, dynamic>{
      'name': instance.name,
      'id': instance.id,
      'push_id': instance.pushId
    };

依赖

dependencies:
  flutter:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.2
  json_annotation: ^1.2.0

dev_dependencies:
  flutter_test:
    sdk: flutter
  build_runner: ^1.0.0
  json_serializable: ^1.5.1
@CarGuo
Copy link
Owner

CarGuo commented Oct 14, 2018

执行过 **flutter packages pub run build_runner build ** ??

@silent-tan
Copy link
Author

嗯嗯,是的。用的是新版本的包....

@CarGuo
Copy link
Owner

CarGuo commented Oct 15, 2018

首先,如果序列化的类已经有了,你需要执行的是

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

之后新版的格式有点变了,如下

import 'package:json_annotation/json_annotation.dart';


part 'Template.g.dart';

@JsonSerializable()
class Template {

  String name;

  int id;


  @JsonKey(name: "push_id")
  int pushId;

  Template(this.name, this.id, this.pushId);


  factory Template.fromJson(Map<String, dynamic> json) => _$TemplateFromJson(json);
  Map<String, dynamic> toJson() => _$TemplateToJson(this);
}

@CarGuo CarGuo closed this as completed Oct 15, 2018
@CarGuo CarGuo mentioned this issue Nov 21, 2019
@mr-shitou mr-shitou mentioned this issue Nov 30, 2019
@theecn theecn mentioned this issue Dec 18, 2019
@byue666 byue666 mentioned this issue Dec 19, 2019
@dfaofeng dfaofeng mentioned this issue Dec 24, 2019
@alienzh alienzh mentioned this issue Dec 27, 2019
@YaoHooHoo YaoHooHoo mentioned this issue Jan 15, 2020
@w497763094 w497763094 mentioned this issue Mar 10, 2020
@JevonShi JevonShi mentioned this issue Mar 10, 2020
@HaobinXu HaobinXu mentioned this issue Mar 20, 2020
@luan823230 luan823230 mentioned this issue Mar 24, 2020
@Andy957 Andy957 mentioned this issue Apr 16, 2020
@ngu2008 ngu2008 mentioned this issue Apr 17, 2020
@hzg1105 hzg1105 mentioned this issue May 3, 2020
@MrHGJ MrHGJ mentioned this issue May 19, 2020
@hzg1105 hzg1105 mentioned this issue Jun 14, 2020
@wangkr wangkr mentioned this issue Jul 4, 2020
@gss3000 gss3000 mentioned this issue Jul 8, 2020
@zaeMyn zaeMyn mentioned this issue Aug 5, 2020
@Liway Liway mentioned this issue Jun 22, 2022
@jayVRIY jayVRIY mentioned this issue Dec 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants