Home > Mobile >  Automatically set State of Button WITHOUT pressing it
Automatically set State of Button WITHOUT pressing it

Time:01-04

friends, I have got a State Management Problem I couldn't get rid of and I want to reach out to you.

Basically, I activate with the Buttons a game and I am sending a String to the uC. The uC does its stuff and sends a response to Flutter including gameFinished=true (that works).

Now I want to reset the State of the Button to the init state WITHOUT pressing the Button. Following are some things I tried that didn't work.

@override
  void initState() {
    super.initState();
    setState(() {
      gameAktivated = false;
      gameStarted = false;
    });
  }
void asyncSetState() async {
    setState(() async {
      gameAktivated = false;
      gameStarted = false;
    });
  }

I am changing the style from "Start" to "Stop" when the Button is pressed and i send Data to the uC. (Works) Edit: Ofc I have a second button that triggers gameAktivated=true :)

ElevatedButton(
 onPressed: () {
  if (gameAktivated) {
   setState(() {
   gameStarted = !gameStarted;
  });
 if (gameStarted) {
  //Send Data to uC
 } else if (!gameStarted) {
  //Send Data to uC
 }
}
},
                    
child:
!gameStarted ? const Text('Start') : const Text('Stop'),
),

Button Displays Stop now. Following I am receiving a String from the uC that I jsonEncode and I receive gameFinished=true. (Works)

Container(
 child: streamInit
 ? StreamBuilder<List<int>>(
  stream: stream,
  builder: (BuildContext context,
  AsyncSnapshot<List<int>> snapshot) {
   if (snapshot.hasError) {
    return Text('Error: ${snapshot.error}');
   }
   if (snapshot.connectionState ==ConnectionState.active) {
    // getting data from Bluetooth
    var currentValue =const BluetoothConnection().dataParser(snapshot.data);
    config.jsonDeserializeGameFinished(currentValue);
    if(config.gameFinished){
     setState(() {
      gameAktivated = false;
      gameStarted = false;
     });
     asyncSetState();//Tested both methods seperate!
    }
    return Column(
     children: [
      Text(config.time.toString()),
     ],
    );
   } else {
    return const Text(
    'Check the stream',
    textAlign: TextAlign.center,
   );
  }
},
 ): const Text("NaN",textAlign: TextAlign.center,),
),

When I try to reset the state like in the code above this error occures: enter image description here

Calling setState Async didnt work for me either. Where and how can I set the state based on the response from the uC? Is it possible without using Provider Lib? Thanks in advance Manuel.

CodePudding user response:

You cannot use setState inside StreamBuilder because StreamBuilder will be listening to stream to build and at the same time it rebuild when setState() is used. So it throws

setState() called during build

How to overcome this ?

Remove the setState from within the StreamBuilder and setState based on the stream by listening to it inside the initState() method .

  @override
  void initState() {
      stream.listen((event) {
     // You can use event here
      if(condition)             //            
  • Related