【问题标题】:How to save some specific items from a list? Flutter/Dart如何保存列表中的某些特定项目?颤振/飞镖
【发布时间】:2022-10-15 20:40:10
【问题描述】:

我有一个如下列表:

你好,我有一个如下列表:

如果月 = 5 我想存储括号“[cardBillsTotal]”,我该怎么做?

如果月份 = 5 我想存储括号“[cardBillsTotal]”,我该怎么做?

{
    "results": [
        {
            "firstName": "Thomas",
            "lastName": "Papioanou",
            "userID": 238,
            "month": 5,
            "year": 2022,
            "cardBillsTotal": [
                {
                    "serial": "884221337251",
                    "value": 1.0450,
                    "vat": 0.2508,
                    "total": 1.2958,
                    "energy": 0
                }
            ]
        },
        {
            "firstName": "Thomas",
            "lastName": "Papioanou",
            "userID": 238,
            "month": 6,
            "year": 2022,
            "cardBillsTotal": [
                {
                    "serial": "884221337251",
                    "value": 3.4034,
                    "vat": 0.8168,
                    "total": 4.2202,
                    "energy": 0
                }
            ]
        },
        {
            "firstName": "Thomas",
            "lastName": "Papioanou",
            "userID": 238,
            "month": 7,
            "year": 2022,
            "cardBillsTotal": [
                {
                    "serial": "884221337251",
                    "value": 2.0900,
                    "vat": 0.5016,
                    "total": 2.5916,
                    "energy": 0
                }
            ]
        },
        {
            "firstName": "Thomas",
            "lastName": "Papioanou",
            "userID": 238,
            "month": 5,
            "year": 2022,
            "cardBillsTotal": [
                {
                    "serial": "941368618045",
                    "value": 2.2884,
                    "vat": 0.5492,
                    "total": 2.8376,
                    "energy": 0
                }
            ]
        },
        {
            "firstName": "Thomas",
            "lastName": "Papioanou",
            "userID": 238,
            "month": 6,
            "year": 2022,
            "cardBillsTotal": [
                {
                    "serial": "941368618045",
                    "value": 1.1083,
                    "vat": 0.2660,
                    "total": 1.3743,
                    "energy": 0
                }
            ]
        }
    ],
    "count": 5
}

如果您需要,这是我的请求和代码:

Future<List<Evsebillall>> fetchall() async {
    String? token = await this.storage.read(key: "token");
    Map<String, String> headers = {
      "Content-Type": "application/json",
      "Accept": "application/json",
      "Authorization": "Bearer " + (token ?? ""),
    };
    final response = await http.get(Uri.parse(
        this.serverIP + ':' + this.serverPort +
            '/user/contractedChargeTransactionsList?page=1&limit=10'),
        headers: headers);
    if (response.statusCode == 200) {

      setState(() {
        cardBillsall = jsonDecode(response.body)["results"] as List;
        print(cardBillsall);
        var result = cardBillsall.map((e) => Evsebillall.fromJson(e)).toList();
        print (data);
      });
      return cardBillsall.map((e) => Evsebillall.fromJson(e)).toList();
    }
    else{
      throw Exception('Failed to load Bills');
    }
  }

我尝试了这样的事情,但没有成功:

var data = cardBillsall.map((e) => e["month"]=5).toList();

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    尝试这个:

    var data = cardBillsall.where((element) => element["month"]== 5).toList();
    

    【讨论】:

      猜你喜欢
      • 2021-11-23
      • 2021-01-24
      • 2020-08-12
      • 1970-01-01
      • 2021-06-29
      • 2021-06-21
      • 2019-12-29
      • 1970-01-01
      • 2022-01-16
      相关资源
      最近更新 更多