【问题标题】:Flutter: why json_annotation package is not generating *.g.dart fileFlutter:为什么 json_annotation 包没有生成 *.g.dart 文件
【发布时间】:2021-05-20 04:14:48
【问题描述】:

我正在尝试使用 json_annotation 包来序列化 json 对象,但它没有生成 *.g.dart 文件

我的 pubspec.yaml 文件

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^1.0.0
  lottie: ^0.7.0+1
  font_awesome_flutter: ^8.11.0
  effective_dart: ^1.3.0
  provider: ^4.3.3
  http: ^0.12.2
  dio: ^3.0.10
  connectivity: ^2.0.2
  retrofit: ^1.3.4+1
  json_annotation: ^3.1.1


dev_dependencies:
  flutter_test:
    sdk: flutter
  retrofit_generator: ^1.4.1+2
  build_runner: ^1.11.1

我的 news.dart 文件

import 'package:json_annotation/json_annotation.dart';
part 'news.g.dart';

@JsonSerializable()
class News {
  Source source;
  String author;
  String title;
  String description;
  String url;
  String urlToImage;
  String publishedAt;
  String content;

  News(
      {this.source,
      this.author,
      this.title,
      this.description,
      this.url,
      this.urlToImage,
      this.publishedAt,
      this.content});
  factory News.fromJson(Map<String, dynamic> json) => _$NewsFromJson(json);
}

@JsonSerializable()
class Source {
  String id;
  String name;
  Source({this.id, this.name});

  factory Source.fromJson(Map<String, dynamic> source) =>
      _$SourceFromJson(source);
}

我正在使用这个命令来生成文件,

flutter pub run build_runner build

我在另一个项目中做了同样的事情,它工作正常,但在这里它不工作

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    所以如果你想使用 json_annotation 包,那么你还需要 json_serializable 包,所以我只需要添加那个包就可以了。

    【讨论】:

      猜你喜欢
      • 2021-09-07
      • 2020-02-10
      • 2021-11-11
      • 2021-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-29
      相关资源
      最近更新 更多