Last updated:
0 purchases
flutter lifecycle binding
chinese readme
Aim #
bind future to State ,when state disposed ,all binded future will cancelled;
Usage: #
create BindingHelper instance
BindingHelper _helper = BindingHelper();
copied to clipboard
bind future to BindingHelper
future.bindLifecycle(_helper).then((...))
copied to clipboard
dispose BindingHelper when ui disposed (for example: state disposed)
@override
void dispose() {
super.dispose();
_helper.dispose();
}
copied to clipboard
if you want use in State ,there is another mixin you can use.
add mixin on State
import 'package:lifecycle_binding/lifecycle_binding.dart';
class _State extends State with StateLifecycleBinding
copied to clipboard
bind future to State
future.bindLifecycleToState(this).then((){...})
copied to clipboard
Attention #
1.when use future like this:
future.then((){
//section 1
})
bindLifecycle(helper)
.then((){
//section 2
})
copied to clipboard
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();
}
}
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.