form_bloc 0.2.0

form_bloc #

Pub

A dart package that helps to create forms with BLoC pattern using bloc package without writing a lot of boilerplate code.


To see complex examples check flutter_form_bloc.


Before to use this package you need to know the core concepts of bloc package.


Example #

dependencies:
  form_bloc: ^0.2.0
import 'package:form_bloc/form_bloc.dart';

class LoginFormBloc extends FormBloc<String, String> {
  final emailField = TextFieldBloc<String>(
    validators: [Validators.validEmail],
  );
  final passwordField = TextFieldBloc<String>(
    validators: [Validators.notEmpty],
  );

  @override
  List<FieldBloc> get fieldBlocs => [emailField, passwordField];

  @override
  Stream<FormBlocState<String, String>> onSubmitting() async* {
    // Login logic...
    await Future<void>.delayed(Duration(seconds: 2));
    yield currentState.toSuccess();
  }
}

Basic usage #

1. Import it #

import 'package:form_bloc/form_bloc.dart';

2. Create a class that extends FormBloc<SuccessResponse, FailureResponse>

FormBloc<SuccessResponse, FailureResponse>

SuccessResponse The type of the success response.

FailureResponse The type of the failure response.

For example, the SuccessResponse type and FailureResponse type of LoginFormBloc will be String

import 'package:form_bloc/form_bloc.dart';

class LoginFormBloc extends FormBloc<String, String> {}

2. Create Field Blocs #

You need to create field blocs, and these need to be final.

You can create:

For example the LoginFormBloc will have two TextFieldBloc<String>, so the Error type will be String, and the validators must return a error of String type.

import 'package:form_bloc/form_bloc.dart';

class LoginFormBloc extends FormBloc<String, String> {
  final emailField = TextFieldBloc<String>(
    validators: [Validators.validEmail],
  );
  final passwordField = TextFieldBloc<String>(
    validators: [Validators.notEmpty],
  );

}

3. Implement the get method fieldBlocs #

You need to override the get method fieldBlocs and return a list with all FieldBlocs.

For example the LoginFormBloc need to return a List with emailField and passwordField.

import 'package:form_bloc/form_bloc.dart';

class LoginFormBloc extends FormBloc<String, String> {
  final emailField = TextFieldBloc<String>(
    validators: [Validators.validEmail],
  );
  final passwordField = TextFieldBloc<String>(
    validators: [Validators.notEmpty],
  );

  @override
  List<FieldBloc> get fieldBlocs => [emailField, passwordField];

}

4. Implement the method onSubmitting #

onSubmitting return a Stream<FormBlocState<SuccessResponse, FailureResponse>> and will called when the form is submitting.

You must call all your business logic of this form here, and yield the corresponding state.

You can yield a new state using:

For example onSubmitting of LoginFormBloc will return a Stream<FormBlocState<String, String>> and yield currentState.toSuccess().

import 'package:form_bloc/form_bloc.dart';

class LoginFormBloc extends FormBloc<String, String> {
  final emailField = TextFieldBloc<String>(
    validators: [Validators.validEmail],
  );
  final passwordField = TextFieldBloc<String>(
    validators: [Validators.notEmpty],
  );

  @override
  List<FieldBloc> get fieldBlocs => [emailField, passwordField];

  @override
  Stream<FormBlocState<String, String>> onSubmitting() async* {
    // Login logic...
    await Future<void>.delayed(Duration(seconds: 2));
    yield currentState.toSuccess();
  }
}

Credits #

This package uses the following packages:

0.2.0 #

Initial Version of the library.

example/main.dart

import 'package:form_bloc/form_bloc.dart';

class LoginFormBloc extends FormBloc<String, String> {
  final emailField = TextFieldBloc<String>(
    validators: [Validators.validEmail],
  );
  final passwordField = TextFieldBloc<String>(
    validators: [Validators.notEmpty],
  );

  @override
  List<FieldBloc> get fieldBlocs => [emailField, passwordField];

  @override
  Stream<FormBlocState<String, String>> onSubmitting() async* {
    // Login logic...
    await Future<void>.delayed(Duration(seconds: 2));
    yield currentState.toSuccess();
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  form_bloc: ^0.2.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:form_bloc/form_bloc.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
39
Health:
Code health derived from static analysis. [more]
97
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
67
Learn more about scoring.

We analyzed this package on Aug 21, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:form_bloc/form_bloc.dart.

Health suggestions

Fix lib/src/blocs/boolean_field/boolean_field_state.dart. (-1 points)

Analysis of lib/src/blocs/boolean_field/boolean_field_state.dart reported 2 hints:

line 36 col 7: DO use curly braces for all flow control structures.

line 38 col 7: DO use curly braces for all flow control structures.

Fix lib/src/blocs/select_field/select_field_state.dart. (-1 points)

Analysis of lib/src/blocs/select_field/select_field_state.dart reported 2 hints:

line 49 col 7: DO use curly braces for all flow control structures.

line 51 col 7: DO use curly braces for all flow control structures.

Fix lib/src/blocs/text_field/text_field_state.dart. (-1 points)

Analysis of lib/src/blocs/text_field/text_field_state.dart reported 2 hints:

line 51 col 7: DO use curly braces for all flow control structures.

line 53 col 7: DO use curly braces for all flow control structures.

Fix lib/src/blocs/form/form_state.dart. (-0.50 points)

Analysis of lib/src/blocs/form/form_state.dart reported 1 hint:

line 58 col 7: DO use curly braces for all flow control structures.

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (bloc).

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
bloc ^0.14.4 0.14.4 0.15.0
equatable ^0.4.0 0.4.0
meta ^1.1.6 1.1.7
rxdart ^0.22.1+1 0.22.1+1
Transitive dependencies
collection 1.14.12
Dev dependencies
test ^1.6.0