我想使用一個從StateNotifier
in間接擴展的類StateNotifierProvider
,但它不起作用。
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”不符合型別引數“Notifier”的系結“StateNotifier<AsyncValue<List>>”。嘗試使用屬于或屬于“StateNotifier<AsyncValue<List>>”子類的型別。
BaseDogListController 中 state 的使用是它不直接從 StateNotifier 擴展的原因。
我該如何解決這個問題?
uj5u.com熱心網友回復:
問題是你如何定義你的提供者
它說它使用ShibaListController
– 有 for state 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> {}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/391684.html
上一篇:帶有未初始化引數錯誤的單例模式