2

can anyone help!! I tried before to put ! before data but it still gives me error and I have another error in the line var keys = value.snapshot.value.keys; it said that The property 'keys' can't be unconditionally accessed because the receiver can be 'null'.

DatabaseReference reference = FirebaseDatabase.instance.reference();
  await reference.child('Newss').once().then((value) {
    if(value.snapshot.value !=null){

      var keys = value.snapshot.value.keys;
      var data = value.snapshot.value;
      for(var singleValue in keys){
        newssData.add(
           Newss(
             title: data[singleValue]['title'],
             image: data[singleValue]['image'],
             desc: data[singleValue]['desc'],
             ));
             newssData.reversed.toList();
      }
    }else{
      Fluttertoast.showToast(msg: 'No post uploaded yet');
    }

  });
  return newssData;




  }
  • [Non-local variables will not be automatically type-promoted.](https://stackoverflow.com/q/65035574/) Reorder your code and adjust your `if` check to test the local variable instead: `var data = value.snapshot.value; if (data != null) { var keys = data.keys; ... }` – jamesdlin Feb 24 '22 at 01:38
  • it`s not working – Abdallah Hossam Feb 24 '22 at 14:06
  • Is it a new error being reported against a different line? You probably also will have the same problem when you do `data[singleValue]['title']`, `data[singleValue]['image']`, etc. If you can guarantee that they aren't `null`, then you can add a non-null cast operator: e.g. `data[singleValue]['title']!`, etc. – jamesdlin Feb 24 '22 at 17:08

0 Answers0