0

I am trying to display individual elements of a JSON data on a dynamic listview. However, I keep getting "type 'int' is not a subtype of type 'String'" error and I have no idea why.

The code works if I include just the left() function in the widget located under the Row in the buildFlightsColumn function. But once I include the middle() and right() functions, I get the error.

Widget buildListView() {
    print(data);
    return ListView.builder(
      itemCount: data == null ? 0 : data.length,
      itemBuilder:  (context, index) {
        return buildFlightsColumn(data[index]); 
      }
    );
  }


  Widget buildFlightsColumn(dynamic item) => Container( 
    height: 150.0, 
    decoration: BoxDecoration(
    ),
    child: new Row( 
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: <Widget>[ 
        left(item['PlaceId']),
        middle(item['IataCode']),
        right()
      ],
    ),
  );

  Container left(dynamic item) {
    return new Container (
      child: Text(   
        item,
        textAlign: TextAlign.left,
        style: TextStyle(
          fontSize: 25.0,
          color: Colors.red,
        )
      ),
    );
  }

  Container middle(dynamic item) {
    return new Container( 
      child: Text(   
        item,
        textAlign: TextAlign.left,
        style: TextStyle(
          fontSize: 25.0,
          color: Colors.red,
        )
      ),
    );
  }

  Container right() {
    return new Container(
      child: RaisedButton( 
        onPressed: () { 
        },
        child: Text('Book Flights'),
      )
    );
  }

The data passed into the buildFlightsColumn function is JSON data returned by the API request:

[{PlaceId: 65368, IataCode: LAX, Name: Los Angeles International, Type: Station, SkyscannerCode: LAX, CityName: Los Angeles, CityId: LAXA, CountryName: United States}, {PlaceId: 81727, IataCode: SFO, Name: San Francisco International, Type: Station, SkyscannerCode: SFO, CityName: San Francisco, CityId: SFOA, CountryName: United States}]
1

3 Answers 3

1

Text widgets cannot display int s, they can only interpet strings , so your error is coming from this code

  Widget buildFlightsColumn(dynamic item) => Container( 
    height: 150.0, 
    decoration: BoxDecoration(
    ),
    child: new Row( 
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: <Widget>[ 
        left(item['PlaceId']) // item['placeId'] is int,
        middle(item['IataCode']),
        right()
      ],
    ),
  );

  Container left(dynamic item) {
    return new Container (
      child: Text(   
        item, // here item is int, which is not allowed <-----------------------
        textAlign: TextAlign.left,
        style: TextStyle(
          fontSize: 25.0,
          color: Colors.red,
        )
      ),
    );
  }

you can change it number to string using the .toString() method, or string interpolation


  Container left(dynamic item) {
    return new Container (
      child: Text(   
        item.toString(), // here item is String <-----------------------
        textAlign: TextAlign.left,
        style: TextStyle(
          fontSize: 25.0,
          color: Colors.red,
        )
      ),
    );
  }

Sign up to request clarification or add additional context in comments.

1 Comment

please accept the answer if it solves your question, thanks
0

I think the problem is that PlaceId is of type int but you try to use it as a String.

Change your code like this:

Container left(dynamic item) {
return new Container (
  child: Text(   
    item.toString(), //change the type here
    textAlign: TextAlign.left,
    style: TextStyle(
      fontSize: 25.0,
      color: Colors.red,
    )
  ),
);
}

Or like this:

 Widget buildFlightsColumn(dynamic item) => Container( 
height: 150.0, 
decoration: BoxDecoration(
),
child: new Row( 
  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  children: <Widget>[ 
    left(item['PlaceId'].toString()) // convert item['placeId'] to String
    middle(item['IataCode']),
    right()
  ],
),

);

Comments

0

Coming to your question, the Flutter Text widget accepts only the String data type. Try using x.toString() to convert int to String or use "$x" or "${x}" in Text widget.

To be honest, it is actually a bad practice to use JSON objects as it is in the flutter code. You should consider doing serialization & deserialization as it increases robustness. In simple terms, deserialization means converting your JSON into class object and serialization is the reverse. Here is an example from flutter docs.

class User {
  final String name;
  final String email;

  User(this.name, this.email);

  User.fromJson(Map<String, dynamic> json)
      : name = json['name'],
        email = json['email'];

  Map<String, dynamic> toJson() =>
    {
      'name': name,
      'email': email,
    };
}

It is strongly recommended to use this technique to have more control over data and their data-types.

Map userMap = jsonDecode(jsonString);
var user = User.fromJson(userMap); // convert json to obj

print('Howdy, ${user.name}!');
print('We sent the verification link to ${user.email}.');

String json = jsonEncode(user); // convert obj to json

More on this topic: https://flutter.dev/docs/development/data-and-backend/json

1 Comment

I will carry out your recommendations.

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.