Home > Net >  Why my toast cannot show when i try use connect activity flutter at initState?
Why my toast cannot show when i try use connect activity flutter at initState?

Time:05-11

i try to show toast event my connection is disabled, i just create toast at initState, so i think i can't build widget in initState, that's true?

this

CodePudding user response:

Why don't you make a global bool variable that is set to false, and in case of connectionStateChange, use setState() to update bool value to true. Check the bool value in initState() to return a Toast

CodePudding user response:

You can build widgets inside initState like ScaffoldMessenger which extends StatefulWidget.One of the solutions can be the below code:

 if(result == ConnectivityResult.none){
        ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
          content: Text("No internet connection"),
        ));
      }
  • Related