class 从 StateNotifierProvider 中的 StateNotifier 间接扩展的方法不起作用

class that indirectly extends from StateNotifier in StateNotifierProvider doesn't work

我想在 StateNotifierProvider 中使用从 StateNotifier 间接扩展的 class,但它不起作用。

    import 'package:riverpod/riverpod.dart';
    
    abstract class BaseDog {}
    class Shiba extends BaseDog {}
    
    
    abstract class BaseDogListController
        extends StateNotifier<AsyncValue<List<BaseDog>>> {
      BaseDogListController() : super(const AsyncValue.loading());
    //doing something with state.
    }
    class ShibaListController extends BaseDogListController {}
    
    
    final shibaListControllerProvider =
        StateNotifierProvider<ShibaListController//←this code is error, AsyncValue<List<Shiba>>>(
            (_) => ShibaListController());

这里是输出:

'ShibaListController' doesn't conform to the bound 'StateNotifier<AsyncValue<List>>' of the type parameter 'Notifier'. Try using a type that is or is a subclass of 'StateNotifier<AsyncValue<List>>'.

BaseDogListController 中状态的使用是它不直接从 StateNotifier 扩展的原因。

我该如何解决这个问题?

问题在于您如何定义提供者

它说它使用 ShibaListController – 状态为 AsyncValue<List<BaseDog>>,但您告诉提供商它的状态定义为 AsyncValue<List<Shiba>>

这些类型不匹配。

您可能想要 BaseDogListController 通用:

abstract class BaseDogListController<DogType extends BaseDog>
    extends StateNotifier<AsyncValue<List<DogType>>> {
  BaseDogListController() : super(const AsyncValue.loading());
//doing something with state.
}

class ShibaListController extends BaseDogListController<Shiba> {}