0

I'm new developer on flutter and i blinding app using flutter So I'm using Provider packet and i need change widget UL when Bluetooth state was change i made the code that doing this but in my code the widget UL change after run app the second time not change at the same moment can i change widget at same time

class 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 widget

   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'),);
      }


    }

    }

MultiProvider

 return MultiProvider(providers: [
        ChangeNotifierProvider.value(value:AuthProvider.initialize()),
      ChangeNotifierProvider.value(value:Per.initialize()),
])
mohmmed ali
  • 805
  • 1
  • 5
  • 10

1 Answers1

0

You need actually re-run your CheckBluetooth() function. Currently it just runs once from Per.initialize().

You could do this by calling modal.CheckBluetooth();. Then it will execute that function and update the UI via the Provider once it is done. A Provider will not keep running its functions just in case something changes, you will have to do that manually.

Benedikt J Schlegel
  • 1,707
  • 1
  • 10
  • 23