【发布时间】:2020-09-08 06:55:36
【问题描述】:
我是 Flutter 的新开发人员,我使用 Flutter 使应用程序失明所以我正在使用 Provider 数据包,当蓝牙状态发生变化时我需要更改小部件 UL 我编写了执行此操作的代码,但在我的代码中小部件第二次运行应用程序后UL更改不会同时更改我可以同时更改小部件
类 ChangeNotifier
class Per extends ChangeNotifier {
bool BLu;
Per.initialize(){
CheckBluetooth();
}
CheckBluetooth()async{
await FlutterBlue.instance.state.listen((state)async {
if(state==BluetoothState.on){
BLu=true;
notifyListeners();
}else{
BLu=false;
notifyListeners();
}
if (state==BluetoothState.off) {
BLu=false;
notifyListeners();
} else if (state==BluetoothState.on){
BLu=true;
notifyListeners();
}
});
}
类小部件
class _HomeState extends State<Hom> {
@override
Widget build(BuildContext context) {
final modal =Provider.of<Per>(context);
if(modal.BLu!=true){
return Center(child: Text('NO'),);
}else{
return Center(child: Text('YES'),);
}
}
}
多提供者
return MultiProvider(providers: [
ChangeNotifierProvider.value(value:AuthProvider.initialize()),
ChangeNotifierProvider.value(value:Per.initialize()),
])
【问题讨论】:
-
非常感谢,我在 chaking blu 后调用 CheckBluetooth() 解决了这个问题
标签: flutter provider flutter-change-notifier