async_builder 1.2.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 69

async_builder - Improved Future and Stream builder for Flutter. #

This package provides AsyncBuilder, a widget similar to StreamBuilder / FutureBuilder which is designed to reduce boilerplate and improve error handling.

It also provides InitBuilder, which makes it easier to start async tasks safely.

How to use #

1. Add to dependencies

dependencies:
  async_builder: ^1.2.0

2. Import

import 'package:async_builder/async_builder.dart';
import 'package:async_builder/init_builder.dart';

AsyncBuilder Examples #

Future #

AsyncBuilder<String>(
  future: myFuture,
  waiting: (context) => Text('Loading...'),
  builder: (context, value) => Text('$value'),
  error: (context, error, stackTrace) => Text('Error! $error'),
)

Stream #

AsyncBuilder<String>(
  stream: myStream,
  waiting: (context) => Text('Loading...'),
  builder: (context, value) => Text('$value'),
  error: (context, error, stackTrace) => Text('Error! $error'),
  closed: (context, value) => Text('$value (closed)'),
)

Note that you cannot provide both a stream and future.

AsyncBuilder Features #

Separate builders #

Instead of a single builder, AsyncBuilder allows you to specify separate builders depending on the state of the asynchronous operation:

  • waiting(context) - Called when no events have fired yet.
  • builder(context, value) - Required. Called when a value is available.
  • error(context, error, stackTrace) - Called if there was an error.
  • closed(context, value) - Called if the stream was closed.

If any of these are not provided then it defaults to calling builder (potentially with a null value).

Error handling #

AsyncBuilder does not silently ignore errors by default.

If an exception occurs and error is provided, the widget will rebuild and call that builder. Otherwise, if error is not provided and silent not true then the exception and stack trace will be printed to console (default behavior).

Initial value #

If initial is provided, it is used in place of the value before one is available.

RxDart ValueStream #

If stream is a ValueStream (BehaviorSubject) holding an existing value, that value will be available immediately on first build.

Stream pausing #

The StreamSubscription for this widget can be paused with the pause parameter, this is useful if you want to notify the upstream StreamController that you don't need updates.

InitBuilder #

InitBuilder is a widget that initializes a value only when its configuration changes, this is extremely useful because it allows you to safely start async tasks without making a whole new StatefulWidget.

The basic usage of this widget is to make a separate function outside of build that starts the task and then pass it to InitBuilder, for example:

static Future<int> getNumber() async => ...;

build(context) => InitBuilder<int>(
  getter: getNumber,
  builder: (context, future) => AsyncBuilder<int>(
    future: future,
    builder: (context, value) => Text('$value'),
  ),
);

In this case, getNumber is only ever called on the first build.

You may also want to pass arguments to the getter, for example to query shared preferences:

final String prefsKey;

build(context) => InitBuilder.arg<String, String>(
  getter: sharedPrefs.getString,
  arg: prefsKey,
  builder: (context, future) => AsyncBuilder<String>(
    future: future,
    builder: (context, value) => Text('$value'),
  ),
);

The alternate constructors InitBuilder.arg to InitBuilder.arg7 can be used to pass arguments to the getter, these will re-initialize the value if and only if either getter or the arguments change.

[1.2.0] #

  • Implemented InitBuilder
  • Bug fixes

[1.1.0] #

  • Added example
  • Bug fixes
  • Updated rxdart version constraints

[1.0.0] - Initial release. #

  • Implemented AsyncBuilder

example/lib/main.dart

import 'dart:async';
import 'dart:math';

import 'package:async_builder/async_builder.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blueGrey,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: const MyHomePage(title: 'AsyncBuilder Example'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: ListView(
        children: [
          AsyncTestChild1(),
          AsyncTestChild2(),
          AsyncTestChild3(),
          AsyncTestChild4(),
        ],
      ),
      backgroundColor: Colors.blueGrey.shade600,
    );
  }
}

class AsyncTestChild1 extends StatefulWidget {
  @override
  _AsyncTestChild1State createState() => _AsyncTestChild1State();
}

class _AsyncTestChild1State extends State<AsyncTestChild1> {
  Future<int> randomNumber;

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final textTheme = theme.textTheme;
    return TestCard(
      title: 'Random Numbers - Future',
      desc: 'This example completes a future with a random number after 2 seconds.',
      child: Row(children: [
        RaisedButton(
          color: Colors.blue,
          textColor: Colors.white,
          child: const Text('Generate'),
          onPressed: () {
            setState(() {
              randomNumber = Future.delayed(const Duration(seconds: 2), () => Random().nextInt(100));
            });
          },
        ),
        const Padding(padding: EdgeInsets.only(right: 16)),
        if (randomNumber != null) AsyncBuilder<int>(
          waiting: (context) => const CircularProgressIndicator(),
          builder: (context, i) => Text('$i', style: textTheme.headline6),
          future: randomNumber,
        ),
      ]),
    );
  }
}

class AsyncTestChild2 extends StatefulWidget {
  @override
  _AsyncTestChild2State createState() => _AsyncTestChild2State();
}

class _AsyncTestChild2State extends State<AsyncTestChild2> {
  StreamController<int> randomNumber;

  void initController() {
    setState(() {
      randomNumber?.close();
      randomNumber = StreamController<int>();
    });
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final textTheme = theme.textTheme;
    return TestCard(
      title: 'Random Numbers - Stream',
      desc: 'This example adds a random number to a stream after 1 second.',
      child: Row(children: [
        RaisedButton(
          color: Colors.blue,
          textColor: Colors.white,
          child: const Text('Reset'),
          onPressed: randomNumber == null ? null : () {
            setState(() {
              randomNumber = null;
            });
          },
        ),
        const Padding(padding: EdgeInsets.only(right: 8)),
        RaisedButton(
          color: Colors.blue,
          textColor: Colors.white,
          child: const Text('Add'),
          onPressed: () async {
            if (randomNumber == null) initController();
            final ctrl = randomNumber;
            await Future<void>.delayed(const Duration(seconds: 1));
            ctrl.add(Random().nextInt(100));
          },
        ),
        const Padding(padding: EdgeInsets.only(right: 16)),
        if (randomNumber != null) AsyncBuilder<int>(
          waiting: (context) => const CircularProgressIndicator(),
          builder: (context, i) => Text('$i', style: textTheme.headline6),
          stream: randomNumber.stream,
        ),
      ]),
    );
  }
}

class AsyncTestChild3 extends StatefulWidget {
  @override
  _AsyncTestChild3State createState() => _AsyncTestChild3State();
}

class _AsyncTestChild3State extends State<AsyncTestChild3> {
  StreamController<int> randomNumber;

  void initController() {
    setState(() {
      randomNumber?.close();
      final ctrl = StreamController<int>();
      randomNumber = ctrl;
      final timer = Timer.periodic(const Duration(milliseconds: 500), (timer) {
        ctrl.add(Random().nextInt(100));
      });
      ctrl.onCancel = timer.cancel;
    });
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final textTheme = theme.textTheme;
    return TestCard(
      title: 'Random Numbers - Closing',
      desc: 'This example continuously adds numbers to a stream until it is closed.',
      child: Row(children: [
        RaisedButton(
          color: Colors.blue,
          textColor: Colors.white,
          child: Text(randomNumber == null ? 'Start' : 'Restart'),
          onPressed: initController,
        ),
        const Padding(padding: EdgeInsets.only(right: 8)),
        RaisedButton(
          color: Colors.blue,
          textColor: Colors.white,
          child: const Text('Close'),
          onPressed: randomNumber == null || randomNumber.isClosed ? null : () {
            setState(() {
              randomNumber.close();
            });
          },
        ),
        const Padding(padding: EdgeInsets.only(right: 16)),
        if (randomNumber != null) AsyncBuilder<int>(
          waiting: (context) => const CircularProgressIndicator(),
          builder: (context, i) => Text('$i', style: textTheme.headline6),
          closed: (context, i) => Text('$i (Closed)', style: textTheme.headline6),
          stream: randomNumber.stream,
        ),
      ]),
    );
  }
}

class AsyncTestChild4 extends StatefulWidget {
  @override
  _AsyncTestChild4State createState() => _AsyncTestChild4State();
}

class _AsyncTestChild4State extends State<AsyncTestChild4> {
  StreamController<int> randomNumber;
  var pause = false;

  void initController() {
    setState(() {
      randomNumber?.close();

      final ctrl = StreamController<int>();
      randomNumber = ctrl;

      Timer timer;
      void start() {
        timer = Timer.periodic(const Duration(milliseconds: 500), (timer) {
          ctrl.add(Random().nextInt(100));
        });
      }

      ctrl.onPause = () => timer.cancel();
      ctrl.onCancel = () => timer.cancel();
      ctrl.onResume = start;

      start();
    });
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final textTheme = theme.textTheme;
    return TestCard(
      title: 'Random Numbers - Pausing',
      desc: 'This example continuously adds numbers to a stream but allows the subscription to be paused.',
      child: Row(children: [
        RaisedButton(
          color: Colors.blue,
          textColor: Colors.white,
          child: Text(randomNumber == null ? 'Start' : 'Restart'),
          onPressed: initController,
        ),
        const Padding(padding: EdgeInsets.only(right: 16)),
        Text('Pause', style: textTheme.subtitle2),
        Switch(value: pause, onChanged: (b) {
          setState(() {
            pause = b;
          });
        }, activeColor: Colors.blue),
        const Padding(padding: EdgeInsets.only(right: 16)),
        if (randomNumber != null) AsyncBuilder<int>(
          waiting: (context) => const CircularProgressIndicator(),
          builder: (context, i) => Text('$i', style: textTheme.headline6),
          stream: randomNumber.stream,
          pause: pause,
        ),
      ]),
    );
  }
}

class TestCard extends StatelessWidget {
  final String title;
  final String desc;
  final Widget child;

  const TestCard({this.title, this.desc, this.child});

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final textTheme = theme.textTheme;
    return Card(
      child: Padding(padding: const EdgeInsets.all(8), child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Padding(
            child: Text(title, style: textTheme.headline6),
            padding: const EdgeInsets.symmetric(vertical: 8),
          ),
          const Divider(),
          Text(desc, style: textTheme.subtitle1),
          const Padding(padding: EdgeInsets.only(bottom: 16)),
          child,
        ],
      )),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  async_builder: ^1.2.0

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

We analyzed this package on Jul 2, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13
  • Flutter: 1.17.5

Analysis suggestions

Package not compatible with SDK dart

because of import path [async_builder] that is in a package requiring null.

Health suggestions

Format lib/async_builder.dart.

Run flutter format to format lib/async_builder.dart.

Format lib/init_builder.dart.

Run flutter format to format lib/init_builder.dart.

Format lib/src/async_builder.dart.

Run flutter format to format lib/src/async_builder.dart.

Format lib/src/common.dart.

Run flutter format to format lib/src/common.dart.

Format lib/src/init_builder.dart.

Run flutter format to format lib/src/init_builder.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
flutter 0.0.0
rxdart >=0.20.0 <1.0.0 0.24.1
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8
Dev dependencies
flutter_test