Flutter: why json_annotation package is not generating *.g.dart file

Issue

I’m trying to use json_annotation package to serialise the json object but it is not generating the *.g.dart file

my pubspec.yaml file

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

my news.dart file

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);
}

I’m using this command to generate the file,

flutter pub run build_runner build

I did same in my another project and it was working fine but it here it is not working

Solution

So if you want to use json_annotation package then you also need json_serializable package so I just needed to add that package then it will work.

Answered By – Pokaboom

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published