Home > Software engineering >  If statement AND expression not working Flutter
If statement AND expression not working Flutter

Time:12-12

I want to display items, in my case red cards for when there is a second-yellow and when there is straight red. So I want to execute an if statement for displaying both second yellow and red cards but it is not working in my case, can you tell what I am doing wrong.

my code below: My Model class

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

Widget where I am displaying red cards

                       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(),
                          ),
                        ),

Thanks in advance.

CodePudding user response:

You need to use || instead of &&:

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

you want to have red widget when status is second-yellow or straight-red not these status at the same time.

  • Related