哥哥教你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);
}
}
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);
}
3,Flutter Redux 导包 flutter_redux: ^0.5.3
4,Fish_Redux 导包 fish_redux: ^0.1.7
5,Provider 导包 provider: ^3.0.0+1