从 PraseQuery flutter 中检索特定数据

retrieve specific data from PraseQuery flutter

我在我的 flutter 项目上使用 back4app.com 服务(Prase SDK)来处理我的后端。

在这个方法中,我尝试查询特定对象:


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;

并且我已经获得关于此对象的全部数据作为地图列表:

[{"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"}]

但我不需要整个数据,我只需要一个特定的键,例如这张地图中的值我只需要 UsersEaten 键值,我应该如何在我的查询中应用这种过滤器???

创建数据class我为它选择了名称Example

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));
}

很遗憾,我不知道如何使用这项服务back4app.com,但它应该是这样的

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);
    
}

如果我理解你的问题,你想减少服务器返回的密钥数量。 这可以使用 keysToReturn(List<String> keys).

来实现
 /// Define which keys in an object to return.
 ///
 /// [String] keys will only return the columns of a result you want the data for,
 /// this is useful for large objects
 void keysToReturn(List<String> keys) {
    limiters['keys'] = concatenateArray(keys);
 }

因此您的查询可能如下所示:

parseQuery
  ..whereContains('eatenBy', getUsrName!)
  ..whereEqualTo('eatenDate', date)
  keysToReturn(['YOUR_KEY_THAT_SHOULD_BE_RETURNED']);

还有与此方法完全相反的方法,称为 excludeKeys(List<String> keys)