Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

flutter - The superclass 'Bloc<Event, State>' doesn't have a zero argument constructor

enter image description here

  1. If I create a constructor to call super, I have to pass an argument to the BlocProvider's create property too. I don't know how to handle this.

    CounterBloc(CounterState initialState) : super(initialState);

  2. If the below code is necessary for the bloc?

    @override CounterState get initialState => ShowCounterState(counterValue);

Appreciate your help in advance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You shouldn't override initialState. If you'd like to specify the initial state in your Bloc class while having a zero-argument constructor, constructor, then do something like this:

CounterBloc() : super(ShowCounterState(0));

Alternatively, use the constructor in your point #1, and pass the value when instantiating the Bloc, like so:

final yourBloc = CounterBloc(ShowCounterState(0));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...