dart 如何在三进制运算符中使用if/else以外的更多内容?

7uzetpgm  于 2023-07-31  发布在  其他
关注(0)|答案(3)|浏览(115)

我在为这个三元运算符编写正确的语法时遇到了麻烦。我想写三个if语句和一个else语句。
但据我所知,三元运算符只适用于if/else。
如何纠正代码中的错误?

Text(
              (browns == true && blacks == true && whites == true)
              ? 'Proportion: $browns agutis : $blacks pretos : $whites albinos'
              : (yellows == true && yellows == true && blacks == true)
                  ? 'Proportion: $yellows dourados : $blacks pretos : $browns chocolates'
                  : (reds == true && yellows == true && blacks == true)
                      ? 'Proportion: $reds coloridos : $whites brancos'
                      : 'Invalid proportion',
                 
            ),

字符串

编辑(显示更多代码):

body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              brownBlackWhites
                  ? 'Proportion: $browns agutis : $blacks pretos : $whites albinos'
                  : brownYellowBlack
                      ? 'Proportion: $yellows dourados : $blacks pretos : $browns chocolates'
                      :
                      // else if
                      redWhite
                          ? 'Proportion: $reds coloridos : $whites brancos'
                          // else
                          : 'Invalid proportion',
              style: TextStyle(
                  fontFamily: 'courier',
                  fontSize: 20.0,
                  fontWeight: FontWeight.bold,
                  height: 2.5,
                  letterSpacing: 0.7),
            ),].),),


根据第一代码片段的图像:x1c 0d1x的数据

yxyvkwin

yxyvkwin1#

if (browns == true)if (browns)相同,因此可以将条件存储在三个变量中,以提高可读性

final brownBlackWhites = browns && blacks && whites;
    final yellowBlack = yellows && blacks; // I think depend on your login you should add `&& browns` 
    final redYellowBlack = reds && yellows && blacks;
    Text(
      // if
      brownBlackWhites
          ? 'Proportion: $browns agutis : $blacks pretos : $whites albinos'
          :
          // else if
          yellowBlack
              ? 'Proportion: $yellows dourados : $blacks pretos : $browns chocolates'
              :
              // else if
              redYellowBlack
                  ? 'Proportion: $reds coloridos : $blacks pretos : $browns chocolates'
                  // else
                  : 'Invalid proportion',
    )

字符串

编辑

从那一行去掉点

),].),),


成为

),]),),

编辑2

final browns = colors.where((color) => color == Colors.brown).length;
final brownBlackWhites = browns == true && blacks == true && whites == true;


问题是browns的类型是int,所以browns == true不能工作,因为'=='比较两个相同的类型,而在这种情况下,你比较int和boolean

1tuwyuhd

1tuwyuhd2#

我的建议是使用一个builder widget,使代码可读性和可维护性,而不与三元运算符冲突。三进制运算符很难理解和维护,如果有一个以上的条件。

Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Builder(builder: (BuildContext context) {
          String text = '';
          if (browns == true && blacks == true && whites == true) {
            text =
                'Proportion: $browns agutis : $blacks pretos : $whites albinos';
          } else if (yellows == true && yellows == true && blacks == true) {
            text =
                'Proportion: $yellows dourados : $blacks pretos : $browns chocolates';
          } else if (reds == true && yellows == true && blacks == true) {
            'Proportion: $reds coloridos : $whites brancos';
          } else {
            text = 'Invalid proportion';
          }
          return Text(
            text,
            style: TextStyle(
                fontFamily: 'courier',
                fontSize: 20.0,
                fontWeight: FontWeight.bold,
                height: 2.5,
                letterSpacing: 0.7),
          );
        })
      ],
    ),

字符串

pkmbmrz7

pkmbmrz73#

这是在三元运算符中使用两个或多个条件的示例

const isValue = (_value) => _value > 100 ? "Greater Than 100" : _value < 100 ? "Lower Than 100" : "Equal to 100";

console.log(isValue(120)) // Output => Greater Than 100

console.log(isValue(50)) // Output => Lower Than 100

console.log(isValue(100)) // Output => Equal to 100

字符串
enter image description here

相关问题