我試圖將 Cubit 與相應的狀態類一起使用,如果我省略關鍵字 required,Android Studio 會在狀態類建構式中引發錯誤。我只是想了解為什么?
這是 counter_cubit.dart 中的代碼
class CounterCubit extends Cubit<CounterState> {
CounterCubit() : super(CounterState(currentValue: 0));
void increment() => emit(CounterState(currentValue: state.currentValue 1));
}
這是 counter_state.dart 中的代碼
class CounterState<int> {
int currentValue;
CounterState({required this.currentValue});
}
為什么在這個用例中建構式中需要 required 關鍵字?
我在 Android Studio v Arctic Fox 2020.3.1 中作業,使用 Flutter v2.5.3、Dart v2.14.4、flutter_bloc:^8.0.0 和 bloc:^8.0.0
謝謝
uj5u.com熱心網友回復:
這是因為currentValue沒有標記為可空。
你可以這樣做 int? currentValue;
另一個潛在的解決方案是更改您的建構式:
CounterState(this.currentValue); (注意缺少的花括號)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/395277.html
上一篇:洗掉這些物件的最有效方法
下一篇:從同一個父類的另一個類訪問物件
