The Superclass 'Bloc' Doesn't Have A Zero Argument Constructor In Dart
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:
Answer
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.
Related Questions
- → How do you create a 12 or 24 mnemonics code for multiple cryptocurrencies (ETH, BTC and so on..)
- → Flutter: input text field don't work properly in a simple example..... where am I wrong?
- → Can I customize the code formatting of Dart code in Atom?
- → Is it possible to develop iOS apps with Flutter on a Linux virtual machine?
- → Display SnackBar in Flutter
- → JSON ObjectMapper in Flutter
- → Material flutter app source code
- → TabBarSelection No such method error
- → How do I set the animation color of a LinearProgressIndicator?
- → Add different routes/screens to Flutter app
- → Is there a way to get the size of an existing widget?
- → How to share a file using flutter
- → Is there an easy way to find particular text built from RichText in a Flutter test?