超类 'Bloc<Event, State>' 没有零参数构造函数
The superclass 'Bloc<Event, State>' doesn't have a zero argument constructor
如果我创建一个构造函数来调用 super,我也必须将一个参数传递给 BlocProvider 的创建 属性。我不知道怎么处理。
CounterBloc(CounterState initialState) : super(initialState);
是否需要以下代码?
@覆盖
CounterState get initialState => ShowCounterState(counterValue);
提前感谢您的帮助。
您不应覆盖 initialState
。如果您想在 Bloc
class 中指定初始状态,同时具有零参数构造函数,构造函数,请执行以下操作:
CounterBloc() : super(ShowCounterState(0));
或者,使用第 1 点中的构造函数,并在实例化 Bloc 时传递值,如下所示:
final yourBloc = CounterBloc(ShowCounterState(0));
如果我创建一个构造函数来调用 super,我也必须将一个参数传递给 BlocProvider 的创建 属性。我不知道怎么处理。
CounterBloc(CounterState initialState) : super(initialState);
是否需要以下代码?
@覆盖 CounterState get initialState => ShowCounterState(counterValue);
提前感谢您的帮助。
您不应覆盖 initialState
。如果您想在 Bloc
class 中指定初始状态,同时具有零参数构造函数,构造函数,请执行以下操作:
CounterBloc() : super(ShowCounterState(0));
或者,使用第 1 点中的构造函数,并在实例化 Bloc 时传递值,如下所示:
final yourBloc = CounterBloc(ShowCounterState(0));