-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The getter 'isSubmissionFailure' isn't defined for the type 'FormzSta…
…tus'. Try importing the library that defines 'isSubmissionFailure', correcting the name to the name of an existing getter, or defining a getter or field named 'isSubmissionFailure'
- Loading branch information
1 parent
e6ef2e2
commit ccfd672
Showing
13 changed files
with
309 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:authentication_repository/authentication_repository.dart'; | ||
import 'package:bloc/bloc.dart'; | ||
import 'package:bloc_complete_practice/login/models/models.dart'; | ||
import 'package:equatable/equatable.dart'; | ||
import 'package:formz/formz.dart'; | ||
|
||
part 'login_event.dart'; | ||
part 'login_state.dart'; | ||
|
||
class LoginBloc extends Bloc<LoginEvent, LoginState> { | ||
final AuthenticationRepository _authenticationRepository; | ||
|
||
LoginBloc({ | ||
required AuthenticationRepository authenticationRepository, | ||
}) : _authenticationRepository = authenticationRepository, | ||
super(const LoginState()); | ||
|
||
@override | ||
Stream<LoginState> mapEventToState( | ||
LoginEvent event, | ||
) async* { | ||
if (event is LoginUsernameChanged) { | ||
yield _mapLoginUsernameChangedToState(event, state); | ||
} else if (event is LoginPasswordChanged) { | ||
yield _mapLoginPasswordChangedToState(event, state); | ||
} else if (event is LoginFormSubmitted) { | ||
yield* _mapLoginFormSubmittedToState(event, state); | ||
} | ||
} | ||
|
||
LoginState _mapLoginUsernameChangedToState( | ||
LoginUsernameChanged event, | ||
LoginState state, | ||
) { | ||
final username = Username.dirty(event.username); | ||
return state.copyWith( | ||
username: username, status: Formz.validate([username, state.password])); | ||
} | ||
|
||
LoginState _mapLoginPasswordChangedToState( | ||
LoginPasswordChanged event, | ||
LoginState state, | ||
) { | ||
final password = Password.dirty(event.password); | ||
return state.copyWith( | ||
password: password, status: Formz.validate([password, state.username])); | ||
} | ||
|
||
Stream<LoginState> _mapLoginFormSubmittedToState( | ||
LoginFormSubmitted event, | ||
LoginState state, | ||
) async* { | ||
if (state.status.isValidated) { | ||
yield state.copyWith(status: FormzStatus.submissionInProgress); | ||
try { | ||
await _authenticationRepository.logIn( | ||
username: state.username.value, | ||
password: state.password.value, | ||
); | ||
yield state.copyWith(status: FormzStatus.submissionSuccess); | ||
} catch (e) { | ||
print(e.toString()); | ||
yield state.copyWith(status: FormzStatus.submissionFailure); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
part of 'login_bloc.dart'; | ||
|
||
abstract class LoginEvent extends Equatable { | ||
const LoginEvent(); | ||
|
||
@override | ||
List<Object> get props => []; | ||
} | ||
|
||
class LoginUsernameChanged extends LoginEvent { | ||
final String username; | ||
|
||
const LoginUsernameChanged(this.username); | ||
|
||
@override | ||
List<Object> get props => [username]; | ||
} | ||
|
||
class LoginPasswordChanged extends LoginEvent { | ||
final String password; | ||
|
||
const LoginPasswordChanged(this.password); | ||
|
||
@override | ||
List<Object> get props => [password]; | ||
} | ||
|
||
class LoginFormSubmitted extends LoginEvent { | ||
const LoginFormSubmitted(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
part of 'login_bloc.dart'; | ||
|
||
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]; | ||
} | ||
|
||
class LoginInitial extends LoginState {} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export 'password.dart'; | ||
export 'username.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import 'package:formz/formz.dart'; | ||
|
||
enum PasswordValidationError { empty } | ||
|
||
class Password extends FormzInput<String, PasswordValidationError> { | ||
const Password.pure() : super.pure(''); | ||
const Password.dirty([String value = '']) : super.dirty(value); | ||
|
||
@override | ||
PasswordValidationError? validator(String? value) { | ||
return value?.isNotEmpty == true ? null : PasswordValidationError.empty; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import 'package:formz/formz.dart'; | ||
|
||
enum UsernameValidationError { empty } | ||
|
||
class Username extends FormzInput<String, UsernameValidationError> { | ||
const Username.pure() : super.pure(''); | ||
const Username.dirty([String value = '']) : super.dirty(value); | ||
|
||
@override | ||
UsernameValidationError? validator(String? value) { | ||
return value?.isNotEmpty == true ? null : UsernameValidationError.empty; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import 'package:bloc_complete_practice/login/bloc/login_bloc.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
|
||
/* | ||
* The LoginForm handles notifying the LoginBloc of user events and | ||
* also responds to state changes using BlocBuilder and BlocListener. | ||
*/ | ||
class LoginForm extends StatelessWidget { | ||
@override | ||
Widget build(BuildContext context) { | ||
return BlocListener<LoginBloc, LoginState>( | ||
listener: (context, state) { | ||
if (state.status.isSubmissionFailure) {} | ||
}, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import 'package:authentication_repository/authentication_repository.dart'; | ||
import 'package:bloc_complete_practice/login/bloc/login_bloc.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
|
||
import 'login_form.dart'; | ||
|
||
/* | ||
* The LoginPage is responsible for exposing the Route as well as | ||
* creating and providing the LoginBloc to the LoginForm | ||
*/ | ||
class LoginPage extends StatelessWidget { | ||
static Route<void> route() { | ||
return MaterialPageRoute( | ||
builder: (context) { | ||
return LoginPage(); | ||
}, | ||
); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar(title: const Text("Login")), | ||
body: BlocProvider( | ||
create: (context) { | ||
return LoginBloc( | ||
authenticationRepository: | ||
RepositoryProvider.of<AuthenticationRepository>(context)); | ||
}, | ||
child: LoginForm(), | ||
), | ||
); | ||
} | ||
} |
Oops, something went wrong.