-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: async validation support #61
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
13d20c5
Added async formz
jopmiddelkamp 9ccabb4
Naming improvement
jopmiddelkamp c7ce923
Updated for a better version with extra control of when the validatio…
jopmiddelkamp 0762086
Improved the async example
jopmiddelkamp 1472b84
Renamed copyWithErrorReset to copyWithResetError
jopmiddelkamp 3557684
Added comments to the public apis
jopmiddelkamp 6300e73
Added tests for the async additions
jopmiddelkamp d8db87b
Analyzer fixes
jopmiddelkamp cfb94c7
Added some async description to the readme
jopmiddelkamp 2a84427
Fixed analyzer issues
jopmiddelkamp 2f77736
Added one more test to get code coverage to 100%
jopmiddelkamp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,27 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "formz", | ||
"request": "launch", | ||
"type": "dart" | ||
}, | ||
{ | ||
"name": "async_example", | ||
"cwd": "example", | ||
"request": "launch", | ||
"type": "dart", | ||
"program": "lib/async/main.dart" | ||
}, | ||
{ | ||
"name": "sync_example", | ||
"cwd": "example", | ||
"request": "launch", | ||
"type": "dart", | ||
"program": "lib/sync/main.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
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,8 @@ | ||
class EmailRepository { | ||
const EmailRepository(); | ||
|
||
Future<bool> findByAddress(String emailAddress) async { | ||
await Future<void>.delayed(const Duration(seconds: 1)); | ||
return true; | ||
} | ||
} |
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,6 @@ | ||
import 'package:example/async/ui/app.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
void main() { | ||
runApp(const App()); | ||
} |
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,19 @@ | ||
import 'package:example/async/data/repositories/email_repository.dart'; | ||
import 'package:example/async/ui/feature/register/view/register_page.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:provider/provider.dart'; | ||
|
||
class App extends StatelessWidget { | ||
const App({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Provider.value( | ||
value: const EmailRepository(), | ||
child: const MaterialApp( | ||
title: 'Example', | ||
home: RegisterPage(), | ||
), | ||
); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
example/lib/async/ui/feature/register/cubit/register_cubit.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,78 @@ | ||
import 'package:example/async/ui/feature/register/cubit/register_state.dart'; | ||
import 'package:example/async/ui/shared/forms/validators/amount_validator.dart'; | ||
import 'package:example/async/ui/shared/forms/validators/email_validator.dart'; | ||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
import 'package:formz/formz.dart'; | ||
|
||
class RegisterCubit extends Cubit<RegisterState> { | ||
RegisterCubit({ | ||
required AmountValidator amountValidator, | ||
required EmailValidator emailValidator, | ||
}) : _amountValidator = amountValidator, | ||
_emailValidator = emailValidator, | ||
super(RegisterState()); | ||
|
||
final AmountValidator _amountValidator; | ||
final EmailValidator _emailValidator; | ||
|
||
Future<void> amountChanged(String value) async { | ||
emit( | ||
state.copyWith( | ||
amount: state.amount.copyWithResetError( | ||
value: value, | ||
validationStatus: AsyncFormzInputValidationStatus.validating, | ||
), | ||
), | ||
); | ||
final error = await _amountValidator.validate(state.amount); | ||
emit( | ||
state.copyWith( | ||
amount: state.amount.copyWith( | ||
error: error, | ||
validationStatus: AsyncFormzInputValidationStatus.validated, | ||
), | ||
), | ||
); | ||
_updateFormState(); | ||
} | ||
|
||
Future<void> emailChanged(String value) async { | ||
emit( | ||
state.copyWith( | ||
email: state.email.copyWithResetError(value: value), | ||
), | ||
); | ||
final canValidate = _emailValidator.canValidate(state.email); | ||
if (!canValidate) { | ||
return; | ||
} | ||
emit( | ||
state.copyWith( | ||
email: state.email.copyWith( | ||
validationStatus: AsyncFormzInputValidationStatus.validating, | ||
), | ||
), | ||
); | ||
final error = await _emailValidator.validate(state.email); | ||
emit( | ||
state.copyWith( | ||
email: state.email.copyWith( | ||
error: error, | ||
validationStatus: AsyncFormzInputValidationStatus.validated, | ||
), | ||
), | ||
); | ||
_updateFormState(); | ||
} | ||
|
||
void _updateFormState() { | ||
emit( | ||
state.copyWith( | ||
isFormValid: Formz.validate([ | ||
state.amount, | ||
state.email, | ||
]), | ||
), | ||
); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
example/lib/async/ui/feature/register/cubit/register_state.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,14 @@ | ||
import 'package:example/async/ui/shared/forms/inputs/amount_input.dart'; | ||
import 'package:example/async/ui/shared/forms/inputs/email_input.dart'; | ||
import 'package:freezed_annotation/freezed_annotation.dart'; | ||
|
||
part 'register_state.freezed.dart'; | ||
|
||
@freezed | ||
class RegisterState with _$RegisterState { | ||
factory RegisterState({ | ||
@Default(false) bool isFormValid, | ||
@Default(Amount('', max: 0.5)) Amount amount, | ||
@Default(Email('')) Email email, | ||
}) = _RegisterState; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I appreciate the example app from a more "real world usage" point of view, but it defeats the purpose of the example of allowing people to understand what a package API looks like when used.