Flutter json_serialization not generating *.g.dart files

json_serialization is a library for to generate to/from JSON code for a class, annotate it with JsonSerializable. A class annotated with JsonSerializable should generate a .g.dart file. If it’s not generating the .g.dart file, check the following and make sure the json_serialization installation was is done properly.

1. Make sure the json_serializable library is installed, if not run this command from the root folder to install it.

flutter pub add json_serializable

2. Make sure the build_runner library is installed as a dev dependency, if not, run this command from the root folder to install it.

flutter pub add -d build_runner

3. Make sure the data model class has the following imports, assuming the file name for the data model class is person.dart

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

4. Here is an example for a data model class for a Person. Make sure it is annotated with @JsonSerializable(); it has a default constructor; it has a factory method fromJson; it has a factory method toJson; the stub method call should start with underscore and dollar sign such as _$PersonFromJson(json) and _$PersonToJson(this) in this example.

import 'package:json_annotation/json_annotation.dart';

part 'person.g.dart';

@JsonSerializable()
class Person {
  /// The generated code assumes these values exist in JSON.
  final String firstName, lastName;

  /// The generated code below handles if the corresponding JSON value doesn't
  /// exist or is empty.
  final DateTime? dateOfBirth;

  Person({required this.firstName, required this.lastName, this.dateOfBirth});

  /// Connect the generated [_$PersonFromJson] function to the `fromJson`
  /// factory.
  factory Person.fromJson(Map json) => _$PersonFromJson(json);

  /// Connect the generated [_$PersonToJson] function to the `toJson` method.
  Map toJson() => _$PersonToJson(this);
}

5. Make sure to run the build runner to build the generated g.dart file. Run the following command from the project root directory.

flutter pub run build_runner build

6. The generated file should show up in the same directory where the data class model is. In this case, a new file person.g.dart should be generated because the file name for the data model class is person.dart. The content in the generated file should look like this.

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'person.dart';

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

Person _$PersonFromJson(Map json) => Person(
      firstName: json['firstName'] as String,
      lastName: json['lastName'] as String,
      dateOfBirth: json['dateOfBirth'] == null
          ? null
          : DateTime.parse(json['dateOfBirth'] as String),
    );

Map _$PersonToJson(Person instance) => {
      'firstName': instance.firstName,
      'lastName': instance.lastName,
      'dateOfBirth': instance.dateOfBirth?.toIso8601String(),
    };

Search within Codexpedia

Custom Search

Search the entire web

Custom Search