从 StateNotifierProvider 中的 StateNotifier 间接扩展的类不起作用



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

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());

这里是输出:

'hibaListController'不符合绑定的'StateNotifier<异步值<列表>gt;'类型参数"Notifier"的。尝试使用属于或属于StateNotifier<异步值<列表>gt;'。

在BaseDogListController中使用state是它不直接从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> {}

最新更新