Skip to content

Commit

Permalink
The getter 'isSubmissionFailure' isn't defined for the type 'FormzSta…
Browse files Browse the repository at this point in the history
…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
sultanmyrza committed Apr 11, 2021
1 parent e6ef2e2 commit ccfd672
Show file tree
Hide file tree
Showing 13 changed files with 309 additions and 91 deletions.
69 changes: 69 additions & 0 deletions lib/login/bloc/login_bloc.dart
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);
}
}
}
}
30 changes: 30 additions & 0 deletions lib/login/bloc/login_event.dart
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();
}
30 changes: 30 additions & 0 deletions lib/login/bloc/login_state.dart
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 added lib/login/login.dart
Empty file.
2 changes: 2 additions & 0 deletions lib/login/models/models.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export 'password.dart';
export 'username.dart';
13 changes: 13 additions & 0 deletions lib/login/models/password.dart
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;
}
}
13 changes: 13 additions & 0 deletions lib/login/models/username.dart
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;
}
}
18 changes: 18 additions & 0 deletions lib/login/view/login_form.dart
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) {}
},
);
}
}
35 changes: 35 additions & 0 deletions lib/login/view/login_page.dart
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(),
),
);
}
}

0 comments on commit ccfd672

Please sign in to comment.