'"The method 'cast' isn't defined for the type 'Object'" when reading from Firebase

I create banner in listview and get the data from firebase database.

Future<List<String>> getBaners(DatabaseReference bannerRef){
  //The method 'cast' isn't defined for the type 'Object'.
  return bannerRef.once().then((snapshot) => snapshot.snapshot.value!.cast<String>().toList());
}


Solution 1:[1]

Try using

Future<List<String>> getBaners(DatabaseReference bannerRef)async{ 
  final DataSnapshot res = await bannerRef.get();
  if(res.exists)
    return res.value as List<String>;
  return [];
}
 

I am willing to help more if you can provide more details about the structure of the data that bannerRef is pointing to.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Frank van Puffelen