Skip to content

Some outputs of freezed are not compatible with dart ^3.13 #1352

@victorcatn

Description

@victorcatn

Describe the bug

Freezed adds the final modifier to parameters a collection type by default unless makeCollectionsUnmodifiable is explicitly set to false. That is now illegal in dart 3.13 due to the Primary Constructors implementation:

With this feature, all other declarations of formal parameters as final will be a compile-time error. This ensures that final int x is unambiguously a declaring parameter. Developers who wish to maintain a style whereby formal parameters are never modified will have a lint to flag all such mutations.

Similarly, with this feature a regular (non-declaring) formal parameter can not use the syntax var name, it must have a type (T name) or the type must be omitted (name).

To Reproduce

Run a dart project using the following dependencies in pubspec.yml:

environment:
  sdk: ^3.13.0-103.1.beta

dependencies:
  freezed_annotation: ^3.1.0

dev_dependencies:
  build_runner: ^2.15.0
  freezed: ^3.2.6-dev.1

main.dart:

import 'package:freezed_annotation/freezed_annotation.dart';

part 'main.freezed.dart';

@Freezed()
abstract class A with _$A {
  const factory A({required List<int> b}) = _A;
}

run
dart run build_runner build

see the generated code:

class _A implements A {
  const _A({required final  List<int> b}): _b = b;
  ...

run
dart analyze

The following error is thrown:
error • lib/main.freezed.dart:209:22 • Can't have modifier 'final' here. Try removing 'final'. • extraneous_modifier

Expected behavior
No errors should appear in the generated code

Workaround
Set makeCollectionsUnmodifiable: false

There's another illegal case without workaround: union types with deserialization:

@Freezed(unionKey: 'type')
sealed class A with _$A {
  const factory A.B() = B;

  @FreezedUnionValue('c')
  const factory A.C() = C;

  factory A.fromJson(Map<String, dynamic> json) => _$AFromJson(json);
}

generates

@JsonSerializable()

class B implements A {
  const B({final  String? $type}): $type = $type ?? 'B';
...

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions