I have to get data from Firebase Database and through the key "keys" take all the data and insert them in "postsList". To do this I used the for-in loop. The latter throws me an error.
The error is: "A nullable expression can't be used as an iterator in a for-in loop. Try checking that the value isn't 'null' before using it as an iterator."
class _HomePageState extends State<HomePage> {
List<Posts> postsList = [];
@override
void initState() {
super.initState();
DatabaseReference postsRef = FirebaseDatabase.instance.reference().child("Posts");
postsRef.once().then((snap)
{
var keys = snap.snapshot.key;
var values = snap.snapshot.value;
postsList.clear();
for (var individual in keys) {
Posts posts = Posts(
values[individual]['url'],
values[individual]['descrizione'],
values[individual]['data'],
values[individual]['ora'],
);
postsList.add(posts);
}
setState(()
{
print('Length : $postsList.length');
});
});
}