简体   繁体   中英

Why flutter snackBar not working inside if statement?

I have coded a SnackBar inside the if condition, if the condition is true it has to show SnackBar but it's not happening even when the condition is true.

this is my code

TextButton(
    onPressed: () async {
      await fetch();
      await _scan();
      setState(() {
        total = initial! + last!;
      });
      if (total.toString() == scanResult.rawContent){
        ScaffoldMessenger.of(context).showSnackBar(snackBar)                                 
      }
    },
    child: Text("scan")), 

I don't know why it's not working when even the condition is true

use ScaffoldMessenger outside the setState

onPressed: () async {
    await fetch();
    await _scan();
  total = initial! + last!;
    setState(() { });      
    if(total.toString() == scanResult.rawContent){
      ScaffoldMessenger.of(context).showSnackBar(snackBar) ;   
     }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM