Flutter Dart颤振中的零参数构造函数是什么
Dart颤振中的零参数构造函数是什么 在flatter中创建一个bloc时,我收到以下错误Flutter Dart颤振中的零参数构造函数是什么,flutter,dart,bloc,Flutter,Dart,Bloc,Dart颤振中的零参数构造函数是什么 在flatter中创建一个bloc时,我收到以下错误 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
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>'.
请指导如何修复它。谢谢
下面是代码
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();
}
}
}
}
从v5.0.0起已从中删除initialState属性。这是
您应该改用超级构造函数:
class QuoteBloc extends Bloc<QuoteEvent, QuoteState> {
final QuoteRepository repository;
QuoteBloc({@required this.repository}) :
assert(this.repository != null),
super(QuoteEmpty());
...
从v5.0.0起已从中删除initialState属性。这是
您应该改用超级构造函数:
class QuoteBloc extends Bloc<QuoteEvent, QuoteState> {
final QuoteRepository repository;
QuoteBloc({@required this.repository}) :
assert(this.repository != null),
super(QuoteEmpty());
...
零参数构造函数实际上是可以用零参数调用的构造函数。这包括只接受可选参数的构造函数 如果派生类没有显式调用其基类构造函数,则派生类的构造函数将以零参数隐式调用基类中的默认未命名构造函数。如果需要在基类中调用命名构造函数,或者需要传递参数,则需要显式调用基类构造函数
在您的例子中,QuoteBloc派生自Bloc,但QuoteBloc构造函数没有显式调用Bloc中的任何构造函数,这显然没有提供默认的零参数构造函数。零参数构造函数实际上是可以使用零参数调用的构造函数。这包括只接受可选参数的构造函数 如果派生类没有显式调用其基类构造函数,则派生类的构造函数将以零参数隐式调用基类中的默认未命名构造函数。如果需要在基类中调用命名构造函数,或者需要传递参数,则需要显式调用基类构造函数
在您的例子中,QuoteBloc派生自Bloc,但QuoteBloc构造函数没有显式调用Bloc中的任何构造函数,这显然没有提供默认的零参数构造函数。请提供Bloc类的代码示例。您是否使用Flatter\u bloc软件包?@谢谢,我已经添加了bloc代码是的,我正在使用Flatter\u bloc软件包请提供您的bloc类的代码示例。你使用flutter\u bloc软件包吗?@谢谢,我已经添加了bloc代码是的,我正在使用flutter\u bloc软件包