Search code examples
flutterandroid-sqlitesqflite

The method 'add' was called on null : Flutter, SQFlite


I am getting data from SQLite in flutter. When I try to convert the list of Map to list of Objects. It gives the error of

The method 'add' was called on null

On debuging, it shows that it has data but still gives the error.

Here is my code

 List<Image> imagesList;
    if (imagesListMap != null) {
      imagesListMap.forEach((element) {
        imagesList.add(Image.FromDatabase(element));
      });
    }

And its the debugging screenshot

SS of debug


Solution

  • You need to initialize the List like this before calling add() on it..

    List<Image> imagesList = [];