我想在flutter中收集一个变量中相似值的数据,以进一步用于下拉选择

问题描述 投票:0回答:1

运行 API 时获取此类响应:

[{
    "docId": 1053,
    "tokenNo": 23477779999999,
    "docTitle": "Karishma resort ,baner",
    "address": "baner",
    "city": "pune",
    "partyName": "Rajesh patil",
    "pinCode": "452899",
    "docType": "Commercial",
    "duration": "24",
    "startDate": "2023-01-11",
    "endDate": "2025-01-10",
    "docStatus": "Open",
    "rentDesc": "17000",
    "createdBy": "Vaishnavi",
    "updatedBy": "null"
}, {
    "docId": 1052,
    "tokenNo": 22010799911122,
    "docTitle": "swapnapurti heights",
    "address": "Pirangut",
    "city": "Pune",
    "partyName": "Pranjali Sul",
    "pinCode": "411033",
    "docType": "Residential",
    "duration": "12",
    "startDate": "2023-01-07",
    "endDate": "2024-01-06",
    "docStatus": "Open",
    "rentDesc": "",
    "createdBy": "",
    "updatedBy": ""
}, {
    "docId": 1050,
    "tokenNo": 72389572857572,
    "docTitle": "Krishna Murti Nivas",
    "address": "Bavdhan",
    "city": "pune",
    "partyName": "Suhas kale",
    "pinCode": "736476",
    "docType": "Residential",
    "duration": "24",
    "startDate": "2023-01-14",
    "endDate": "2025-01-13",
    "docStatus": "Open",
    "rentDesc": "87033",
    "createdBy": "null",
    "updatedBy": "null"
}, {
    "docId": 932,
    "tokenNo": 2212010909755,
    "docTitle": "6/10 B Digital Apartment",
    "address": "Kothrud",
    "city": "Pune",
    "partyName": "Suresh",
    "pinCode": "411112",
    "docType": "Residential",
    "duration": "11",
    "startDate": "2022-12-01",
    "endDate": "2023-12-01",
    "docStatus": "Open",
    "rentDesc": "5000",
    "createdBy": "Swati",
    "updatedBy": null
}]

我已经实现了以下方法来获取仅包含

docStatus:"Open"
的文档:

List<Document> docs = [];
openedDocs() async {
    final docs = await DocumentController.getdocs(value);
    List<Document> opened;
    for (int i = 0; i <= docs.length; i++) {
      docs[i].docStatus == "Open" ? this.docs = docs : this.docs = List.empty();
    }
  }
Method getDocs() is like below:
 static Future<List<Document>> getdocs(String query) async {
    await SharedPrefService.init();
    var AuthToken = SharedPrefService.pref.getString('authToken');
    // final url = Uri.parse('http://192.168.0.131 :8080/Atdochub-3/document/');
    final url = Uri.parse(
        // 'http://192.168.0.131:8080/AtdochubJ-3/document/'
        'http://192.168.0.131:8080/AtdochubJ-3/document/');
    final response = await http.get(url, headers: {
      HttpHeaders.authorizationHeader: '${AuthToken}',
      'Content-Type': 'application/json; charset=UTF-8',
    });

    if (response.statusCode == 200) {
      final List docs = json.decode(response.body);
      // return json.decode(response.body).toList();

      return docs.map((json) => Document.fromJson(json)).where((doc) {
        final titleLower = doc.docTitle.toLowerCase();
        final tokenLower = doc.tokenNo.toString();
        final searchLower = query.toLowerCase();

        return titleLower.contains(searchLower) ||
            tokenLower.contains(searchLower);
      }).toList();
    } else {
      throw Exception();
    }
  }

我只想获取所有包含

docStatus="Open"
的文档的列表。

flutter dart dropdown
1个回答
1
投票

使用

where
创建匹配条件

return docs.map((json) => Document.fromJson(json))
           .where((doc) => doc.docStatus == 'Open').toList();
© www.soinside.com 2019 - 2024. All rights reserved.