async_controller 0.0.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 62

async_controller #

A library for managing asynchronously loaded data in Flutter.

Do I need this? #

If your project contains isLoading flags or error handling duplicated across many pages, you may benefit from this package. It will let you write async loading with minimal amount of boilerplate. Unlike FutureBuilder, AsyncController ensures that fetch is performed only when necessary. You may configure when controller should refresh using provided Refreshers. More on that later.

final _controller = AsyncController<String>.method(() async {
  await Future<void>.delayed(Duration(seconds: 1));
  return 'Hello world';
});

class Minimal extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return _controller.buildAsyncData(builder: (_, data) {
      // This builder runs only if data is available.
      // buildAsyncData takes care of other situations
      return Text(data);
    });
  }
}

Example: minimal.dart

Pull to refresh #

AsynController provides a method that you can plug right into a RefreshIndicator. Also, if user tries to refresh while loading is already pending the previous loading will be cancelled.

final _controller = AsyncController.method(fetchSomething);
RefreshIndicator(
  onRefresh: _controller.performUserInitiatedRefresh,
  child: _controller.buildAsyncData(builder: buildContent),
)

Example: pull_to_refresh.dart

Loading and error handling #

AsyncController used with buildAsyncData, automatically handles loading and error states. There is no need to manually change isLoading flag, or catch. AsyncController will do the right thing by default, while allowing for customizations.

final _controller = AsyncController.method(() => throw 'error');
_controller.buildAsyncData(builder: builder: (_, data) {
  // this function runs only on success
  return Text(data);
})

Example: failure_handling.dart

Custom loading and error handling #

Loading are error handling widgets are created by AsyncDataDecoration. You may override their behavior by creating custom AsyncDataDecoration. The same decorator can then be used in every AsyncData in your app.

class CustomAsyncDataDecoration extends AsyncDataDecoration {
  @override
  Widget buildError(BuildContext context, dynamic error, VoidCallback tryAgain) {
    return Text('Sorry :(');
  }
}

final _controller = AsyncController.method(() => throw 'error');
_controller.buildAsyncData(
  builder: buildContent,
  decorator: CustomAsyncDataDecoration(),
)

Example: failure_handling_custom.dart

Automatic refresh #

Every AsyncController can be customized to automatically refresh itself in certain situations.

  • Refresh after network connection comes back.
controller.addRefresher(OnReconnectedRefresher());
  • Refresh data every X seconds.
controller.addRefresher(PeriodicRefresher(Duration(seconds: 3)));
  • Refresh after user resumes the app from background.
controller.addRefresher(InForegroundRefresher());
  • Refresh when another ChangeNotifier updates.
controller.addRefresher(ListenerRefresher(listenable));
  1. Easy to use customization through AsyncDataDecoration.
class MyDecoration extends AsyncDataDecoration {
  @override
  Widget buildNoDataYet(BuildContext context) => MyProgressIndicator();
}

Example: refreshers_page.dart

Paginated data #

PagedAsyncController class, which extends AsyncController, is capable of loading data in pages.

Example: paged_loading.dart

Filtering & searching

AsyncController can be extended to implement filtering & searching. You do this by extending FilteringAsyncController. Example: sort_and_search.dart

Async button #

Not really related to AsyncController, but still useful. AsyncButton is a button that handles async onPressed methods. When user presses the button:

  • starts the async operation provided in onPressed method
  • shows loading indicator
  • blocks the user interface to avoid typing on keyboard or leaving the page
  • in case error, shows snackbar
  • finally, cleans up loading indicator & interface lock
AsyncButton(
  // AsyncButtons takes a child like typical button
  child: const Text('Press me!'),
  // AsyncButton accepts async onPressed method and handles it
  onPressed: () => Future.delayed(Duration(seconds: 1)),
  // Through builder method we can support any kind of button
  builder: (x) => FlatButton(
    onPressed: x.onPressed,
    child: x.child,
  ),
),

Example: async_button_example.dart

Interaction with other packages #

AsyncController plays nicely with other. It implements ChangeNotifier and ValueListenable - classes commonly used inside Flutter. You can use it with any state management / dependency injection that you want. The example project includes samples for flutter_hooks and provider.

Example 1: hooks_example.dart

Example 2: provider_example.dart

[0.0.1] - 09.09.2019

  • Initial release

[0.0.2] - 09.09.2019

  • Formatting and README improvements

example/README.md

example #

A new Flutter project.

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:
  async_controller: ^0.0.2

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:async_controller/async_controller.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
28
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
62
Learn more about scoring.

We analyzed this package on Oct 16, 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

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.0 <3.0.0
connectivity ^0.4.3 0.4.5
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test