deferrable 1.0.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 66

deferrable #

void initState() {
    super.initState();
    _append('time created');
    // Cancel the stream subscription on `dispose()` by calling `defer`
    defer(Stream.periodic(const Duration(seconds: 1))
        .listen((_) => setState(() => date = DateTime.now()))
        .cancel);
  }

Why? #

Add operations to perform in dispose call of StatefulWidget State.

Useful when you're creating things in initState that need to be disposed of in dispose.

Instead of having to store things in private members, and clean up in a different spot in the code, simply call defer and this takes care of the rest.

Example #

class _DeferredTimerWidgetState extends State<DeferredTimerWidget>
    with Deferrable {
  var date = DateTime.now();
  @override
  void initState() {
    super.initState();
    _append('time created');
    defer(Stream.periodic(const Duration(seconds: 1))
        .listen((_) => setState(() => date = DateTime.now()))
        .cancel);
  }

  @override
  Widget build(BuildContext context) => Text(date.toIso8601String());
}

See the example and tests for usage.

1.0.2 #

  • blockSetStateAfterDispose configuration flag to prevent setState from being called after dispose.

1.0.1 #

  • isDisposed helper

1.0.0+3 #

  • description

1.0.0+2 #

  • analysis

1.0.0+1 #

  • Initial release

example/lib/main.dart

import 'dart:async';

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

class DeferredTimerWidget extends StatefulWidget {
  @override
  _DeferredTimerWidgetState createState() => _DeferredTimerWidgetState();
}

class _DeferredTimerWidgetState extends State<DeferredTimerWidget>
    with Deferrable {
  var date = DateTime.now();
  @override
  void initState() {
    super.initState();
    _append('time created');
    defer(Stream.periodic(const Duration(seconds: 1))
        .listen((_) => setState(() => date = DateTime.now()))
        .cancel);
    defer(() => Future.microtask(() => _append('time disposed')));
  }

  @override
  Widget build(BuildContext context) => Text(date.toIso8601String());
}

class Deferred extends StatefulWidget {
  @override
  _DeferredState createState() => _DeferredState();
}

class _DeferredState extends State<Deferred> with Deferrable {
  @override
  void initState() {
    super.initState();
    _append('button created');
    defer(() => Future.microtask(() => _append('button disposed')));
  }

  @override
  Widget build(BuildContext context) => RaisedButton(
        onPressed: () => showNotifier.value = false,
        child: Text("Dispose Deferred"),
        color: Colors.green,
      );
}

class App extends StatefulWidget {
  @override
  _AppState createState() => _AppState();
}

final showNotifier = ValueNotifier(false);
final _statusController = StreamController<String>();
final _append = _statusController.add;

class _AppState extends State<App> with Deferrable {
  String status = "nothing";
  @override
  void initState() {
    super.initState();
    defer(_statusController.stream
        .listen((s) => setState(() => status = '$status\n$s'))
        .cancel);
  }

  @override
  Widget build(BuildContext context) => ValueListenableBuilder<bool>(
      valueListenable: showNotifier,
      builder: (context, b, _) => Center(
            child: Column(children: [
              b
                  ? Deferred()
                  : RaisedButton(
                      onPressed: () => showNotifier.value = true,
                      child: Text("Create Deferred"),
                      color: Colors.red,
                    ),
              b ? DeferredTimerWidget() : SizedBox(),
              Text(status),
            ]),
          ));
}

void main() => runApp(MaterialApp(home: Scaffold(body: App())));

Use this package as a library

1. Depend on it

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


dependencies:
  deferrable: ^1.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:deferrable/deferrable.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
31
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]
66
Learn more about scoring.

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

  • Dart: 2.8.4
  • pana: 0.13.14
  • Flutter: 1.17.5

Analysis suggestions

Package not compatible with SDK dart

Because:

  • deferrable that is a package requiring null.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8 1.2.1
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
Dev dependencies
flutter_test
pedantic any