Justin Dosaj
Justin Dosaj

Reputation: 597

How to grab parent data from a firebase database?

Here is my database and I only want to grab the titles and put them into a list but when I call data snapshot I get every single piece of data in the database with this code:

 void readTitle()
  {
    DBRef.child('Movies').once().then((DataSnapshot datasnapshot){
      print('MOVIE TITLE SNAP SHOT');
      print(datasnapshot.value.toString());
    });
  }

enter image description here

If you cannot do this, then what are some work arounds? Can I parse the entire data snapshot somehow to get only the titles into a list? I am not 100% sure how to do that or how to make this work for my needs.

Upvotes: 0

Views: 63

Answers (1)

Josteve Adekanbi
Josteve Adekanbi

Reputation: 12733

Try this out

  Future<List<String>> getMovies() async{
    DataSnapshot movies =  await DBRef.child("Movies").once();
    var moviesMap = Map<String, dynamic>.from(movies.value);
    var moviesList = [];
    moviesMap.forEach((key, value){
      moviesList.add(key);
    });
    return moviesList //moviesList is a List of the movie names;
  }

Upvotes: 2

Related Questions