Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When creating State, if it is a specific type and written in a single line, it will result in an error. #929

Open
tattuu opened this issue May 28, 2023 · 3 comments
Assignees
Labels
bug Something isn't working question Further information is requested

Comments

@tattuu
Copy link

tattuu commented May 28, 2023

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

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;
}

no_error

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.

@tattuu tattuu added bug Something isn't working needs triage labels May 28, 2023
@rrousselGit
Copy link
Owner

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.

@rrousselGit rrousselGit added question Further information is requested and removed needs triage labels May 28, 2023
@tattuu
Copy link
Author

tattuu commented May 28, 2023

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

  1. Create new Flutter Project
  2. Add the following two packages to pubspec.yaml
    dev_dependencies:
      build_runner: ^2.4.4
      freezed: ^2.3.4
  3. command execution: flutter pub get
  4. Create temp_state.dart
  5. 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;
    }
  6. 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.

@simphotonics
Copy link

simphotonics commented May 18, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants