freezed
freezed copied to clipboard
When creating State, if it is a specific type and written in a single line, it will result in an error.
Describe the bug
If you create a value of type List<Map<String, dynamic>> in State, and write it as shown in the attached image 1, there will be no errors when you execute flutter pub run build_runner build in two lines. However, if you execute flutter pub run build_runner build in one line, you will get the error message: "The name 'Map' isn't a type, so it can't be used as a type argument. Try correcting the name to an existing type or defining a type named 'Map'."
In this particular type, an error occurs only when it is written in a single line.
To Reproduce
no error
import 'package:freezed_annotation/freezed_annotation.dart';
part 'temp_state.freezed.dart';
@freezed
class TempState with _$TempState {
const factory TempState({
// two lines
@Default(<Map<String, dynamic>>[])
List<Map<String, dynamic>> xxxxxxxxxxxxxxxxxxxx,
}) = _TempState;
}
error
import 'package:freezed_annotation/freezed_annotation.dart';
part 'temp_state.freezed.dart';
@freezed
class TempState with _$TempState {
const factory TempState({
// one line
@Default(<Map<String, dynamic>>[]) List<Map<String, dynamic>> xxx,
}) = _TempState;
}
Expected behavior I believe it is desirable for the code to not produce any errors, regardless of whether it is written in one line or two lines.
As this is my first issue to submit to the this project, I apologize if I have provided any incorrect information. I would appreciate it if you could reply whenever you have time.
The errors you're seeing aren't produced by Freezed by Dart itself.
Freezed is unlikely to be at fault here. Rather there's something in your code/environment which causes this.
Without a reproducible example, there's nothing I can do to help.
Thank you for your message. It seems unlikely that Freezed is the cause.
In sending the issue, I created a new flutter project and tried to reproduce the issue, and got the same error. So, if it is not due to freezed, it is most likely a bug in flutter itself.
Just to be sure, I will share the details of my environment where this error occurred.
environment
- Flutter version 3.10.2
- Dart version 3.0.2
reproduction procedure
- Create new Flutter Project
- Add the following two packages to pubspec.yaml
dev_dependencies: build_runner: ^2.4.4 freezed: ^2.3.4 - command execution:
flutter pub get - Create temp_state.dart
- Copy and paste the following code into your file
import 'package:freezed_annotation/freezed_annotation.dart'; part 'temp_state.freezed.dart'; @freezed class TempState with _$TempState { const factory TempState({ // one line @Default(<Map<String, dynamic>>[]) List<Map<String, dynamic>> xxx, }) = _TempState; } - command execution:
flutter packages pub run build_runner build --delete-conflicting-outputs
Sorry to take up your time. This is a very puzzling error, and if it is not likely to be caused by freezed, I would like to throw an issue to flutter project itself.
Tested with freezed: ^2.5.3. This is a work-around:
import 'package:freezed_annotation/freezed_annotation.dart';
part 'temp_state.freezed.dart';
const map = <Map<String, dynamic>>[]; // <-------- Define the default value
@freezed
class TempState with _$TempState {
const factory TempState({
@Default(map) List<Map<String, dynamic>> xxx, // <------- One single line of code
}) = _TempState;
}
and in the generated file temp_state.freezed.dart:
...
/// @nodoc
mixin _$TempState {
List<Map<String, dynamic>> get xxx => throw _privateConstructorUsedError;
@JsonKey(ignore: true)
$TempStateCopyWith<TempState> get copyWith =>
throw _privateConstructorUsedError;
}
...
If the annotation is replaced with: @Default(<Map<String, dynamic>>[]) the generated file temp_state.freezed.dart becomes:
...
/// @nodoc
mixin _$TempState {
Map<String, dynamic>>[]) List<Map<String, dynamic>> get xxx => throw _privateConstructorUsedError;
@JsonKey(ignore: true)
$TempStateCopyWith<TempState> get copyWith => throw _privateConstructorUsedError;
}
...
So to me this looks like an error with the source code generator where it renders
Map<String, dynamic>>[]) List<Map<String, dynamic>> instead of the type List<Map<String, dynamic>> throughout the generated file.