【问题标题】:If statement AND expression not working Flutter如果语句和表达式不起作用 Flutter
【发布时间】:2022-12-17 14:46:49
【问题描述】:

我想显示项目,在我的例子中是红牌,当有第二个黄色和有纯红色时。所以我想执行一个 if 语句来显示第二张黄牌和红牌,但在我的情况下它不起作用,你能告诉我我做错了什么吗?

我的代码如下: 我的模型课

class BookingSummary {
  int? count;
  String? type;
  Color? cardType;

  BookingSummary({this.count, this.type});

  BookingSummary.fromJson(Map<String, dynamic> json) {
    count = json['count'];
    type = json['type'];
    if (type != null) {
      switch (json['type']) {
        case 'straight-red':
          cardType = CustomTheme().logMoveDownRed;
          break;
        case 'second-yellow':
          cardType = CustomTheme().logMoveDownRed;
          break;
        default:
          cardType = null;
      }
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['count'] = count;
    data['type'] = type;
    return data;
  }
}

我在其中显示红牌的小部件

                       SizedBox(
                          height: 11,
                          child: Row(
                            mainAxisSize: MainAxisSize.min,
                            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                            children: team.bookingSummary!.map((e) {
                              if (e.type == 'second-yellow' &&
                                  e.type == 'straight-red') {
                                return Row(
                                    mainAxisSize: MainAxisSize.min,
                                    children: List.generate(e.count!, (index) {
                                      return Padding(
                                        padding: const EdgeInsets.symmetric(
                                            horizontal: 4),
                                        child: AspectRatio(
                                          aspectRatio: 2 / 3,
                                          child: Container(
                                            decoration: BoxDecoration(
                                              border: Border.all(
                                                color: CustomTheme().neutral500,
                                              ),
                                              borderRadius:
                                                  BorderRadius.circular(2),
                                              color: e.cardType,
                                            ),
                                          ),
                                        ),
                                      );
                                    }));
                              } else {
                                return const SizedBox();
                              }
                            }).toList(),
                          ),
                        ),

提前致谢。

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您需要使用 || 而不是 &amp;&amp;

    if (e.type == 'second-yellow' || e.type == 'straight-red')
    

    当状态为 second-yellowstraight-red 而不是这些状态时,您想拥有红色小部件。

    【讨论】:

    • 谢谢,我不知道为什么我错过了,我在想使用 && 就可以了。
    猜你喜欢
    • 2016-10-25
    • 2021-08-02
    • 2012-12-26
    • 2013-06-20
    • 2013-09-04
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多