Home > Net >  A nullable expression can't be used as an iterator in a for-in loop. Try checking that the valu
A nullable expression can't be used as an iterator in a for-in loop. Try checking that the valu

Time:01-07

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');
      });

    });
  }

CodePudding user response:

I have read the second part of your answer,i have an error about "values" in the for loop: "The type 'Object' used in the 'for' loop must implement Iterable"

if(values != null) {
        for (var data in values) {
          Posts posts = Posts(
            data["url"],
            data['descrizione'],
            data['data'],
            data['ora'],
          );
          postsList.add(posts);
        }
      }

CodePudding user response:

The error message should come with a link to this documentation page on unchecked use of a nullable value. As the error says, the keys variable you use in for (var individual in keys) { can be null, which isn't allowed. You should either check if it's null, or assert that it isn't null:

if (keys != null) {
  for (var individual in keys) {
    Posts posts = Posts(
      values[individual]['url'],
      values[individual]['descrizione'],
      values[individual]['data'],
      values[individual]['ora'],
    );
    postsList.add(posts);
  }
}

The above fixes the syntax/compiler problem, but there may well be other problems in your code as I suspect that your keys variable will be a single string with the value Posts. If that's the case, consider looping over values instead, with the same null check as above.


Map<String,int>.from(event.snapshot.value)

map.forEach((k, v) => print("Key : $k, Value : $v"));

DatabaseReference postsRef = FirebaseDatabase.instance.reference().child("Posts");

postsRef.once().then((snap) {
  //            
  •  Tags:  
  • Related