6

I'm using a private constructor in my class, but the code generation fails with

The class Foo has no default constructor.

I'm using latest json_serializable: version i.e. 6.1.5:

@JsonSerializable()
class Foo {
  final int count;
  Foo._(this.count);

  factory Foo.fromJson(Map<String, dynamic> json) => _$Foo._FromJson(json);
}

What am I doing wrong?

iDecode
  • 22,623
  • 19
  • 99
  • 186

2 Answers2

6

You can use @JsonSerializable(constructor: '_') which is introduced in the 4.2.0-dev of the JsonSerializable.

This will allow you to field to specify an alternative constructor to invoke when creating a fromJson helper.

For example:

import 'package:json_annotation/json_annotation.dart';

part 'foo.g.dart';

@JsonSerializable(constructor: '_')
class Foo {
  final int count;
  Foo._(this.count);

  factory Foo.fromJson(Map<String, dynamic> json) => _$FooFromJson(json);
}

Now here, instead of using fromJson like this _$Foo._FromJson(json), use it as _$FooFromJson(json)

ibhavikmakwana
  • 8,948
  • 4
  • 20
  • 39
0

if you need to json_serializable: generate is for you, You have to define the default constructor. but you can mack this trick:

first do this and run build_runner:

@JsonSerializable()
class Foo {
  final int count;
  Foo(this.count);

  factory Foo.fromJson(Map<String, dynamic> json) => _$FooFromJson(json);
}

then change it to this:

@JsonSerializable()
class Foo {
  final int count;
  Foo._(this.count);

  factory Foo.fromJson(Map<String, dynamic> json) => _$FooFromJson(json);
}

and go to _.g.dart and mack call the Private constructor:

Foo _$FooFromJson(Map<String, dynamic> json) => Foo._(
      json['count'] as int,
    );

Map<String, dynamic> _$FooToJson(Foo instance) => <String, dynamic>{
      'count': instance.count,
    };
samy cripten
  • 191
  • 1
  • 1
  • 11
  • Generated files should not be modified, as the next time build runner generates them, the changes will be removed. – Vlad Sep 12 '22 at 04:52