Dart model throws error when mixing many data types

Issue

I made this simple model to work with an API

class BooksModel {
  List<_Book> _books = [];
  BooksModel.fromJson(Map<dynamic, dynamic> parsedJson) {
    List<_Book> temp = [];
    for (int i = 0; i < parsedJson['books'].length; i++) {
      _Book book = _Book(parsedJson['books'][i]);
      temp.add(book);
    }
    _books = temp;
  }
  List<_Book> get books => _books;
}

class _Book {
    int _id;
    String _name;
  _Book(book) {
      _id = book['id'];
      _name = book['name'];
  }
  int get id => _id;
  int get name => _name;
}

The problem is i have to turn all ‘_Book’ class properties to ‘String’, if i made only one ‘int’ as given in the above example, it throws this error.

type ‘String’ is not a subtype of type ‘int’

I don’t even use that ‘id’ which is ‘int’ in my app, so it’s not about the usage of it, the problem is in this model

Solution

Can you just show the example of your json, so that i can tell you that what your model should be or where it is going wrong.
So maybe your issue is not that big , id required is integer and you are passing the String.

Answered By – Sagar Acharya

Answer Checked By – Willingham (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *