Home > other >  Flutter type issue 'int' is not a subtype of type 'String?'
Flutter type issue 'int' is not a subtype of type 'String?'

Time:01-12

i am implementing like unlike feature .so in my postman the response is

"like_status": "0",
"saved_post_status": "0"
String? likeStatusmodel;
  String? savedPostStatus;

My Modeldatatype i used used is String? for both like_status & Saved_post_status ,as the returned data is in double quotes,

while loading some post its threw this exceptions as

type 'int' is not a subtype of type 'String?',,why this issue, how to solve

also is this check possible while using string

 child: SvgPicture.asset(
                                      color: (singleUserPost.postLikeCount ==
                                              '0')
                                          ? Colors.black
                                          : Colors.red,
                                      fit: BoxFit.cover,
                                      width: 20,
                                      "assets/images/like.svg")),

while changing to

                                      color: (singleUserPost.postLikeCount ==
                                              0)
                                          ? Colors.black
                                          : Colors.red,
                                      fit: BoxFit.cover,
                                      width: 20,
                                      "assets/images/like.svg")),

showing this warning & also conditional check is not done

warning Equality operator == invocation with references of unrelated types

CodePudding user response:

Seems like singleUserPost.postLikeCount can be null and of type String. So need to parse that as int to compare.

Can you try

(int.tryParse(singleUserPost.postLikeCount) ?? 0 == 0)

CodePudding user response:

your first question type 'int' is not a subtype of type 'String?',,why this issue, how to solve solution: to solve this issue you print or log your variable by veriable.runtimeType this will show you data type of your variable. example

void main() {
String a='ab';
print( a.runtimeType);
}

Yes, you can compare string while using == operator . example:

void main() {
String a='ab';
   if(a=='ab'){
    print('this is ab');
  }
}

CodePudding user response:

The issue you are seeing is likely due to the fact that the data you are receiving from the API response is of type int, but you have declared the corresponding model fields as String?. Since an int is not a subtype of String?, the exception is being thrown.

To fix this, you can change the data type of the model fields to int or double if required

int likestatusmodel;
int savedpoststatum;

Regarding the second part of your question, the warning you are seeing is because you are comparing a String type with an int type. To solve this issue you can try one of the following:

convert the string to int using int.parse()

child: SvgPicture.asset(
    color: (int.parse(singleUserPost.postLikeCount) == 0) 
            ? Colors.black 
            : Colors.red,
    fit: BoxFit.cover,
    width: 20,
    "assets/images/like.svg"
),




Change the value of like_status and saved_post_status to int in JSON response
Change the data type of fields in your model class
  • Related