chinese readme

Aim

bind future to State ,when state disposed ,all binded future will cancelled;

Usage:

  1. create BindingHelper instance
BindingHelper _helper = BindingHelper();
  1. bind future to BindingHelper
 future.bindLifecycle(_helper).then((...))
  1. dispose BindingHelper when ui disposed (for example: state disposed)
  @override
  void dispose() {
    super.dispose();
    _helper.dispose();
  }

if you want use in State ,there is another mixin you can use.

  1. add mixin on State

    import 'package:lifecycle_binding/lifecycle_binding.dart';
    
    class _State extends State with StateLifecycleBinding
    
    
  2. bind future to State

future.bindLifecycleToState(this).then((){...})

Attention

1.when use future like this:

  future.then((){
    //section 1
   })
  bindLifecycle(helper)
  .then((){
    //section 2
   })

section 1 will always run, section 2 will not run when state has disposed , so you should put ui releated code in section 2; but

Full Example


import 'package:flutter/material.dart';
import 'package:flutter_lifecycle_binding/lifecycle_binding.dart';

class StatePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _State();
  }
}

class _State extends State {
  String text = "如果5s内退出了页面,我不会被更新,也不会有日志打印";

  BindingHelper _helper = BindingHelper();

  @override
  void initState() {
    super.initState();
    Future.delayed(const Duration(milliseconds: 5000), () {
      return "我被更新了";
    }).bindLifecycle(_helper).then((value) {
      text = value;
      print(text);
      setState(() {});
    });

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(appBar: AppBar(), body: Container(child: Text(text)));
  }

  @override
  void dispose() {
    super.dispose();
    _helper.dispose();
  }
}

Libraries

flutter_lifecycle_binding