Flutter:为什么 json_annotation 包没有生成 *.g.dart 文件

Pok*_*oom 6 dart flutter

我正在尝试使用 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
Run Code Online (Sandbox Code Playgroud)

我的 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);
}
Run Code Online (Sandbox Code Playgroud)

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

flutter pub run build_runner 构建

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

Pok*_*oom 3

因此,如果您想使用json_annotation包,那么您还需要json_serializable包,所以我只需要添加该包即可工作。