'login authentication failure getting LateInitializationError: Field 'loadingBuilder' has not been initialized in flutter
LoginForm:
class LoginForm extends StatelessWidget {
TextEditingController name = TextEditingController();
TextEditingController pass_word = TextEditingController();
String? na;
String? pa;
@override
Widget build(BuildContext context) {
return BlocListener<LoginBloc, LoginState>(
listener: (context, state) {
if (state.status.isSubmissionFailure) {
ScaffoldMessenger.of(context)
..hideCurrentSnackBar()
..showSnackBar(
const SnackBar(content: Text('Authentication Failure')),
);
}
},
child: Align(
alignment: const Alignment(0, -1 / 3),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
BlocBuilder<LoginBloc, LoginState>(
buildWhen: (previous, current) =>
previous.username != current.username,
builder: (context, state) {
return TextField(
controller: name,
key: const Key('loginForm_usernameInput_textField'),
onChanged: (name) {
na = name;
context.read<LoginBloc>().add(LoginUsernameChanged(na!));
},
decoration: InputDecoration(
labelText: 'username',
// errorText:
// state.username.invalid ? 'invalid username' : null,
),
);
},
),
const Padding(padding: EdgeInsets.all(12)),
BlocBuilder<LoginBloc, LoginState>(
buildWhen: (previous, current) =>
previous.password != current.password,
builder: (context, state) {
return TextField(
controller: pass_word,
key: const Key('loginForm_passwordInput_textField'),
onChanged: (pass_word) {
pa = pass_word;
context.read<LoginBloc>().add(LoginPasswordChanged(pa!));
},
obscureText: true,
decoration: InputDecoration(
labelText: 'password',
errorText:
state.password.invalid ? 'invalid password' : null,
),
);
},
),
const Padding(padding: EdgeInsets.all(12)),
BlocBuilder<LoginBloc, LoginState>(
buildWhen: (previous, current) =>
previous.status != current.status,
builder: (context, state) {
return state.status.isSubmissionInProgress
? const CircularProgressIndicator()
: ElevatedButton(
key: const Key('loginForm_continue_raisedButton'),
child: const Text('Login'),
onPressed: state.status.isValidated
? () {
print(
'------------------>>>>>>>>>nananan--$na');
print(
'------------------>>>>>>>>>nananan--$pa');
context
.read<LoginBloc>()
.add(LoginSubmitted(na!, pa!));
}
: null,
);
},
)
],
Loginstate:
class LoginState extends Equatable {
const LoginState({
this.status = FormzStatus.pure,
this.username = const Username.pure(),
this.password = const Password.pure(),
});
final FormzStatus status;
final Username username;
final Password password;
LoginState copyWith({
FormzStatus? status,
Username? username,
Password? password,
}) {
return LoginState(
status: status ?? this.status,
username: username ?? this.username,
password: password ?? this.password,
);
}
@override
List<Object> get props => [status, username, password];
}
LoginEvent:
abstract class LoginEvent extends Equatable {
LoginEvent();
@override
List<Object> get props => [];
}
class LoginUsernameChanged extends LoginEvent {
LoginUsernameChanged(this.username);
final String username;
@override
List<Object> get props => [username];
}
class LoginPasswordChanged extends LoginEvent {
LoginPasswordChanged(this.password);
final String password;
@override
List<Object> get props => [password];
}
class LoginSubmitted extends LoginEvent {
final String username;
final String password;
LoginSubmitted(this.username, this.password);
@override
List<Object> get props => [username, password];
}
LoginBloc:
LoginBloc({
required AuthenticationRepository authenticationRepository,
}) : _authenticationRepository = authenticationRepository,
super(const LoginState()) {
on<LoginUsernameChanged>(_onUsernameChanged);
on<LoginPasswordChanged>(_onPasswordChanged);
on<LoginSubmitted>(_onSubmitted);
}
//final ApiRepository repository;
final AuthenticationRepository _authenticationRepository;
void _onUsernameChanged(
LoginUsernameChanged event,
Emitter<LoginState> emit,
) {
final username = Username.dirty(event.username);
emit(state.copyWith(
username: username,
status: Formz.validate([state.password, username]),
));
}
void _onPasswordChanged(
LoginPasswordChanged event,
Emitter<LoginState> emit,
) {
final password = Password.dirty(event.password);
emit(state.copyWith(
password: password,
status: Formz.validate([password, state.username]),
));
}
void _onSubmitted(
LoginSubmitted event,
Emitter<LoginState> emit,
) async {
if (state.status.isValidated) {
emit(state.copyWith(status: FormzStatus.submissionInProgress));
try {
await _authenticationRepository.logIn(
username: state.username.value,
password: state.password.value,
);
emit(state.copyWith(status: FormzStatus.submissionSuccess));
} catch (e) {
print(e);
emit(state.copyWith(status: FormzStatus.submissionFailure)); //---getting error in this line -------------------------
}
}
}
}
Authentication:
enum AuthenticationStatus { unknown, authenticated, unauthenticated }
class AuthenticationRepository {
final ApiRepository repository = ApiRepository(
apiClient: ApiClient(
httpClient: http.Client(),
),
);
final _controller = StreamController<AuthenticationStatus>();
// AuthenticationRepository(this.repository);
Stream<AuthenticationStatus> get status async* {
await Future<void>.delayed(const Duration(seconds: 1));
yield AuthenticationStatus.unauthenticated;
yield* _controller.stream;
}
Future<void> logIn({
required String username,
required String password,
}) async {
EmpVerifyEntity entity; //---Model class---------
entity = await repository.loginVerify(emp_id: username, password: password);
//-----api name loginverify----------------------
if (username == entity.employee.empId &&
password == entity.employee.password) {
Future.delayed(
const Duration(milliseconds: 300),
() => _controller.add(AuthenticationStatus.authenticated),
);
} else {
print('------------------------wdawdawdawdawdawdaw-------------------');
}
}
void logOut() {
_controller.add(AuthenticationStatus.unauthenticated);
}
void dispose() => _controller.close();
}
I'm getting formzStatus.submissionFailure so it shows auhentication failure, I need to hit api and verify the given data after that only it should authenticate but it is giving me an error before that , how to solve this error and make given data to hit api. ..............................---------------------Thank you-------------.................................
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
