哥哥教你Flutter中5种最常见的状态管理模式对比
1,Bloc + Stream 无需导包,Flutter自带‘dart:async’库
class PageBloc {
int_count =0;
/// StreamController
StreamController_countController =StreamController();
/// 对外提供入口
StreamSinkget _countSink =>_countController.sink;
/// 提供 stream StreamBuilder 订阅
Streamget stream =>_countController.stream;
void dispose() {
_countController.close();
}
void add() {
_count++;
_countSink.add(_count);
}
void dec() {
_count--;
_countSink.add(_count);
}
}
![](https://img.haomeiwen.com/i15581733/50443c3b2c49ced0.png)
2,Scoped_model 导包 scoped_model: ^1.0.1
class CountModelextends Model {
int_count =0;
intget count =>_count;
void add() {
_count++;
notifyListeners();
}
void dec() {
_count--;
notifyListeners();
}
static CountModelof(BuildContext context) =>
ScopedModel.of(context);
}
![](https://img.haomeiwen.com/i15581733/84ab3a508eaeef15.png)
![](https://img.haomeiwen.com/i15581733/a2cf319cb729d885.png)
3,Flutter Redux 导包 flutter_redux: ^0.5.3
![](https://img.haomeiwen.com/i15581733/9ffc27e15d5f3f0b.png)
![](https://img.haomeiwen.com/i15581733/1d691fb16f459c36.png)
![](https://img.haomeiwen.com/i15581733/ef49d08a878d9dff.png)
4,Fish_Redux 导包 fish_redux: ^0.1.7
![](https://img.haomeiwen.com/i15581733/9aa7e6ab5de0f8bd.png)
![](https://img.haomeiwen.com/i15581733/f14bda8859406aef.png)
![](https://img.haomeiwen.com/i15581733/896c04389aad39ff.png)
![](https://img.haomeiwen.com/i15581733/cb58c0990130abb7.png)
![](https://img.haomeiwen.com/i15581733/ccbf6f86e4633e4c.png)
![](https://img.haomeiwen.com/i15581733/d257e8cafc486ccf.png)
5,Provider 导包 provider: ^3.0.0+1
![](https://img.haomeiwen.com/i15581733/160f1fbac3934fab.png)
![](https://img.haomeiwen.com/i15581733/b53a611611d69d40.png)
![](https://img.haomeiwen.com/i15581733/b762419d2d5c9b7e.png)