0

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

lepsch
  • 8,927
  • 5
  • 24
  • 44

1 Answers1

0

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) ;   
     }}
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56