updater_queue_bloc 1.0.1

  • Readme
  • Changelog
  • Installing
  • 50

updater_queue_bloc #

A simple BLoC base class that runs async updater functions sequentially.

This simplifies state management by ensuring that only a single updater function is running at any time.

Updaters can emit multiple new state values, for example a data loading BLoC can emit a LoadingState initially, then await the completion of an HTTP request, and finally emit a LoadedState with the received data.

Example usage #

import 'package:updater_queue_bloc/updater_queue_bloc.dart';

class CounterBloc extends UpdaterQueue<int> {
  CounterBloc() : super(0);

  Future<void> increment() async {
    await map((count) => count + 1);
  }

  Future<void> addNTimes(int n) async {
    await expand(
      (count) async* {
        for (var i = 0; i < n; i++) {
          yield count + n;
        }
      },
    );
  }
}

State Type #

The state type is not always a simple primitive like int in the example above.

Let's image we have a BLoC backing a news article widget, which handles navigating to other articles by invoking a openArticle method.

For this case a shared abstract base state class (ArticleState below) is used so that the BLoC can have a single emitted type, without introduction optional or nullable fields for the individual sub-states.

import 'dart:convert';
import 'dart:io';

import 'package:meta/meta.dart';
import 'package:updater_queue_bloc/updater_queue_bloc.dart';

@immutable
abstract class ArticlePageState {}

class ArticlePageLoading extends ArticlePageState {
  ArticlePageLoading(this.articleId) : assert(articleId != null);

  final int articleId;
}

class ArticlePageLoaded extends ArticlePageState {
  ArticlePageLoaded(this.articleId, this.text)
      : assert(articleId != null),
        assert(text != null);

  final int articleId;
  final String text;
}

class ArticlePageBloc extends UpdaterQueue<ArticlePageState> {
  ArticlePageBloc(int initialArticleId)
      : super(ArticlePageLoading(initialArticleId)) {
    // trigger initial loading (this way we don't need an "idle" state)
    loadArticle(initialArticleId);
  }

  Future<void> loadArticle(int articleId) async {
    await expand(
      (value) async* {
        if (value is ArticlePageLoaded && value.articleId == articleId) {
          // Article already current & loaded
          // not doing anythig and emitting no new state.
          return;
        }

        // Emit loading state, UI might show progress indicator now
        yield ArticlePageLoading(articleId);

        // Fetching the Article Text
        final text = (await (await HttpClient()
                    .getUrl(Uri.parse('http://news.example.com/$articleId')))
                .close())
            .transform(Utf8Decoder())
            .join();

        yield ArticlePageLoaded(articleId, text);
      },
    );
  }
}

[1.0.1] - 2019-05-16

  • Add more elaborate example to README.

[1.0.0] - 2019-05-16

  • Bump version. There is really nothing more to this bloc.
  • Add documentation.

[0.1.1] - 2019-05-16

  • Add example

[0.1.0] - 2019-05-13

  • Initial Release

Use this package as a library

1. Depend on it

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


dependencies:
  updater_queue_bloc: ^1.0.1

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:updater_queue_bloc/updater_queue_bloc.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
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]
50
Learn more about scoring.

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

  • Dart: 2.5.1
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.4

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Maintenance suggestions

Maintain an example.

None of the files in the package's example/ directory matches known example patterns.

Common filename patterns include main.dart, example.dart, and updater_queue_bloc.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test
meta any 1.1.7
pedantic >=1.4.0