flutter - how to implement 'OR' Condition in Flutter
问题描述
I have below logic condition, and want to implement if userId = null OR 0, it will navigate to login Page, else will do its function. how to put 2 conditions in same logic?
if (userId == 0) {
Navigator.push(context, MaterialPageRoute(builder: (context)=> LoginScreen()));
} else {
dynamic addnow = await Provider.of<BagProvider>(
context,
listen: false)
.addToBag(
widget.product.productId.toString());
if (addnow == "done") {
setState(() {
add_button = "added";
});
}
}
解决方案
Use the ||
operator like this
if (userId == 0 || userId == null) {
Navigator.push(context, MaterialPageRoute(builder: (context)=> LoginScreen()));
} else {
dynamic addnow = await Provider.of<BagProvider>(
context,
listen: false)
.addToBag(
widget.product.productId.toString());
if (addnow == "done") {
setState(() {
add_button = "added";
});
}
}
推荐阅读
- go - 处理两个连续错误
- java - 从文件中读取 - 堆满
- jdbc - 如何在 Telosys CLI 中设置 JDBC 驱动程序 JAR 以创建 dbmodel
- spring - 在 RuntimeEnvironment 中包含 BPMN 文件的问题
- oracle - Oracle Scheduler proc 未在下一次开始时间之前完成
- java - 将 Perl 正则表达式转换为 Java
- scala - 在 Scala Play (2.6) EssentialFilter 中处理对 Future 完成的依赖
- docker - 0.0.0.0 没有在 Docker 中发送任何数据
- c# - 实体无法解析符号 SaveChanges
- c# - 在 c# 中返回/创建 JSON 的不同方式有哪些?