0

{"data":[{"id":"32f","regionName":"Korea","companyName":"Machine","catDealerCode":null},{"id":"cbb","regionName":"Korea","companyName":"KR","catDealerCode":null},{"id":"b6125b0e-5ec9",,"regionName":"China","companyName":"CHN","catDealerCode":null}],"code":0,"message":null}

I have data like the one you see above. I extract data according to the companyName. but some countries don't have data. I want to create an if else case within this.but no matter what I do when I say element == null it doesn't accept. Does anyone know where I am doing wrong? How should I create an if else for empty data?

  onTap: () async {
        List<Country> country =
            await fetchList(
              snapshot.data.code);
    
   country.forEach((element) {
         if(element.companyName == null){
       print('element is empty');
     }else{
          print('Here ${element.companyName}');
                                        }
         });
            },

And here's my country list data;

{"data":[{{"code":"KR","name":"Korea","isActive":true,"id":"71"},{"code":"RU","name":"Rusia","isActive":true,"id":"3c"},{"code":"Ch","name":"China","isActive":true,"id":"86"}],"code":0,"message":null}

 class Country {
  String id;
  String companyCode;
  String countryCode;
  String countryId;
  String regionName;
  String companyName;
  Null catDealerCode;

  Country(
      {this.id,
        this.companyCode,
        this.countryCode,
        this.countryId,
        this.regionName,
        this.companyName,
        this.catDealerCode});

  Couuntry.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    companyCode = json['companyCode'];
    countryCode = json['countryCode'];
    countryId = json['countryId'];
    regionName = json['regionName'];
    companyName = json['companyName'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['companyCode'] = this.companyCode;
    data['countryCode'] = this.countryCode;
    data['countryId'] = this.countryId;
    data['regionName'] = this.regionName;
    data['companyName'] = this.companyName;
    return data;
 

} }

4
  • Please format and indent your code correctly otherwise no one will help you. Commented Sep 6, 2020 at 22:23
  • sorry, i'm new here. I still can't get used to it,is it more organized this way? I didn't want to copy the whole page because this is where the relevant part. Sometimes I get a warning like 'extend the description' when I write too much code, Commented Sep 6, 2020 at 22:28
  • Is the companyName really null or it is just an empty string ? Can you add an example where a Company doesn't have a companyName ? @kimSoo Commented Sep 6, 2020 at 22:42
  • actually if there is no companyName it is not included in the data Commented Sep 6, 2020 at 22:45

1 Answer 1

1

I would go with null-aware operator:

onTap: () async {
                              List<Country> country =
                                        await fetchList(
                                            snapshot.data.code);
    
                                      country?.forEach((element) {
                                        if(element?.companyName == null){
                                          print('element is empty');
                                        }else{
                                          print('Here ${element.companyName}');
                                        }
                                      });
                          },
Sign up to request clarification or add additional context in comments.

5 Comments

I tried this solution but it doesn't work:/. It prints as 'Here....', but when pressing the empty item it does not say 'element is empty'
I'm pulling it from a different data but I added it to my question
Add your country class to your question too please.
I added my country class
My guess here is that you are using json_serializable package, with nullable set to default(which is true), which must return you an empty string for your element.companyName when null. Pretty sure you could do if(element?.companyName == null || element?.companyName?.isEmpty) (But i don't like this solution so much...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.