bloc
bloc copied to clipboard
issue: type 'Null' is not a subtype of type '<BLOC_NAME>' in type cast, relate to repository injection
Hi everyone at the great state management ! I did refactor my code from #photo1 to #photo2, and I receive Error: "type 'Null' is not a subtype of type 'LoginBloc' in type cast".
The named "create" function in BlocProvider did not call. For my part, I see they are similar but flutter_bloc said no. How do they work or effect to Flutter tree then create this difference ?
Thank you so much and happy coding !
#photo1
#photo2
These two codes will not behave equally. The closure passed "create" is, by default, executed lazily, which makes, in the first code snippet, the repository instances to be instantiated only when referenced for the first time. But in the refactored code, these same instancies are created eagerly.
But the interesting part is that "LoginBloc" (as described in the error message) is not part of the refactored code.
These two codes will not behave equally. The closure passed "create" is, by default, executed lazily, which makes, in the first code snippet, the repository instances to be instantiated only when referenced for the first time. But in the refactored code, these same instancies are created eagerly.
But the interesting part is that "LoginBloc" (as described in the error message) is not part of the refactored code.
I don't think the refactored code make them are created eagerly, buddy. But maybe, I guess constructor of RepositoryProvider did change a global thing
Hi @phamducmanh1452001
I face the same problem today. So I solve them by wrapping Multi Bloc Provider in Multi Repository Provider.
The flutter_login demo has this problem :
type 'Null' is not a subtype of type 'LoginBloc' in type cast
@phamducmanh1452001 can you please provide a link to a minimal reproduction sample? This isn't enough information to help unfortunately.
I am getting the same error with flutter_login demo
I am getting the same error with flutter_login demo
I'm not able to reproduce. Can you please provide clear reproduction steps along with the full error and stacktrace? Thanks!
Closing for now since I can’t reproduce and there is no reproduction sample. Feel free to provide one and I’m happy to take another look 👍
@felangel Hi bud, I am facing the same error i am sharing the traceback
this is the logi_page.dart file snippet
I have imported login_form.dart since the LoginForm widget needed it
import 'dart:async';
import 'package:authentication_repository/authentication_repository.dart';
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter_login/login/models/models.dart';
import 'package:formz/formz.dart';
part 'login_event.dart';
part 'login_state.dart';
class LoginBloc extends Bloc<LoginEvent, LoginState> {
LoginBloc({
required AuthenticationRepository authenticationRepository,
}) : _authenticationRepository = authenticationRepository,
super(const LoginState()) {
on<LoginUsernameChanged>(_onUsernameChanged);
on<LoginPasswordChanged>(_onPasswordChanged);
on<LoginSubmitted>(_onSubmitted);
}
final AuthenticationRepository _authenticationRepository;
void _onUsernameChanged(
LoginUsernameChanged event,
Emitter<LoginState> emit,
) {
final username = Username.dirty(event.username);
emit(
state.copyWith(
username: username,
isValid: Formz.validate([state.password, username]),
),
);
}
void _onPasswordChanged(
LoginPasswordChanged event,
Emitter<LoginState> emit,
) {
final password = Password.dirty(event.password);
emit(
state.copyWith(
password: password,
isValid: Formz.validate([password, state.username]),
),
);
}
Future<void> _onSubmitted(
LoginSubmitted event,
Emitter<LoginState> emit,
) async {
if (!state.isValid) {
return;
}
emit(state.copyWith(status: FormzSubmissionStatus.inProgress));
try {
await _authenticationRepository.logIn(
username: state.username.value,
password: state.password.value,
);
emit(state.copyWith(status: FormzSubmissionStatus.success));
} catch (_) {
emit(state.copyWith(status: FormzSubmissionStatus.failure));
}
}
}
this is login_bloc.dart