freezed
freezed copied to clipboard
Freezed Fails To Generate Class Depending on Line Break
Describe the bug
I have a class which is generated by freezed and contains a Default
decorator on a IList property. Whenever the decorator and the type are on the same line the freezed generator breaks, if they are on different lines it works.
BuildRunner Failed Output
freezed on lib/model.dart: An error `FormatterException` occurred while formatting the generated source for `package:freezed_line_length_issue/model.dart` which was output to `lib/model.freezed.dart`. This may indicate an issue in the generator, the input source code, or in the source formatter. Could not format because the source could not be parsed:
line 20, column 27 of .: Expected an identifier. ╷ 20 │ IListConst<ServerTrail>([])) IList<ServerTrail> get serverTrails => throw _privateConstructorUsedError; │ ^ ╵ line 20, column 29 of .: A function body must be provided. ╷ 20 │ IListConst<ServerTrail>([])) IList<ServerTrail> get serverTrails => throw _privateConstructorUsedError; │ ^ ╵ line 20, column 29 of .: Expected a class member. ╷ 20 │ IListConst<ServerTrail>([])) IList<ServerTrail> get serverTrails => throw _privateConstructorUsedError; │ ^ ╵ line 38, column 27 of .: Expected an identifier. ╷ 38 │ IListConst<ServerTrail>([])) IList<ServerTrail> serverTrails │ ^ ╵ line 38, column 29 of .: Expected to find '}'. ╷ 38 │ IListConst<ServerTrail>([])) IList<ServerTrail> serverTrails │ ^ ╵ line 57, column 27 of .: Expected to find ','. ╷ 57 │ as IListConst<ServerTrail>([])) IList<ServerTrail>, │ ^ ╵ line 57, column 33 of .: Expected to find ','. ╷ 57 │ as IListConst<ServerTrail>([])) IList<ServerTrail>, │ ^^^^^ ╵ line 58, column 7 of .: Expected to find ';'. ╷ 58 │ )as $Val); │ ^^^^ ╵ line 58, column 11 of .: Expected an identifier. ╷ 58 │ )as $Val); │ ^ ╵ line 58, column 11 of .: Unexpected text ';'. ╷ 58 │ )as $Val); │ ^ ╵ (17 more errors...) [INFO] Running build completed, took 37ms [INFO] Caching finalized dependency graph completed, took 38ms [SEVERE] Failed after 79ms
To Reproduce
//model.dart
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:flutter/foundation.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:freezed_line_length_issue/trail.dart';
part 'model.freezed.dart';
@freezed
class Model with _$Model {
const Model._();
const factory Model({
@Default(IListConst<ServerTrail>([])) IList<ServerTrail> serverTrails, // if IList<ServerTrail> serverTrails is on new line it succeeds
}) = _Model;
}
For completeness here is trail.dart
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:flutter/foundation.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
part 'trail.freezed.dart';
@freezed
sealed class Trail with _$Trail {
const Trail._();
const factory Trail.FromServer({
required String id,
required IList<num> points,
}) = ServerTrail;
}
Expected behavior Build runner would generate types w/out throwing errors regardless of whether the Default decorator is on its own line.
ServerTrail
is code-generated.
Freezed classes do not support generated types as inputs. You'll have to wait for metaprogramming for this.
Do you still have the issue when not using ServerTrail
?
Freezed classes do not support generated types as inputs. You'll have to wait for metaprogramming for this.
I'm a bit confused about this statement. If that is true then why would it work perfectly fine if the @Default
is its own line, and I've had a parameter in a freezed class that used a generated type before and it has worked fine.
I think the issue is with the IList
class. If I just use a normal List<ServerTrail> then it builds correctly.
There's likely a bug here. It's just that your current sample uses patterns that aren't supported
Overall I don't have too much time to investigate this. Feel free to open a PR :)
Faced the same issue and got it working by replacing with typedef. Very strange behavior.
import 'package:freezed_annotation/freezed_annotation.dart';
part 'filter_cache_state.freezed.dart';
typedef AcceptanceReportFilter = Map<AcceptanceReportFilterType,
ListFilter<dynamic, AcceptanceReportFilterType>>;
@freezed
class FilterCacheState with _$FilterCacheState {
const factory FilterCacheState({
required Session? session,
required Map<IssueFilterType, ListFilter<dynamic, IssueFilterType>>?
issueFilter,
required Map<JournalFilterType, ListFilter<dynamic, JournalFilterType>>?
journalFilter,
// This doesn't work as reported in bug with linebreak
required Map<AcceptanceReportFilterType,
ListFilter<dynamic, AcceptanceReportFilterType>>?
acceptanceReportFilter,
// Replacing same with typedef above works
// required AcceptanceReportFilter? acceptanceReportFilter,
}) = _FilterCacheState;
factory FilterCacheState.empty({required Session session}) =>
FilterCacheState(
session: session,
issueFilter: null,
journalFilter: null,
acceptanceReportFilter: null,
);
factory FilterCacheState.noSession() => const FilterCacheState(
session: null,
issueFilter: null,
journalFilter: null,
acceptanceReportFilter: null,
);
}
enum AcceptanceReportFilterType {
freeText,
creator,
}