Describe the bug
build_runner will remove all generated *.g.dart files and fromJson/toJson code in *.freezed.dart but not regenerate after updating to Flutter 1.22.1.
I tried to discard all file changes but they are not fixed. It works fine on Flutter 1.22.0.
This is my pubspec.yaml file:
environment:
sdk: ">=2.7.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
app_review: ^2.0.1
badges: ^1.1.3
bloc: ^6.0.3
bubble: ^1.1.9+1
cached_network_image: ^2.3.2+1
camera: ^0.5.8+8
dartz: ^0.9.2
data_connection_checker: ^0.3.4
dio: ^3.0.10
extended_nested_scroll_view: ^1.0.1
firebase_core: ^0.5.0+1
firebase_analytics: ^6.0.2
firebase_crashlytics: ^0.2.1+1
fluro: ^1.6.3
flutter_bloc: ^6.0.6
flutter_cache_manager: ^1.4.2
flutter_screenutil: ^3.1.1
font_awesome_flutter: ^8.10.0
freezed_annotation: ^0.12.0
get_it: ^5.0.1
image_cropper: ^1.3.1
image_gallery_saver: '^1.6.0'
image_picker: ^0.6.7+11
injectable: ^1.0.5
intl: ^0.16.1
json_annotation: ^3.1.0
kt_dart: ^0.8.0
logger: ^0.9.4
oktoast: ^2.3.2
package_info: ^0.4.3
path: ^1.7.0
percent_indicator: ^2.1.7+4
permission_handler: ^5.0.1+1
photo_view: ^0.10.2
pull_to_refresh: ^1.6.2
shared_preferences: ^0.5.12
sqflite: ^1.3.1+1
timeago: ^2.0.27
url_launcher: ^5.7.2
uuid: ^2.2.2
webview_flutter: ^1.0.1
dev_dependencies:
flutter_test:
sdk: flutter
build_runner: ^1.10.3
flutter_launcher_icons: ^0.8.1
freezed: ^0.12.2
injectable_generator: ^1.0.6
json_serializable: ^3.5.0
mockito: ^4.1.1
pedantic: ^1.9.2
To Reproduce
I have no idea what happened on my code. I just updated Flutter SDK and some packages like build_runner to the latest version and run flutter pub run build_runner watch --delete-conflicting-outputs as usual.
Expected behavior
Resolve this problem.
update:
Flutter SDK looks like irrelevant. After I downgrade the SDK version to 1.22.0 it still is not working.
What do you mean by they stop generating? Do you have an error?
When I run flutter pub run build_runner watch --delete-conflicting-outputs, all old *.g.dart files and fromJson/toJson methods like below will be deleted, and will be not regenerated.
Model _$ModelFromJson(Map<String, dynamic> json) {
return _Model.fromJson(json);
}
No error occurred of course.
...
[FINE] json_serializable:json_serializable on lib/model.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on lib/model.dart:Running JsonLiteralGenerator - 2 of 2
...
[INFO] Build:Caching finalized dependency graph...
[INFO] Build:Caching finalized dependency graph completed, took 391ms
[INFO] Build:Succeeded after 57.5s with 196 outputs (2463 actions)
[SEVERE] Watch:
Terminating builds due to package graph update, please restart the build.
[INFO] Watch:Terminating. No further builds will be scheduled
[INFO] Watch:Builds finished. Safe to exit
That's what I did today:
flutter clean & flutter pub upgradeflutter pub run build_runner watch --delete-conflicting-outputsI'm also seeing this problem. My workaround was to downgrade Flutter, run flutter pub run build_runner watch --delete-conflicting-outputs and then upgrade Flutter. It's not ideal, but should keep development going until a fix has been applied to Freezed.
@James-A-White Thanks for sharing. Well, it's not the best approach but seems not bad. build_runner will cache the previous snapshot unless you clean it up. I guess that's why this problem did not happened when I upgraded to the Flutter 1.22.0.
That's what I did today:
- update Flutter SDK to 1.22.1
- update some packages
- run
flutter clean&flutter pub upgrade- run
flutter pub run build_runner watch --delete-conflicting-outputs- issue happened
- discard all file changes and try step.3~4 again
- still happened
- downgrade Flutter SDK to 1.22.0 and try step.3~4 again
- still happened
I tried that locally and it works for me
The CI also does that and pass
So I'm not sure how to reproduce the issue
@rrousselGit Thank you. That's weird but I'll try to find out more information.
Not that it is super helpful, but it will generate the proper files for most of my data classes, but there are a few that just don't work. I've copied one below for your entertainment pleasure.
// Copyright 漏 2020, InnoVet Europe
// All rights reserved
// package imports
import 'package:freezed_annotation/freezed_annotation.dart';
part 'place_status_request.g.dart';
part 'place_status_request.freezed.dart';
@freezed
abstract class PlaceStatusRequest with _$PlaceStatusRequest {
const factory PlaceStatusRequest({
String staffId,
String accessToken,
String tapPointId,
String roomId,
String floorplanId,
String buildingId,
String facilityId,
String functionId,
String functionCode,
String functionStatusId,
String functionStatusCode,
String data,
String topPriorityParams,
String bottomPriorityParams,
}) = _PlaceStatusRequest;
factory PlaceStatusRequest.fromJson(Map<String, dynamic> json) {
return _$PlaceStatusRequestFromJson(json);
}
}
Everything works fine on Flutter 1.20.4, build_runner 1.10.1, freezed 0.12.1. All files and methods are generated completely. But it is still broken on Flutter 1.22+, even though use build_runner 1.10.1, freezed 0.12.1.
@rrousselGit Hi馃憢, I reproduced this issue follow the steps below:
freezed_test, Flutter: 1.22.1)pubspec.yaml to name: freezed_test
description: A new Flutter project.
publish_to: 'none'
version: 1.0.0+1
environment:
sdk: ">=2.10.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
freezed_annotation: ^0.12.0
json_annotation: ^3.1.0
dev_dependencies:
flutter_test:
sdk: flutter
build_runner: ^1.10.3
freezed: ^0.12.2
json_serializable: ^3.5.0
flutter:
uses-material-design: true
test_model.dartpart 'test_model.freezed.dart';
part 'test_model.g.dart';
@freezed
abstract class TestModel with _$TestModel {
const factory TestModel({
String id,
@JsonKey(name: 'test_name') String testName,
}) = _TestModel;
factory TestModel.fromJson(Map
return _$TestModelFromJson(json);
}
}
4. run `flutter pub upgrade`
5. run `flutter pub run build_runner watch --delete-conflicting-outputs`
And then only `test_model.freezed.dart` will be generated without `test_model.g.dart` and `fromJson/toJson` methods.
Log:
[INFO] Generating build script...
[INFO] Generating build script completed, took 356ms
[INFO] Creating build script snapshot......
[INFO] Creating build script snapshot... completed, took 10.8s
[INFO] Watch:Setting up file watchers...
[INFO] Watch:Setting up file watchers completed, took 13ms
[INFO] Watch:Waiting for all file watchers to be ready...
[INFO] Watch:Waiting for all file watchers to be ready completed, took 200ms
[INFO] BuildDefinition:Initializing inputs
[INFO] BuildDefinition:Building new asset graph...
[INFO] BuildDefinition:Building new asset graph completed, took 530ms
[INFO] BuildDefinition:Checking for unexpected pre-existing outputs....
[INFO] BuildDefinition:Checking for unexpected pre-existing outputs. completed, took 1ms
[INFO] Build:Running build...
[INFO] Heartbeat:1.2s elapsed, 0/3 actions completed.
[INFO] Heartbeat:2.3s elapsed, 0/3 actions completed.
[FINE] freezed:freezed on lib/test_model.dart:Running FreezedGenerator
[INFO] Heartbeat:6.4s elapsed, 1/3 actions completed.
[FINE] freezed:freezed on lib/main.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/widget_test.dart:Running FreezedGenerator
[FINE] json_serializable:json_serializable on lib/main.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on lib/main.dart:Running JsonLiteralGenerator - 2 of 2
[FINE] json_serializable:json_serializable on test/widget_test.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on test/widget_test.dart:Running JsonLiteralGenerator - 2 of 2
[FINE] json_serializable:json_serializable on lib/test_model.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on lib/test_model.dart:Running JsonLiteralGenerator - 2 of 2
[INFO] Build:Running build completed, took 12.0s
[INFO] Build:Caching finalized dependency graph...
[INFO] Build:Caching finalized dependency graph completed, took 159ms
[INFO] Build:Succeeded after 12.1s with 1 outputs (11 actions)
Generated freezed file:
```dart
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies
part of 'test_model.dart';
// **************************************************************************
// FreezedGenerator
// **************************************************************************
T _$identity<T>(T value) => value;
/// @nodoc
class _$TestModelTearOff {
const _$TestModelTearOff();
// ignore: unused_element
_TestModel call({String id, @JsonKey(name: 'test_name') String testName}) {
return _TestModel(
id: id,
testName: testName,
);
}
}
/// @nodoc
// ignore: unused_element
const $TestModel = _$TestModelTearOff();
/// @nodoc
mixin _$TestModel {
String get id;
@JsonKey(name: 'test_name')
String get testName;
$TestModelCopyWith<TestModel> get copyWith;
}
/// @nodoc
abstract class $TestModelCopyWith<$Res> {
factory $TestModelCopyWith(TestModel value, $Res Function(TestModel) then) =
_$TestModelCopyWithImpl<$Res>;
$Res call({String id, @JsonKey(name: 'test_name') String testName});
}
/// @nodoc
class _$TestModelCopyWithImpl<$Res> implements $TestModelCopyWith<$Res> {
_$TestModelCopyWithImpl(this._value, this._then);
final TestModel _value;
// ignore: unused_field
final $Res Function(TestModel) _then;
@override
$Res call({
Object id = freezed,
Object testName = freezed,
}) {
return _then(_value.copyWith(
id: id == freezed ? _value.id : id as String,
testName: testName == freezed ? _value.testName : testName as String,
));
}
}
/// @nodoc
abstract class _$TestModelCopyWith<$Res> implements $TestModelCopyWith<$Res> {
factory _$TestModelCopyWith(
_TestModel value, $Res Function(_TestModel) then) =
__$TestModelCopyWithImpl<$Res>;
@override
$Res call({String id, @JsonKey(name: 'test_name') String testName});
}
/// @nodoc
class __$TestModelCopyWithImpl<$Res> extends _$TestModelCopyWithImpl<$Res>
implements _$TestModelCopyWith<$Res> {
__$TestModelCopyWithImpl(_TestModel _value, $Res Function(_TestModel) _then)
: super(_value, (v) => _then(v as _TestModel));
@override
_TestModel get _value => super._value as _TestModel;
@override
$Res call({
Object id = freezed,
Object testName = freezed,
}) {
return _then(_TestModel(
id: id == freezed ? _value.id : id as String,
testName: testName == freezed ? _value.testName : testName as String,
));
}
}
/// @nodoc
class _$_TestModel implements _TestModel {
const _$_TestModel({this.id, @JsonKey(name: 'test_name') this.testName});
@override
final String id;
@override
@JsonKey(name: 'test_name')
final String testName;
@override
String toString() {
return 'TestModel(id: $id, testName: $testName)';
}
@override
bool operator ==(dynamic other) {
return identical(this, other) ||
(other is _TestModel &&
(identical(other.id, id) ||
const DeepCollectionEquality().equals(other.id, id)) &&
(identical(other.testName, testName) ||
const DeepCollectionEquality()
.equals(other.testName, testName)));
}
@override
int get hashCode =>
runtimeType.hashCode ^
const DeepCollectionEquality().hash(id) ^
const DeepCollectionEquality().hash(testName);
@override
_$TestModelCopyWith<_TestModel> get copyWith =>
__$TestModelCopyWithImpl<_TestModel>(this, _$identity);
}
abstract class _TestModel implements TestModel {
const factory _TestModel(
{String id, @JsonKey(name: 'test_name') String testName}) = _$_TestModel;
@override
String get id;
@override
@JsonKey(name: 'test_name')
String get testName;
@override
_$TestModelCopyWith<_TestModel> get copyWith;
}
Oh, I get what happens
That's because your fromJson syntax is not correct
Do:
factory TestModel.fromJson(Map<String, dynamic> json) => _$TestModelFromJson(json);
Don't:
factory TestModel.fromJson(Map<String, dynamic> json) {
return _$TestModelFromJson(json);
}
The reason why is because if you use {}, Freezed interprets it as you having a custom fromJson and do not use json_serializable
I got it, thank you!
May be you should add this case to document because some people prefer to use {} rather than =>.
Most helpful comment
Oh, I get what happens
That's because your
fromJsonsyntax is not correctDo:
Don't:
The reason why is because if you use
{}, Freezed interprets it as you having a custom fromJson and do not use json_serializable