首页 > 解决方案 > 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";
                                });
                              }
                            }

标签: flutter

解决方案


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

推荐阅读