What is a zero-argument constructor in Dart flutter?
while creating a bloc in flutter I am receiving the following error
The superclass 'Bloc<QuoteEvent, QuoteState>' doesn't have a zero argument constructor.
Try declaring a zero argument constructor in 'Bloc<QuoteEvent, QuoteState>', or explicitly invoking a different constructor in 'Bloc<QuoteEvent, QuoteState>'.
kindly guide how to fix it. thanks
below is code
import 'package:meta/meta.dart';
import 'package:bloc/bloc.dart';
import 'package:random_quote/repositories/repositories.dart';
import 'package:random_quote/models/models.dart';
import 'package:random_quote/bloc/bloc.dart';
class QuoteBloc extends Bloc<QuoteEvent, QuoteState> {
final QuoteRepository repository;
QuoteBloc({@required this.repository}) : assert(repository != null);
@override
QuoteState get initialState => QuoteEmpty();
@override
Stream<QuoteState> mapEventToState(QuoteEvent event) async* {
if (event is FetchQuote) {
yield QuoteLoading();
try {
final Quote quote = await repository.fetchQuote();
yield QuoteLoaded(quote: quote);
} catch (_) {
yield QuoteError();
}
}
}
}
initialState
property was removed from flutter_bloc since v5.0.0. Here is migration guide.
You should use super() constructor instead:
class QuoteBloc extends Bloc<QuoteEvent, QuoteState> {
final QuoteRepository repository;
QuoteBloc({@required this.repository}) :
assert(this.repository != null),
super(QuoteEmpty());
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With