Flutter,提供程序未在三元运算符中解析为“TRUE”

问题描述 投票:0回答:1

如果为 true,Text() 应显示“正常”,如果为 false,则应显示“停电”。

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

但是,Provider.of(context,listen: true).blackoutStatus[int.parse('1')] == '0' 始终解析为 'False'

小部件的代码在这里(删除不相关的部分):

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()
      ],
    );


  }
}

我在上面添加了另一个 Text() 小部件,可以看到 Provider.of(context,listen: true).blackoutStatus[int.parse('1')] 确实在 '0' 或 '2' 之间变化 但是,当 Text() 显示 '0' 时,它仍然解析为 'Blackout'(False)

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

以下是 RxStatus 提供程序的片段:

    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);

  }
}
flutter provider ternary
1个回答
0
投票

发现问题了。 将问题隔离到 Provider.of(context,listen: true).blackoutStatus[int.parse('1')] == '0' 事实证明,在 blackoutStatus 的值中,来自 http get 请求的返回不仅仅是“0”或“2” 测试了response.body.lenght,长度显示为2,所以返回的数据必须有一些空间

因此编辑了代码以使用 contains('0') Provider.of(context,listen: true).blackoutStatus[int.parse('1')].contains('0')

© www.soinside.com 2019 - 2024. All rights reserved.