0

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

ler
  • 1,466
  • 3
  • 20
  • 37

2 Answers2

1

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.

Sagar Acharya
  • 3,397
  • 4
  • 12
  • 34
1

Is book['id'] a string? Try it:

// _id = book['id'];
   _id = int.parse(book['id']);
Kahou
  • 3,200
  • 1
  • 14
  • 21