0

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

1 回答 1

0

您实际上需要重新运行您的CheckBluetooth()功能。目前它只从Per.initialize().

你可以通过调用来做到这一点modal.CheckBluetooth();。然后它将执行该功能并在完成后通过 Provider 更新 UI。提供者不会继续运行其功能,以防万一发生变化,您必须手动执行此操作。

于 2020-05-21T15:56:33.613 回答