Flutter, Provider not resolving to 'TRUE' in ternary operator

39 views Asked by At

The Text() should show 'Normal' if true, and 'Blackout' if false.

Container(
  child: Text(
Provider.of<RxStatus(context,listen:true).blackoutStatus[int.parse('1')] == '0' ? 'Normal':'BlackOut',
style: TextStyle(color: Colors.red),),
         )

However, Provider.of(context,listen: true).blackoutStatus[int.parse('1')] == '0' always resolves to 'False'

The code for the widget is here ( deleting non-pertinent sections):

class Vault extends StatefulWidget {
  const Vault({Key? key}) : super(key: key);

  @override
  _VaultState createState() => _VaultState();
}

class _VaultState extends State<Vault> {
  late Timer _timer;

  @override

  initState() {
    //
  _timer =  Timer.periodic(const Duration(seconds: 5), (timer) {
      Provider.of<RxStatus>(context,listen: false).getFeedback("vault");
      print('vault');
    });
    print("vault initState Called");
  }

  void dispose() {
    print('dispose vault');
    _timer?.cancel();
    super.dispose();
  }


  Widget build(BuildContext context) {
    var screenSize = MediaQuery.of(context).size;

    return  Stack(
      children:[ Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
       
         //Right Wall
          Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              RotatedBox(quarterTurns:1 , child: ProDsxRx(rxLabel: 'Vault 7', rxId: '7')),
              RotatedBox(quarterTurns:1 , child: ProDsxRx(rxLabel: 'Vault 8', rxId: '8')),
              RotatedBox(quarterTurns:1 , child: ProDsxRx(rxLabel: 'Vault 9', rxId: '9')),
              Container(
                child: Text(
                  Provider.of<RxStatus>(context,listen: true).blackoutStatus[int.parse('1')] ,
                  style: TextStyle(color: Colors.red),),
              ),
              Container(
                child: Text(
                  Provider.of<RxStatus>(context,listen: true).blackoutStatus[int.parse('1')] == '0' ? 'Normal': 'BlackOut',
                  style: TextStyle(color: Colors.red),),
              )
            ],
          )
        ],

      ),

        ProdsxFloatingMenuButton()
      ],
    );


  }
}

I have added another Text() widget above and can see that Provider.of(context,listen: true).blackoutStatus[int.parse('1')] is indeed changing between '0' or '2' However, when the Text() shows '0' , it still resolves to 'Blackout'(False)

'0' resolves to 'Blackout' (False) '2' resolves to 'Blackout' (False)

Here is snippets from the RxStatus Provider:

    class RxStatus extends ChangeNotifier {
   List rxIDs = [
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,13, 14, 15, 16, 19, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46
  ];

   final List rxIDs_bank = [
    10, 11, 12,13, 14, 15,19, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46
  ];

  final List rxIDs_vault = [
   1, 2, 3, 4, 5, 6, 7, 8, 9,16,
  ];
 
   Map blackoutStatus = {
     1:'0',2:'0',3:'0',4:'0',5:'0',
     6:'0',7:'0',8:'0',9:'0',10:'0',
     11:'0',12:'0',13:'0',14:'0',
     15:'0', 16:'0', 23:'0', 24:'0', 25:'0', 26:'0',
     27:'0', 28:'0', 29:'0', 30:'0', 31:'0', 32:'0',
     33:'0', 34:'0', 35:'0', 36:'0', 37:'0', 38:'0',
     39:'0', 40:'0', 41:'0', 42:'0', 43:'0', 44:'0',
     45:'0', 46:'0',
   };


   getFeedback(_bank_vault) async {
    if(_bank_vault == 'bank'){
      rxIDs = [...rxIDs_bank];
    }else if(_bank_vault =='vault'){
      rxIDs = [...rxIDs_vault];
    }
   
     //////////////
    rxIDs.asMap().forEach((index, item) async {
      try {
        print('blackout status${item}');
        // Check blackout
        var response = await http.get(Uri.parse('http://172.31.3.${item}/cgi-bin/query.cgi?cmd=cat /sys/devices/platform/videoip/pause'));
          blackoutStatus[item] = response.body;
          print(blackoutStatus);
          notifyListeners();
        }catch (error) {
          blackoutStatus[item] = 'Error'; //
          notifyListeners();
      }

    });
    // print(myStatus);

  }
}
1

There are 1 answers

0
sou pen su On

Found the problem. Isolated the problem to Provider.of(context,listen: true).blackoutStatus[int.parse('1')] == '0' It turns out that in the value in blackoutStatus , from a http get request was returning more than just '0' or '2' Tested the response.body.lenght and length showed 2 so the data returned must of had some space

So edited the code to use contains('0') Provider.of(context,listen: true).blackoutStatus[int.parse('1')].contains('0')