我是颤振的新开发人员,我使用颤振使应用程序失明所以我正在使用提供程序数据包,当蓝牙状态发生更改时我需要更改小部件 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()),
])