cachapa / firedart

A dart-native implementation of the Firebase Auth and Firestore SDKs
https://pub.dev/packages/firedart
Apache License 2.0
174 stars 62 forks source link

Display Collection as a List #77

Open tonyhart7 opened 3 years ago

tonyhart7 commented 3 years ago

image

I want to make a list based on my collection like picture above but the thing is firedart dsnt return document snapshot How I can display that ? because I using Map<String, dynamic > the return Type is Page document level not a Map<String, dynamic>

tonyhart7 commented 3 years ago

@cachapa

tonyhart7 commented 3 years ago

image can Someone told me how UserCollection.fromMap do because im mapping manual <String, dynamic> didnt work

tpham3783 commented 3 years ago

I find that it is best practice to not co-mingle database access code w/ the UI code. Back to your question, what you do is that you query on a collection reference then iterate over each element on the page, which is a document that can directly be converted to a Map type. Here is an example from my data access code:

  Future<List<ChessGame>> getAllChessGames() async {
    List<ChessGame> _games = [];
    CollectionReference cREf = await Glob.controller.firestore.collection("/cchess/atlanta/games");
    final page = await cREf.get();
    for( Document doc in page){
      _games.add(ChessGame.fromMap(doc.map));
      _games.last.id = doc.id;
    }
    return _games;
  }
tonyhart7 commented 3 years ago

I find that it is best practice to not co-mingle database access code w/ the UI code. Back to your question, what you do is that you query on a collection reference then iterate over each element on the page, which is a document that can directly be converted to a Map type. Here is an example from my data access code:

  Future<List<ChessGame>> getAllChessGames() async {
    List<ChessGame> _games = [];
    CollectionReference cREf = await Glob.controller.firestore.collection("/cchess/atlanta/games");
    final page = await cREf.get();
    for( Document doc in page){
      _games.add(ChessGame.fromMap(doc.map));
      _games.last.id = doc.id;
    }
    return _games;
  }

thanks you, you saved my day bro