【问题标题】:retrieve specific data from PraseQuery flutter从 PraseQuery 颤振中检索特定数据
【发布时间】:2021-11-09 01:00:22
【问题描述】:

我在我的颤振项目中使用 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 键值,我应该如何在我的查询中应用这种过滤器???

【问题讨论】:

    标签: flutter dart parse-platform query-builder back4app


    【解决方案1】:

    如果我正确理解您的问题,您希望减少服务器返回的密钥数量。 这可以使用keysToReturn(List&lt;String&gt; 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&lt;String&gt; keys)

    【讨论】:

    • 我很感激,用这种方法效果很好。
    【解决方案2】:

    创建数据类我为它选择了名称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);
        
    }
    

    【讨论】:

    • 非常感谢,现在有什么方法可以总结这些元素吗?我的意思是 exampleList.forEach((element) {print(element.eatenCal);});现在返回数字我如何收集和汇总这些数字?
    • 例如,一些带有数字的字段eatenCal。我更正了我的答案并在最后3行添加了总和计算。因此,您可以根据收到的信息计算出您想要的任何东西。
    • 我真的很感激。一切都很好,但是在我的控制台中,我首先有两个数字 eatenCal 列表中第一项的数字,第二个数字是整个列表的总和。为什么我有第一个号码?
    猜你喜欢
    • 2020-08-20
    • 1970-01-01
    • 2020-04-15
    • 1970-01-01
    • 2021-02-03
    • 2019-06-11
    • 2020-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多