flutter_bloc_network 1.0.4

  • Readme
  • Changelog
  • Example
  • Installing
  • 71

flutter_bloc_network #

This package helps to manage network states used with flutter_bloc by wrapping the loaded content into a NetworkState

Sample #

Check out the sample project that shows how to load users async and show the different NetworkStates

Getting Started #

Basic Usage #

To use this plugin, add flutter_bloc_network as a dependency in your pubspec.yaml file.

Bloc #

In your initial state

  NetworkState<MyObject> myObject = NetworkStateUninitialized();

In Bloc -> mapEventToState

  yield currentState.copyWith({
    myObject: NetworkStateLoading();
  });
  try {
    MyObject result = await doNetworkStuff();
    yield currentState.copyWith({
      myObject: NetworkStateSucceeded(result);
    });
  } catch(e) {
    yield currentState.copyWith({
      myObject: NetworkStateFailed(e);
    });
  }

Widget #

BlocNetworBuilder<MyBloc, MyState, MyEvent, MyObject>(
  getValue: (state) => state.myObject,
  loading: MyLoadingWidget(),
  initializeEvent: LoadMyObjectEvent(),
  error: MyErrorWidget(),
  builder: (BuildContext context, MyObject value) => MyWidget(value);
)

properties

Widget loading;                           // This widget gets shown if the state is NetworkStateLoading
Widget uninitialized;                     // this widget gets shown if state is NetworkStateUninitialized, if its not set the loading widget gets shown
bool animateSwitch;                       // Use AnimatedSwitcher on top
StateWidgetBuilder<T> builder;            // Builder gets called if state is NetworkStateSucceded
E initializeEvent;                          // this event gets dispatched if state is NetworkStateUninitialized
StateValueGetter<S, T> getValue;          // get the actual value from the BlocState
Widget error;                             // This widget gets shown if the state is NetworkStateFailed
Function(BuildContext, T) onSucceeded;    // This function gets called if the state changed to NetworkStateSucceeded
Function(BuildContext, dynamic) onFailed; // This function gets called if the state changed to NetworkStateFailed

Usage with custom bloc or without bloc #

You can use the NetworkStateWidget to use NetworkState without BlocBuilder. So you can use your own BlocBuilder or use it in different situations where you dont want a BlocBuilder.

NetworkStateWidget #

NetworkStateWidget<MyObject>(
  state: myNetworkStateObject,
  loading: MyLoadingWidget(),
  error: MyErrorWidget(),
  builder: (BuildContext context, MyObject value) => MyWidget(value),
  initialize: () {
    myService.startLoadingMyObject();
  }
)
NetworkState<T> state;          // the NetworkState object
Widget loading;                 // This widget gets shown if the state is
Widget error;                   // This widget gets shown if the state is NetworkStateFailed
bool animateSwitch;             // Use AnimatedSwitcher on top
Widget uninitialized;           // this widget gets shown if state is NetworkStateUninitialized, if its not set the loading widget gets shownStateWidgetBuilder<T> builder;  // Builder gets called if state is NetworkStateSucceded
Function initialize;            // this function gets called if state is NetworkStateUninitialized

NetworkState without data #

If you wanna represent a NetworkCall that has no response/data you can simply pass void as generic type and pass null for your NetworkStateSucceeded

Keep in mind to not use the passed value in your builder or onSucceeded Listener.

Check the sample project to see the usage.

In your initial state

NetworkState<void> callWithoutResponse = NetworkStateUninitialized();

In Bloc -> mapEventToState

yield currentState.copyWith(
  callWithoutResponse: NetworkStateLoading(),
);
try {
  await _userService.createUser(event.username, event.age);
  yield currentState.copyWith(
    callWithoutResponse: NetworkStateSucceeded(null),
  );
} catch(e) {
  yield currentState.copyWith(
    callWithoutResponse: NetworkStateFailed(e),
  );
}

in your widget

BlocNetworkBuilder<MyBloc, MyState, MyEvent, void>(
  getValue: (state) => state.callWithoutResponse,
  builder: (context, value) => MySucceededWidget(), // dont use value (its null)
  loading: MyLoadingWidget(),
  error: MyErrorWidget(),
  initializeEvent: DoMyNetworkCallEvent(),
)

1.0.4 - 12.1.2019 #

  • Updated flutter_bloc & bloc to 2.0.0

1.0.3 - 22.10.2019 #

  • Updated flutter_bloc & bloc to 1.0.0

1.0.2 - 13.10.2019 #

  • Used add instead of deprecated dispatch for bloc event call

1.0.1 - 13.10.2019 #

  • Updated dependency

1.0.0 - 13.10.2019 #

  • Added initial functionallity

example/README.md

flutter_bloc_network_sample #

A sample using flutter_bloc_network

Getting Started #

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

Use this package as a library

1. Depend on it

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


dependencies:
  flutter_bloc_network: ^1.0.4

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support 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:flutter_bloc_network/flutter_bloc_network.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
43
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
71
Learn more about scoring.

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

  • Dart: 2.6.1
  • pana: 0.13.1+4
  • Flutter: 1.12.13+hotfix.2

Health suggestions

Format lib/generated/i18n.dart.

Run flutter format to format lib/generated/i18n.dart.

Format lib/widgets/NetworkStateWidget.dart.

Run flutter format to format lib/widgets/NetworkStateWidget.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
bloc ^2.0.0 2.0.0 3.0.0-dev.1
flutter 0.0.0
flutter_bloc ^2.0.0 2.1.1 3.0.0-dev.1
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
provider 3.2.0 4.0.0-dev
rxdart 0.22.6 0.23.0-dev.3
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test