Search code examples
flutterdartparse-platformquery-builderback4app

retrieve specific data from PraseQuery flutter


I'm using back4app.com services (Prase SDK) on my flutter project to handle my backend.

in this method I try to query on specific object :


Future<List> getList(String date) async {
    final QueryBuilder<ParseObject> parseQuery =
        QueryBuilder<ParseObject>(ParseObject('UsersEaten'));

    parseQuery
      ..whereContains('eatenBy', getUsrName!)
      ..whereEqualTo('eatenDate', date);

    final ParseResponse apiResponse = await parseQuery.query();

    if (apiResponse.success && apiResponse.results != null) {
      List<dynamic>? apiRes = apiResponse.results;

and I've got whole data about this object as a List of Map :

[{"className":"UsersEaten","objectId":"OmrLz358Cb","createdAt":"2021-09-12T11:27:41.824Z","updatedAt":"2021-09-12T11:27:41.824Z","eatenTitle":"egg roll","eatenCal":180,"eatenFat":40,"eatenProtein":30,"eatenCarb":10,"eatenBy":"usr45","eatenDate":"2021-09-12"}, {"className":"UsersEaten","objectId":"lWIeMw54mH","createdAt":"2021-09-12T12:37:21.389Z","updatedAt":"2021-09-12T12:37:21.389Z","eatenTitle":"meat butter","eatenCal":235,"eatenFat":34,"eatenProtein":34,"eatenCarb":9,"eatenBy":"usr45","eatenDate":"2021-09-12"}]

but I dont need whole data I just want a specific Key , Values from this map for example I just need UsersEaten key values, how should I apply this kind of filter in my query???


Solution

  • Create data class I chose the name Example for it

    class Example {
      String? className;
      String? objectId;
      String? createdAt;
      String? updatedAt;
      String? eatenTitle;
      int? eatenCal;
      int? eatenFat;
      int? eatenProtein;
      int? eatenCarb;
      String? eatenBy;
      String? eatenDate;
     
      Example({
        this.className,
        this.objectId,
        this.createdAt,
        this.updatedAt,
        this.eatenTitle,
        this.eatenCal,
        this.eatenFat,
        this.eatenProtein,
        this.eatenCarb,
        this.eatenBy,
        this.eatenDate,
      });
    
      
    
      Map<String, dynamic> toMap() {
        return {
          'className': className,
          'objectId': objectId,
          'createdAt': createdAt,
          'updatedAt': updatedAt,
          'eatenTitle': eatenTitle,
          'eatenCal': eatenCal,
          'eatenFat': eatenFat,
          'eatenProtein': eatenProtein,
          'eatenCarb': eatenCarb,
          'eatenBy': eatenBy,
          'eatenDate': eatenDate,
        };
      }
    
      factory Example.fromMap(Map<String, dynamic> map) {
        return Example(
          className: map['className'],
          objectId: map['objectId'],
          createdAt: map['createdAt'],
          updatedAt: map['updatedAt'],
          eatenTitle: map['eatenTitle'],
          eatenCal: map['eatenCal'],
          eatenFat: map['eatenFat'],
          eatenProtein: map['eatenProtein'],
          eatenCarb: map['eatenCarb'],
          eatenBy: map['eatenBy'],
          eatenDate: map['eatenDate'],
        );
      }
    
      String toJson() => json.encode(toMap());
    
      factory Example.fromJson(String source) => Example.fromMap(json.decode(source));
    }
    

    Unfortunately, I don't know how to use this service back4app.com, but it should look like this

    if (apiResponse.success && apiResponse.results != null) {
          
        
        final maps = jsonDecode(apiResponse.results).cast<Map<String, dynamic>>();
    
        var exampleList = List.generate(maps.length, (i) {
          return Example.fromMap(maps[i]);
        });
    
        //sum of calories
        num sum = 0;
        exampleList.forEach((element){sum += element.eatenCal;});
        print(sum);
        
    }