rrousselGit / riverpod

A reactive caching and data-binding framework. Riverpod makes working with asynchronous code a breeze.
https://riverpod.dev
MIT License
5.82k stars 888 forks source link

Code generation InvalidType for Freezed union/sealed type #3454

Closed AlexeyKatsuro closed 1 month ago

AlexeyKatsuro commented 1 month ago

The Riverpod generator cannot infer the type and replace it with InvalidType for the Family argument if it is one of the subtypes generated with Freezed as a subtype of a sealed class .

To Reproduce

import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'model.freezed.dart';
part 'model.g.dart';

@freezed
sealed class Model with _$Model {
  factory Model.first(String a) = First;
  factory Model.second(int b, bool c) = Second;
}

@riverpod
Second sealedModel(SealedModelRef ref, {required First first}) {
  return Second(0, false);
}

Generates:

class SealedModelFamily extends Family<Second> {
  /// See also [sealedModel].
  const SealedModelFamily();

  /// See also [sealedModel].
  SealedModelProvider call({
    required InvalidType first,
  }) {
    return SealedModelProvider(
      first: first,
    );
  }
...
}

class SealedModelProvider extends AutoDisposeProvider<Second> {
  /// See also [sealedModel].
  SealedModelProvider({
    required InvalidType first,
  }) 
  ...
rrousselGit commented 1 month ago

This is a known limitation of codegeneration.

And 3.0.0 already has a workaround to this, so closing :)

AlexeyKatsuro commented 1 month ago

@rrousselGit, thank you for the quick response. I've tried the latest version, and it works as expected. Thank you!