Closed orestesgaolin closed 4 years ago
Adding build.yaml
file from here solved the problem:
targets:
$default:
builders:
freezed|freezed:
enabled: false
builders:
freezed:
import: "package:freezed/builder.dart"
builder_factories: ["freezed"]
build_extensions: { ".dart": [".freezed.dart"] }
auto_apply: dependents
build_to: source
runs_before:
- json_serializable|json_serializable
- source_gen|combining_builder
Ok, I don't get it but now it doesn't work anymore. I removed one property from Record class and now no files are generated at all. I cleaned the project, removed .dart-tool directory and tried several times with and without build.yaml.
So to sum up this is how it looks now:
record.dart
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:flutter/foundation.dart';
part 'record.g.dart';
part 'record.freezed.dart';
@freezed
abstract class Record with _$Record {
const factory Record(
@JsonKey(name: '_id') int id,
@JsonKey(name: 'Identyfikator') int longId,
@JsonKey(name: 'Nazwa') String name,
@JsonKey(name: 'Synonim') String synonyms,
@JsonKey(name: 'Typ') String type,
@JsonKey(name: 'additionalInfo', nullable: true) String additionalInfo,
) = _Record;
factory Record.fromJson(Map<String, dynamic> json) => _$RecordFromJson(json);
// Map<String, dynamic> toJson() => toJson(this);
}
api_result.dart
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:flutter/foundation.dart';
import 'record.dart';
part 'api_result.freezed.dart';
part 'api_result.g.dart';
@freezed
abstract class ApiResult with _$ApiResult {
const factory ApiResult(
@JsonKey(name: 'records') List<Record> records,
@JsonKey(name: 'q') String query,
@JsonKey(name: 'resource_id') String resourceId,
@JsonKey(name: 'include_total') bool includeTotal,
@JsonKey(name: '_links') Map<String, String> links,
@JsonKey(name: 'total') int total,
) = _ApiResult;
factory ApiResult.fromJson(Map<String, dynamic> json) =>
_$ApiResultFromJson(json);
// Map<String, dynamic> toJson() => _$ApiResultToJson(this);
}
targets:
$default:
builders:
freezed|freezed:
enabled: false
builders:
freezed:
import: "package:freezed/builder.dart"
builder_factories: ["freezed"]
build_extensions: { ".dart": [".freezed.dart"] }
auto_apply: dependents
build_to: source
runs_before:
- json_serializable|json_serializable
- source_gen|combining_builder
commands:
> rm -rf ./.dart_tool
> flutter clean
Cleaning Xcode workspace... 3,0s
Deleting .dart_tool...
> flutter pub get
Running "flutter pub get" in recycleapp...
> flutter pub run build_runner build
[INFO] Generating build script...
[INFO] Generating build script completed, took 297ms
[INFO] Creating build script snapshot......
[INFO] Creating build script snapshot... completed, took 8.8s
[INFO] Initializing inputs
[INFO] Building new asset graph...
[INFO] Building new asset graph completed, took 499ms
[INFO] Checking for unexpected pre-existing outputs....
[INFO] Checking for unexpected pre-existing outputs. completed, took 1ms
[INFO] Running build...
[INFO] Generating SDK summary...
[INFO] 2.9s elapsed, 0/16 actions completed.
[INFO] Generating SDK summary completed, took 2.8s
[INFO] 3.9s elapsed, 3/19 actions completed.
[INFO] 5.0s elapsed, 3/19 actions completed.
[INFO] 6.2s elapsed, 3/19 actions completed.
[INFO] 7.2s elapsed, 4/20 actions completed.
[INFO] 8.3s elapsed, 5/21 actions completed.
[INFO] 10.4s elapsed, 6/21 actions completed.
[INFO] 13.7s elapsed, 8/21 actions completed.
[INFO] 15.0s elapsed, 21/21 actions completed.
[INFO] Running build completed, took 15.1s
[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 62ms
[INFO] Succeeded after 15.1s with 0 outputs (42 actions)
If I remove build.yaml
file then we come back to the initial problem with The method _$_$_ApiResultFromJson' isn't defined for the class '_$_ApiResult
:
> flutter pub run build_runner build
[INFO] Generating build script...
[INFO] Generating build script completed, took 283ms
[INFO] Initializing inputs
[INFO] Reading cached asset graph...
[WARNING] Throwing away cached asset graph because the build phases have changed. This most commonly would happen as a result of adding a new dependency or updating your dependencies.
[INFO] Cleaning up outputs from previous builds....
[INFO] Cleaning up outputs from previous builds. completed, took 4ms
[INFO] Generating build script...
[INFO] Generating build script completed, took 58ms
[INFO] Creating build script snapshot......
[INFO] Creating build script snapshot... completed, took 8.3s
[INFO] Initializing inputs
[INFO] Building new asset graph...
[INFO] Building new asset graph completed, took 489ms
[INFO] Checking for unexpected pre-existing outputs....
[INFO] Checking for unexpected pre-existing outputs. completed, took 1ms
[INFO] Running build...
[INFO] 1.0s elapsed, 3/19 actions completed.
[INFO] 2.1s elapsed, 3/19 actions completed.
[INFO] 3.1s elapsed, 3/19 actions completed.
[INFO] 4.3s elapsed, 3/19 actions completed.
[INFO] 5.3s elapsed, 5/19 actions completed.
[INFO] 9.7s elapsed, 6/21 actions completed.
[INFO] 10.8s elapsed, 7/21 actions completed.
[INFO] 11.8s elapsed, 20/21 actions completed.
[INFO] Running build completed, took 12.7s
[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 45ms
[INFO] Succeeded after 12.7s with 2 outputs (67 actions)
What is the version of build_runner that you are using?
Hi @rrousselGit. I was hacking around and finally just removed everything that could affect this i.e.:
targets:
$default:
builders:
freezed|freezed:
enabled: true
builders:
freezed:
import: "package:freezed/builder.dart"
builder_factories: ["freezed"]
build_extensions: { ".dart": [".freezed.dart"] }
auto_apply: dependents
build_to: source
runs_before:
- json_serializable|json_serializable
- source_gen|combining_builder
Build_runner is at 1.7.4 right now and it seems it's working. The most annoying part was that it worked once and then stopped only after removing one property from the model class.
Now I see in the log that both freezed and json_serializable builders are running in proper order:
Hello, I have this problem too.
This is my pubspec.yaml
targets:
$default:
builders:
freezed|freezed:
enabled: false
builders:
freezed:
import: "package:freezed/builder.dart"
builder_factories: ["freezed"]
build_extensions: { ".dart": [".freezed.dart"] }
auto_apply: dependents
build_to: source
runs_before:
- json_serializable|json_serializable
- source_gen|combining_builder
name: project
description: A new Flutter project.
version: 1.0.0+1
environment:
sdk: ">=2.6.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
chopper: ^3.0.2
cupertino_icons: ^0.1.2
http: ^0.12.0+4
built_value: ^7.0.8
injectable: ^0.1.0
get_it: ^3.1.0
dartz: ^0.8.9
equatable: ^1.0.3
data_connection_checker: ^0.3.4
flutter_html: ^0.11.1
flutter_auth_buttons: ^0.6.0
flutter_facebook_login: ^3.0.0
google_sign_in: ^4.1.1
sqflite: ^1.2.1
url_launcher: ^5.4.1
provider: ^4.0.4
freezed_annotation: ^0.7.1
dev_dependencies:
flutter_test:
sdk: flutter
built_value_generator: ^7.0.8
injectable_generator: ^0.1.0
build_runner:
lint: ^1.1.1
chopper_generator: ^3.0.4
freezed: ^0.9.2
flutter:
fonts:
- family: MyFlutterApp
fonts:
- asset: fonts/MyFlutterApp.ttf
uses-material-design: true
assets:
- assets/ic_facebook.png
- assets/ic_google.png
- assets/privacy_and_policy.html
And even if I clean the project, delete the build folder delete pubspec.lock, etc... It will never generate my order.g.dart file.
Here is the freezed class
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:json_annotation/json_annotation.dart';
part 'order.g.dart';
part 'order.freezed.dart';
@freezed
abstract class Order with _$Order {
const factory Order({
@JsonKey(name: "Eto_mey") String name,
int age,
}) = _Order;
factory Order.fromJson(Map<String, dynamic> json) => _$OrderFromJson(json);
}
and here is the generated freeezd class
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named
part of 'order.dart';
// **************************************************************************
// FreezedGenerator
// **************************************************************************
Order _$OrderFromJson(Map<String, dynamic> json) {
return _Order.fromJson(json);
}
mixin _$Order {
@JsonKey(name: 'Eto_mey')
String get name;
int get age;
Order copyWith({@JsonKey(name: 'Eto_mey') String name, int age});
Map<String, dynamic> toJson();
}
class _$OrderTearOff {
const _$OrderTearOff();
_Order call({@JsonKey(name: 'Eto_mey') String name, int age}) {
return _Order(
name: name,
age: age,
);
}
}
const $Order = _$OrderTearOff();
@JsonSerializable()
class _$_Order implements _Order {
const _$_Order({@JsonKey(name: 'Eto_mey') this.name, this.age});
factory _$_Order.fromJson(Map<String, dynamic> json) =>
_$_$_OrderFromJson(json);
@override
@JsonKey(name: 'Eto_mey')
final String name;
@override
final int age;
@override
String toString() {
return 'Order(name: $name, age: $age)';
}
@override
bool operator ==(dynamic other) {
return identical(this, other) ||
(other is _Order &&
(identical(other.name, name) ||
const DeepCollectionEquality().equals(other.name, name)) &&
(identical(other.age, age) ||
const DeepCollectionEquality().equals(other.age, age)));
}
@override
int get hashCode =>
runtimeType.hashCode ^
const DeepCollectionEquality().hash(name) ^
const DeepCollectionEquality().hash(age);
@override
_$_Order copyWith({
Object name = freezed,
Object age = freezed,
}) {
return _$_Order(
name: name == freezed ? this.name : name as String,
age: age == freezed ? this.age : age as int,
);
}
@override
Map<String, dynamic> toJson() {
return _$_$_OrderToJson(this);
}
}
abstract class _Order implements Order {
const factory _Order({@JsonKey(name: 'Eto_mey') String name, int age}) =
_$_Order;
factory _Order.fromJson(Map<String, dynamic> json) = _$_Order.fromJson;
@override
@JsonKey(name: 'Eto_mey')
String get name;
@override
int get age;
@override
_Order copyWith({@JsonKey(name: 'Eto_mey') String name, int age});
}
This is my pubspec.lock
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
_fe_analyzer_shared:
dependency: transitive
description:
name: _fe_analyzer_shared
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.3"
analyzer:
dependency: transitive
description:
name: analyzer
url: "https://pub.dartlang.org"
source: hosted
version: "0.39.4"
archive:
dependency: transitive
description:
name: archive
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.11"
args:
dependency: transitive
description:
name: args
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.2"
async:
dependency: transitive
description:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.4.0"
boolean_selector:
dependency: transitive
description:
name: boolean_selector
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.5"
build:
dependency: transitive
description:
name: build
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.2"
build_config:
dependency: transitive
description:
name: build_config
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.2"
build_daemon:
dependency: transitive
description:
name: build_daemon
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.3"
build_resolvers:
dependency: transitive
description:
name: build_resolvers
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.3"
build_runner:
dependency: "direct dev"
description:
name: build_runner
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
build_runner_core:
dependency: transitive
description:
name: build_runner_core
url: "https://pub.dartlang.org"
source: hosted
version: "4.5.2"
built_collection:
dependency: transitive
description:
name: built_collection
url: "https://pub.dartlang.org"
source: hosted
version: "4.3.2"
built_value:
dependency: "direct main"
description:
name: built_value
url: "https://pub.dartlang.org"
source: hosted
version: "7.0.9"
built_value_generator:
dependency: "direct dev"
description:
name: built_value_generator
url: "https://pub.dartlang.org"
source: hosted
version: "7.0.9"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.2"
checked_yaml:
dependency: transitive
description:
name: checked_yaml
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.2"
chopper:
dependency: "direct main"
description:
name: chopper
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.2"
chopper_generator:
dependency: "direct dev"
description:
name: chopper_generator
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.4"
code_builder:
dependency: transitive
description:
name: code_builder
url: "https://pub.dartlang.org"
source: hosted
version: "3.2.1"
collection:
dependency: transitive
description:
name: collection
url: "https://pub.dartlang.org"
source: hosted
version: "1.14.11"
convert:
dependency: transitive
description:
name: convert
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
crypto:
dependency: transitive
description:
name: crypto
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.3"
csslib:
dependency: transitive
description:
name: csslib
url: "https://pub.dartlang.org"
source: hosted
version: "0.16.1"
cupertino_icons:
dependency: "direct main"
description:
name: cupertino_icons
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.3"
dart_style:
dependency: transitive
description:
name: dart_style
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.3"
dartz:
dependency: "direct main"
description:
name: dartz
url: "https://pub.dartlang.org"
source: hosted
version: "0.8.9"
data_connection_checker:
dependency: "direct main"
description:
name: data_connection_checker
url: "https://pub.dartlang.org"
source: hosted
version: "0.3.4"
equatable:
dependency: "direct main"
description:
name: equatable
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.1"
fixnum:
dependency: transitive
description:
name: fixnum
url: "https://pub.dartlang.org"
source: hosted
version: "0.10.11"
flutter:
dependency: "direct main"
description: flutter
source: sdk
version: "0.0.0"
flutter_auth_buttons:
dependency: "direct main"
description:
name: flutter_auth_buttons
url: "https://pub.dartlang.org"
source: hosted
version: "0.6.0"
flutter_facebook_login:
dependency: "direct main"
description:
name: flutter_facebook_login
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.0"
flutter_html:
dependency: "direct main"
description:
name: flutter_html
url: "https://pub.dartlang.org"
source: hosted
version: "0.11.1"
flutter_test:
dependency: "direct dev"
description: flutter
source: sdk
version: "0.0.0"
flutter_web_plugins:
dependency: transitive
description: flutter
source: sdk
version: "0.0.0"
freezed:
dependency: "direct dev"
description:
name: freezed
url: "https://pub.dartlang.org"
source: hosted
version: "0.9.2"
freezed_annotation:
dependency: "direct main"
description:
name: freezed_annotation
url: "https://pub.dartlang.org"
source: hosted
version: "0.7.1"
get_it:
dependency: "direct main"
description:
name: get_it
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.0"
glob:
dependency: transitive
description:
name: glob
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
google_sign_in:
dependency: "direct main"
description:
name: google_sign_in
url: "https://pub.dartlang.org"
source: hosted
version: "4.1.4"
google_sign_in_platform_interface:
dependency: transitive
description:
name: google_sign_in_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.4"
google_sign_in_web:
dependency: transitive
description:
name: google_sign_in_web
url: "https://pub.dartlang.org"
source: hosted
version: "0.8.3+2"
graphs:
dependency: transitive
description:
name: graphs
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.0"
html:
dependency: transitive
description:
name: html
url: "https://pub.dartlang.org"
source: hosted
version: "0.14.0+3"
http:
dependency: "direct main"
description:
name: http
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.0+4"
http_multi_server:
dependency: transitive
description:
name: http_multi_server
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.0"
http_parser:
dependency: transitive
description:
name: http_parser
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.3"
image:
dependency: transitive
description:
name: image
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.4"
injectable:
dependency: "direct main"
description:
name: injectable
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.0"
injectable_generator:
dependency: "direct dev"
description:
name: injectable_generator
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.0"
io:
dependency: transitive
description:
name: io
url: "https://pub.dartlang.org"
source: hosted
version: "0.3.3"
js:
dependency: transitive
description:
name: js
url: "https://pub.dartlang.org"
source: hosted
version: "0.6.1+1"
json_annotation:
dependency: transitive
description:
name: json_annotation
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.1"
lint:
dependency: "direct dev"
description:
name: lint
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.1"
logging:
dependency: transitive
description:
name: logging
url: "https://pub.dartlang.org"
source: hosted
version: "0.11.4"
matcher:
dependency: transitive
description:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.6"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.8"
mime:
dependency: transitive
description:
name: mime
url: "https://pub.dartlang.org"
source: hosted
version: "0.9.6+3"
nested:
dependency: transitive
description:
name: nested
url: "https://pub.dartlang.org"
source: hosted
version: "0.0.4"
node_interop:
dependency: transitive
description:
name: node_interop
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.3"
node_io:
dependency: transitive
description:
name: node_io
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.1+2"
package_config:
dependency: transitive
description:
name: package_config
url: "https://pub.dartlang.org"
source: hosted
version: "1.9.1"
package_resolver:
dependency: transitive
description:
name: package_resolver
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.10"
path:
dependency: transitive
description:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.6.4"
pedantic:
dependency: transitive
description:
name: pedantic
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0+1"
petitparser:
dependency: transitive
description:
name: petitparser
url: "https://pub.dartlang.org"
source: hosted
version: "2.4.0"
plugin_platform_interface:
dependency: transitive
description:
name: plugin_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.2"
pool:
dependency: transitive
description:
name: pool
url: "https://pub.dartlang.org"
source: hosted
version: "1.4.0"
provider:
dependency: "direct main"
description:
name: provider
url: "https://pub.dartlang.org"
source: hosted
version: "4.0.4"
pub_semver:
dependency: transitive
description:
name: pub_semver
url: "https://pub.dartlang.org"
source: hosted
version: "1.4.3"
pubspec_parse:
dependency: transitive
description:
name: pubspec_parse
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.5"
quiver:
dependency: transitive
description:
name: quiver
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.5"
shelf:
dependency: transitive
description:
name: shelf
url: "https://pub.dartlang.org"
source: hosted
version: "0.7.5"
shelf_web_socket:
dependency: transitive
description:
name: shelf_web_socket
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.3"
sky_engine:
dependency: transitive
description: flutter
source: sdk
version: "0.0.99"
source_gen:
dependency: transitive
description:
name: source_gen
url: "https://pub.dartlang.org"
source: hosted
version: "0.9.5"
source_span:
dependency: transitive
description:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.5"
sqflite:
dependency: "direct main"
description:
name: sqflite
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.2+1"
stack_trace:
dependency: transitive
description:
name: stack_trace
url: "https://pub.dartlang.org"
source: hosted
version: "1.9.3"
stream_channel:
dependency: transitive
description:
name: stream_channel
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
stream_transform:
dependency: transitive
description:
name: stream_transform
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
string_scanner:
dependency: transitive
description:
name: string_scanner
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.5"
synchronized:
dependency: transitive
description:
name: synchronized
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.0"
term_glyph:
dependency: transitive
description:
name: term_glyph
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
test_api:
dependency: transitive
description:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.11"
timing:
dependency: transitive
description:
name: timing
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.1+2"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.6"
url_launcher:
dependency: "direct main"
description:
name: url_launcher
url: "https://pub.dartlang.org"
source: hosted
version: "5.4.2"
url_launcher_macos:
dependency: transitive
description:
name: url_launcher_macos
url: "https://pub.dartlang.org"
source: hosted
version: "0.0.1+4"
url_launcher_platform_interface:
dependency: transitive
description:
name: url_launcher_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.6"
url_launcher_web:
dependency: transitive
description:
name: url_launcher_web
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.1+1"
vector_math:
dependency: transitive
description:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.8"
watcher:
dependency: transitive
description:
name: watcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.9.7+14"
web_socket_channel:
dependency: transitive
description:
name: web_socket_channel
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
xml:
dependency: transitive
description:
name: xml
url: "https://pub.dartlang.org"
source: hosted
version: "3.5.0"
yaml:
dependency: transitive
description:
name: yaml
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.0"
sdks:
dart: ">=2.7.0 <3.0.0"
flutter: ">=1.12.13+hotfix.4 <2.0.0"
Even now I have created a new project and Implemented the library but still not generating the *.g.dart class 😥
person.dart
import 'package:freezed_annotation/freezed_annotation.dart';
part 'person.freezed.dart';
part 'person.g.dart';
@freezed
abstract class Person with _$Person {
factory Person({
String name,
int age,
}) = _Person;
factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);
}
person.freezed.dart
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named
part of 'person.dart';
// **************************************************************************
// FreezedGenerator
// **************************************************************************
Person _$PersonFromJson(Map<String, dynamic> json) {
return _Person.fromJson(json);
}
mixin _$Person {
String get name;
int get age;
Person copyWith({String name, int age});
Map<String, dynamic> toJson();
}
class _$PersonTearOff {
const _$PersonTearOff();
_Person call({String name, int age}) {
return _Person(
name: name,
age: age,
);
}
}
const $Person = _$PersonTearOff();
@JsonSerializable()
class _$_Person implements _Person {
_$_Person({this.name, this.age});
factory _$_Person.fromJson(Map<String, dynamic> json) =>
_$_$_PersonFromJson(json);
@override
final String name;
@override
final int age;
@override
String toString() {
return 'Person(name: $name, age: $age)';
}
@override
bool operator ==(dynamic other) {
return identical(this, other) ||
(other is _Person &&
(identical(other.name, name) ||
const DeepCollectionEquality().equals(other.name, name)) &&
(identical(other.age, age) ||
const DeepCollectionEquality().equals(other.age, age)));
}
@override
int get hashCode =>
runtimeType.hashCode ^
const DeepCollectionEquality().hash(name) ^
const DeepCollectionEquality().hash(age);
@override
_$_Person copyWith({
Object name = freezed,
Object age = freezed,
}) {
return _$_Person(
name: name == freezed ? this.name : name as String,
age: age == freezed ? this.age : age as int,
);
}
@override
Map<String, dynamic> toJson() {
return _$_$_PersonToJson(this);
}
}
abstract class _Person implements Person {
factory _Person({String name, int age}) = _$_Person;
factory _Person.fromJson(Map<String, dynamic> json) = _$_Person.fromJson;
@override
String get name;
@override
int get age;
@override
_Person copyWith({String name, int age});
}
pubspec.yaml
name: f_test
description: A sample command-line application.
# version: 1.0.0
# homepage: https://www.example.com
environment:
sdk: ">=2.7.0 <3.0.0"
dependencies:
freezed_annotation: ^0.7.1
# path: ^1.6.0
dev_dependencies:
pedantic: ^1.8.0
test: ^1.6.0
freezed: ^0.9.2
build_runner:
pubspec.lock
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
_fe_analyzer_shared:
dependency: transitive
description:
name: _fe_analyzer_shared
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.3"
analyzer:
dependency: transitive
description:
name: analyzer
url: "https://pub.dartlang.org"
source: hosted
version: "0.39.4"
args:
dependency: transitive
description:
name: args
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.3"
async:
dependency: transitive
description:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.4.0"
boolean_selector:
dependency: transitive
description:
name: boolean_selector
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
build:
dependency: transitive
description:
name: build
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.2"
build_config:
dependency: transitive
description:
name: build_config
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.2"
build_daemon:
dependency: transitive
description:
name: build_daemon
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.3"
build_resolvers:
dependency: transitive
description:
name: build_resolvers
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.3"
build_runner:
dependency: "direct dev"
description:
name: build_runner
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
build_runner_core:
dependency: transitive
description:
name: build_runner_core
url: "https://pub.dartlang.org"
source: hosted
version: "4.5.2"
built_collection:
dependency: transitive
description:
name: built_collection
url: "https://pub.dartlang.org"
source: hosted
version: "4.3.2"
built_value:
dependency: transitive
description:
name: built_value
url: "https://pub.dartlang.org"
source: hosted
version: "7.0.9"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.3"
checked_yaml:
dependency: transitive
description:
name: checked_yaml
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.2"
code_builder:
dependency: transitive
description:
name: code_builder
url: "https://pub.dartlang.org"
source: hosted
version: "3.2.1"
collection:
dependency: transitive
description:
name: collection
url: "https://pub.dartlang.org"
source: hosted
version: "1.14.12"
convert:
dependency: transitive
description:
name: convert
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
coverage:
dependency: transitive
description:
name: coverage
url: "https://pub.dartlang.org"
source: hosted
version: "0.13.9"
crypto:
dependency: transitive
description:
name: crypto
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.4"
csslib:
dependency: transitive
description:
name: csslib
url: "https://pub.dartlang.org"
source: hosted
version: "0.16.1"
dart_style:
dependency: transitive
description:
name: dart_style
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.3"
fixnum:
dependency: transitive
description:
name: fixnum
url: "https://pub.dartlang.org"
source: hosted
version: "0.10.11"
freezed:
dependency: "direct dev"
description:
name: freezed
url: "https://pub.dartlang.org"
source: hosted
version: "0.9.2"
freezed_annotation:
dependency: "direct main"
description:
name: freezed_annotation
url: "https://pub.dartlang.org"
source: hosted
version: "0.7.1"
glob:
dependency: transitive
description:
name: glob
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
graphs:
dependency: transitive
description:
name: graphs
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.0"
html:
dependency: transitive
description:
name: html
url: "https://pub.dartlang.org"
source: hosted
version: "0.14.0+3"
http:
dependency: transitive
description:
name: http
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.0+4"
http_multi_server:
dependency: transitive
description:
name: http_multi_server
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.0"
http_parser:
dependency: transitive
description:
name: http_parser
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.3"
io:
dependency: transitive
description:
name: io
url: "https://pub.dartlang.org"
source: hosted
version: "0.3.3"
js:
dependency: transitive
description:
name: js
url: "https://pub.dartlang.org"
source: hosted
version: "0.6.1+1"
json_annotation:
dependency: transitive
description:
name: json_annotation
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.1"
logging:
dependency: transitive
description:
name: logging
url: "https://pub.dartlang.org"
source: hosted
version: "0.11.4"
matcher:
dependency: transitive
description:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.6"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.8"
mime:
dependency: transitive
description:
name: mime
url: "https://pub.dartlang.org"
source: hosted
version: "0.9.6+3"
multi_server_socket:
dependency: transitive
description:
name: multi_server_socket
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.2"
node_interop:
dependency: transitive
description:
name: node_interop
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.3"
node_io:
dependency: transitive
description:
name: node_io
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.1+2"
node_preamble:
dependency: transitive
description:
name: node_preamble
url: "https://pub.dartlang.org"
source: hosted
version: "1.4.8"
package_config:
dependency: transitive
description:
name: package_config
url: "https://pub.dartlang.org"
source: hosted
version: "1.9.1"
package_resolver:
dependency: transitive
description:
name: package_resolver
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.10"
path:
dependency: transitive
description:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.6.4"
pedantic:
dependency: "direct dev"
description:
name: pedantic
url: "https://pub.dartlang.org"
source: hosted
version: "1.9.0"
pool:
dependency: transitive
description:
name: pool
url: "https://pub.dartlang.org"
source: hosted
version: "1.4.0"
pub_semver:
dependency: transitive
description:
name: pub_semver
url: "https://pub.dartlang.org"
source: hosted
version: "1.4.3"
pubspec_parse:
dependency: transitive
description:
name: pubspec_parse
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.5"
quiver:
dependency: transitive
description:
name: quiver
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.3"
shelf:
dependency: transitive
description:
name: shelf
url: "https://pub.dartlang.org"
source: hosted
version: "0.7.5"
shelf_packages_handler:
dependency: transitive
description:
name: shelf_packages_handler
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
shelf_static:
dependency: transitive
description:
name: shelf_static
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.8"
shelf_web_socket:
dependency: transitive
description:
name: shelf_web_socket
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.3"
source_gen:
dependency: transitive
description:
name: source_gen
url: "https://pub.dartlang.org"
source: hosted
version: "0.9.5"
source_map_stack_trace:
dependency: transitive
description:
name: source_map_stack_trace
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
source_maps:
dependency: transitive
description:
name: source_maps
url: "https://pub.dartlang.org"
source: hosted
version: "0.10.9"
source_span:
dependency: transitive
description:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.6.0"
stack_trace:
dependency: transitive
description:
name: stack_trace
url: "https://pub.dartlang.org"
source: hosted
version: "1.9.3"
stream_channel:
dependency: transitive
description:
name: stream_channel
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
stream_transform:
dependency: transitive
description:
name: stream_transform
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
string_scanner:
dependency: transitive
description:
name: string_scanner
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.5"
term_glyph:
dependency: transitive
description:
name: term_glyph
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
test:
dependency: "direct dev"
description:
name: test
url: "https://pub.dartlang.org"
source: hosted
version: "1.14.2"
test_api:
dependency: transitive
description:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.15"
test_core:
dependency: transitive
description:
name: test_core
url: "https://pub.dartlang.org"
source: hosted
version: "0.3.3"
timing:
dependency: transitive
description:
name: timing
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.1+2"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.6"
vm_service:
dependency: transitive
description:
name: vm_service
url: "https://pub.dartlang.org"
source: hosted
version: "4.0.0"
watcher:
dependency: transitive
description:
name: watcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.9.7+14"
web_socket_channel:
dependency: transitive
description:
name: web_socket_channel
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
webkit_inspection_protocol:
dependency: transitive
description:
name: webkit_inspection_protocol
url: "https://pub.dartlang.org"
source: hosted
version: "0.5.0+1"
yaml:
dependency: transitive
description:
name: yaml
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.0"
sdks:
dart: ">=2.7.0 <3.0.0"
And here is the console output when running flutter pub run build_runner watch
[INFO] Generating build script...
[INFO] Generating build script completed, took 310ms
[INFO] Setting up file watchers...
[INFO] Setting up file watchers completed, took 10ms
[INFO] Waiting for all file watchers to be ready...
[INFO] Waiting for all file watchers to be ready completed, took 11ms
[INFO] Initializing inputs
[INFO] Reading cached asset graph...
[INFO] Reading cached asset graph completed, took 49ms
[INFO] Checking for updates since last build...
[INFO] Checking for updates since last build completed, took 439ms
[INFO] Cleaning up outputs from previous builds....
[INFO] Cleaning up outputs from previous builds. completed, took 13ms
[INFO] Terminating. No further builds will be scheduled
[INFO] Builds finished. Safe to exit
[INFO] Generating build script...
[INFO] Generating build script completed, took 74ms
[INFO] Creating build script snapshot......
[INFO] Creating build script snapshot... completed, took 9.7s
[INFO] Setting up file watchers...
[INFO] Setting up file watchers completed, took 10ms
[INFO] Waiting for all file watchers to be ready...
[INFO] Waiting for all file watchers to be ready completed, took 11ms
[INFO] Initializing inputs
[INFO] Building new asset graph...
[INFO] Checking for unexpected pre-existing outputs....
[INFO] Checking for unexpected pre-existing outputs. completed, took 1ms
[INFO] Running build...
[INFO] 1.1s elapsed, 1/4 actions completed.
[INFO] Running build completed, took 2.1s
[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 33ms
[INFO] Succeeded after 2.2s with 1 outputs (4 actions)
@amebrahimi I did a thorough cleanup of the project and removed build.yaml etc.
After that I added build.yaml for json_serializable:
targets:
$default:
builders:
json_serializable:
options:
# Options configure how source code is generated for every
# `@JsonSerializable`-annotated class in the package.
#
# The default value for each is listed.
any_map: true
create_to_json: true
disallow_unrecognized_keys: false
explicit_to_json: true
My pubspec looks like this:
name: app
version: 1.0.0+1
environment:
sdk: ">=2.7.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
freezed_annotation: ^0.7.1
json_serializable: ^3.2.5
dev_dependencies:
flutter_driver:
sdk: flutter
flutter_test:
sdk: flutter
test: any
build_runner:
freezed:
And I run build_runner via:
flutter pub run build_runner build --delete-conflicting-outputs
All in all everything sorted out after several cleans etc. Check if there are any errors in verbose mode.
I've added the json_serializable library to my pubspec.yaml and it worked, I think you should update your document and put this in that anyone wants to use the serializable part have to implement the json_serializable library. Thank you for you're response 😍🤗
That fixed it for me. Docs don't make this clear.
Someone told me to put json_serializable
in dev dependencies along with Freezed that I am using. Which is correct or is it case by case?
This is correct
Just ran into this problem myself. Docs are still really bad for specifying this! Surely it should be mentioned alongside the first example of creating a freezed model. All you get is "add part 'model.g.dart
and rebuild" without any other context until the bottom of the readme.
Just an informative "you need json_serialisable
to use this feature" would be helpful!
This is still not working for me, even with the json_serializable
dependency.
No main.g.dart
is generated after following the directions in the README
This is still not working for me, even with the
json_serializable
dependency. Nomain.g.dart
is generated after following the directions in theREADME
Finally I got mine work, which you have to put following to your original main.dart file. Otherwise the main.g.dart
won't be generated.
part 'main.g.dart';
After running on current stable 1.12.13+hotfix.8:
I see that:
g.dart
files are not generatedModel classes:
api_result.dart
and
record.dart
My pubspec dependencies are:
And flutter doctor output: