I want to create feeds like that of Facebook in my flutter application. I have stored data in a realtime database of flutter which is as follows:
.
The keys p1, p2, p3, ... store three values, named dp, feed and name. The dp and feed are image file URLs from Firebase Storage.
The code that I am writing to retrieve them is as follows:
class _DoodleHomeState extends State<DoodleHome> { List<Posts> postList = []; @override void initState() { // TODO: implement initState super.initState(); print(postList.length); //This prints the output 0 DatabaseReference postsRef = FirebaseDatabase.instance.reference().child("feeds"); print("At line 38"); //This prints the output postsRef.once().then((DataSnapshot snap) { print("At line 42"); //This does not print var KEYS = snap.value.keys; var DATA = snap.value; postList.clear(); for(var singleKey in KEYS){ Posts post = new Posts( DATA[singleKey]['dp'], DATA[singleKey]['feed'], DATA[singleKey]['name'], ); setState(() { postList.add(post); }); } print(postList.length); //This does not print setState(() { }); }); }
In this code, I have mentioned in the comments that the control does not go into the postsRef.once().then((DataSnapshot snap)
function, and the code does not seem to move forward from that point, because the print("At line 42")
statement isn't working.I have not yet added the code to show those files in my flutter application because I don't know if they are getting stored or not. Every answer and YouTube video that I have found works through similar logic by using .once()
and .then()
functions.
The firebase dependencies of my project in pubspec.yaml file are as follows:
firebase_core: ^0.5.0 firebase_auth: ^0.18.0+1 google_sign_in: ^4.5.3 firebase_database: ^4.4.0 path_provider: ^0.4.1
Please help me out.