flutterfreezedflutter-freezed

flutter freezed package missing when and map methods in generated file


I'm working on a Flutter project and using the freezed package. I created a generic ApiResult class like this:

import 'package:freezed_annotation/freezed_annotation.dart';

part 'api_result.freezed.dart';

@freezed
sealed class ApiResult<T> with _$ApiResult<T> {
  const factory ApiResult.success(T data) = Success<T>;
  const factory ApiResult.failure(String msg) = Failure<T>;
}

After running the code generation, the generated file does not include when, map, or similar pattern-matching methods. Here's a snippet from the generated code:

class Success<T> implements ApiResult<T> {
  const Success(this.data);
  final T data;
  // ...
}

class Failure<T> implements ApiResult<T> {
  const Failure(this.msg);
  final String msg;
  // ...
}

As you can see, there are no when or map methods generated. I expected them to be available since they are commonly used with freezed classes for pattern matching.

Why are the when and map methods missing? How can I fix this?

I'm using the latest version of freezed and build_runner, and I ran

dart run build_runner build --delete-conflicting-outputs 

without any issues. Any help would be appreciated!


Solution

  • As the migration guide says(v2 to v3):

    Freezed no longer generates .map/.when extensions and their derivatives for freezed classes used for pattern matching. Instead, use Dart's built-in pattern matching syntax.