close

[Solved] type ‘int’ is not a subtype of type ‘String’ error in Dart

Hello Guys How are you all? Hope You all are fine. When I was trying to run my flutter app and suddenly I get the following error in my stack track. type ‘int’ is not a subtype of type ‘String’ error in Dart in a flutter. So today Here I come with all possible solutions for this error.

We are providing you all possible solutions to solve this error. let’s start this article without wasting your time.

How type ‘int’ is not a subtype of type ‘String’ error in Dart Error Occurs ?

I am accessing value from list in listView builder but I am getting following error.

type 'int' is not a subtype of type 'String' error

Here is my code

List friendListData = _data['info'];

        child: new ListView.builder(
          itemCount: friendListData.length,
          itemBuilder: (BuildContext context, int index) {
            print("${friendListData[index]}");

            var userData = friendListData[index];
            var userScore1 = userData['user_info']['total_score'];
            var totalScrore = userScore1 / 100 


            return new ListTile(
              userScore: new Text(totalScrore ?? "0.00%"),
            );
          },
        ),

How to Solve type ‘int’ is not a subtype of type ‘String’ error in Dart Error?

  1. How to Solve type 'int' is not a subtype of type 'String' error in Dart Error?

    To Solve type 'int' is not a subtype of type 'String' error in Dart Error is saying you are passing String where flutter required for the Integer value. So that you have to pass Integer there Or you can just use our solutions to solve this error.

  2. type 'int' is not a subtype of type 'String' error in Dart

    To Solve type 'int' is not a subtype of type 'String' error in Dart Error is saying you are passing String where flutter required for the Integer value. So that you have to pass Integer there Or you can just use our solutions to solve this error.

Solution 1 : Use not Equal to null

Here use userName != null just like below.

userScore: new Text(totalScrore != null ? '$totalScrore ' : "0.00%"),

Or You can Use

userScore: new Text('${totalScrore ?? "0.00%"}'),

Solution 2 : Use ternary operator

The code in following line from your snippet is:

userScore: new Text(totalScrore ?? "0.00%"),

While, it should actually look like following:

userScore: new Text(totalScrore?.toString() ?? "0.00%"),

Solution 3 : Int to string Convert

Here totalScrore is Intiger value and userScrore: Require String and also Text() is also required string.

So that the totalScrore = userScore1 / 100 should be deleted, and the Text should be like

userScore = Text(userScore1  == null ? "0.00%" : '${userScore1 / 100}')

Summery

So, It’s All About this Error. I hope this tutorial helps you to Solve your error. Please Comment Below if You stucks anywhere with my code. And please comment below on which solution worked for you. Thank You.

Also Check Out Below Tutorials

Leave a Comment

%d bloggers like this: