提问人:sou pen su 提问时间:10/7/2023 最后编辑:sou pen su 更新时间:10/8/2023 访问量:28
Flutter,Provider 未在三元运算符中解析为“TRUE”
Flutter, Provider not resolving to 'TRUE' in ternary operator
问:
如果为 true,则 Text() 应显示 'Normal',如果为 false,则应显示 'Blackout'。
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”解析为“停电”(False) “2”解析为“Blackout”(假)
以下是 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);
}
}
答:
0赞
sou pen su
10/8/2023
#1
发现问题。 将问题隔离到 Provider.of(context,listen: true).blackoutStatus[int.parse('1')] == '0' 事实证明,在 blackoutStatus 中的值中,来自 http get 请求返回的不仅仅是“0”或“2” 测试了 response.body.lenght 和 length 显示 2,因此返回的数据必须有一些空间
所以编辑了代码以使用 contains('0') Provider.of(context,listen: true).blackoutStatus[int.parse('1')].contains('0')
评论
RxStatus