超类'Bloc< xxx,xxx>'dart中没有零参数构造函数 [英] The superclass 'Bloc<xxx, xxx>' doesn't have a zero argument constructor in dart
问题描述
我是Dart语言开发的初学者.我尝试创建受
编辑
我的pubspec.yaml依赖项如下:
您似乎需要为集团提供初始状态.像这样:
Class UserRegBloc扩展了Bloc< UserRegEvent,UserRegState>{UserRepository userRepository;UserRegBloc({@ required UserRepository userRepository}):super(UserRegInitialState()){userRepository = UserRepository();}//...}
其中 UserRegInitialState
是 UserRegState
的子类.
这是由于bloc库版本不同而引起的差异.您的链接项目使用的是flutter_bloc 3.0版,问题中相同软件包的版本是6.1.1.因此,代码之间的差异很小.该错误基本上表明 UserRegBloc
的基类需要一个参数.因此,如果不提供该参数就无法声明子类,从而导致错误.
I am a beginner in Dart language development. I try to create a sample flutter application BLOC pattern inspired by this GitHub repo, but I got some error message related to the class inheritance. I am already familiar with the inheritance and superclass and subclass programming in the dot net C# language. But in the case of the dart, I need some advice.
Here is my code:
class UserRegBloc extends Bloc<UserRegEvent, UserRegState> {
UserRepository userRepository;
UserRegBloc({@required UserRepository userRepository}) {
userRepository = UserRepository();
}
@override
UserRegState get initialState => UserRegInitial();
@override
Stream<UserRegState> mapEventToState(UserRegEvent event) async* {
if (event is SignUpButtonPressed) {
yield UserRegLoading();
try {
var user = await userRepository.signUpUserWithEmailPass(
event.email, event.password);
print("BLOC : ${user.email}");
yield UserRegSuccessful(user: user);
} catch (e) {
yield UserRegFailure(message: e.toString());
}
}
}
}
Edit
My pubspec.yaml dependencies are below:
It looks like you need to provide an initial state to the bloc. Something like this:
class UserRegBloc extends Bloc<UserRegEvent, UserRegState> {
UserRepository userRepository;
UserRegBloc({@required UserRepository userRepository}) : super(UserRegInitialState()) {
userRepository = UserRepository();
}
// ...
}
where UserRegInitialState
is a subclass of UserRegState
.
This is the difference due to different versions of the bloc library. Your link project uses flutter_bloc version 3.0 and the version of the same package in your question is 6.1.1. So there is a small difference in code. The error, basically, tells that the base class of UserRegBloc
requires one parameter. So you couldn't declare a subclass without providing that parameter, thus the error.
这篇关于超类'Bloc< xxx,xxx>'dart中没有零参数构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!