getAllArticles method

Future<Map<String, List<EducationArticle>>> getAllArticles ()

Read all of the articles from the database and return a map containing the category and the respective list of articles

Implementation

Future<Map<String, List<EducationArticle>>> getAllArticles() async {

  DatabaseReference dbArticlesRef = FirebaseDatabase.instance.reference().child(
      'Articles');
  await dbArticlesRef.keepSynced(true);
  Map<String, List<EducationArticle>> articlesByCategoryMap = Map();
  List<EducationArticle> articleList = List<EducationArticle>();

  dbArticlesRef.once().then((DataSnapshot snapshot) {
    Map<dynamic, dynamic> categories = snapshot.value;
    categories.forEach((key, value) {
      value.forEach((key, value) {
        articleList.add(EducationArticle.fromMap(value));
      });
      articleList.sort((a,b) => a.compareTo(b));
      articlesByCategoryMap[key] = articleList;
      articleList.clear();
      });
    });
    return articlesByCategoryMap;
}