-1

Opening the box

var savedList;

Future initiateHive() async {
    ///Creating a HiveBox to Store data
    savedList = await Hive.openBox('Musicbox');
}

The function to put data to hive

var songFav = SongPlayList()..songInfo = songs[currentIndex].id;

print(songs[currentIndex].id);

savedList.put(songs[currentIndex].id, songFav);

The error is

Another exception was thrown: NoSuchMethodError: The method 'put' was called on null.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Abin vs
  • 103
  • 5

1 Answers1

0

I think the problem is that savedList.put(songs[currentIndex].id, songFav); is called before initiateHive() finishes execution since initiateHive() is asynchronous so savedList is still null

you can do something like:

if (savedList != null){
    savedList.put(songs[currentIndex].id, songFav);
}