flutter 如何检查列表中的值是否为空,如果不是,则获取值

tquggr8v  于 2022-11-25  发布在  Flutter
关注(0)|答案(3)|浏览(308)

我从这样的服务器上得到一个列表

[
  {
    "Date": "2022-10-21",
    "Wages": [
      {
        "Name": "101 Working hours",
        "Balance": "8.00",
        "Date": "2022-10-21",
        "ShiftName": "AU128"
      },
      {
        "Name": "102 Bonus pay",
        "Balance": "3:48",
        "Date": "2022-10-21",
        "ShiftName": ""
      },
      {
        "Name": "110 Split Shift",
        "Balance": "1:00",
        "Date": "2022-10-21",
        "ShiftName": ""
      },
      {
        "Name": "111 Wage reduction",
        "Balance": "1:00",
        "Date": "2022-10-21",
        "ShiftName": ""
      }
    ]
  },
]

如果班次名称不为空并显示在FE,我想获取班次名称班次名称在工资列表中

Text(
  Wages[i].shiftName ?? "",
  style: wageTextStyle,
),

我尝试使用List.any((any) => any.containsValue());
但我不知道从服务器获取哪个值,因为shiftName可以更改
我的API从提供程序使用者调用方法

Consumer<EmployeeWageAccountsProvider>(
              builder: (context, data, child) {
              if (!data.isLoading) {
              int length = data.getEmployeeAccountsData!.length;
              if (data.getEmployeeAccountsData!.isNotEmpty) {
                wageAccountsData = data.getEmployeeAccountsData!;
                return ListView.builder(
                  itemCount: length,
                  shrinkWrap: true,
                  scrollDirection: Axis.vertical,
                  itemBuilder: (context, i) {
                    return WageAccountsCard(
                      date: Helper.formatStringDate(
                          wageAccountsData[i].date!),
                     shiftName: wageAccountsData[i].wages! 
         [i].shiftName!.isEmpty ? "":wageAccountsData[i].wages! 
               [i].shiftName,
                     wages: wageAccountsData[i].wages,
                    );
                  },
                );
              }
              return noDataFound(context, 50);
            }
            return const WageAccountsShimmer();
          }),

工资帐户卡向用户显示数据

class WageAccountsCard extends StatelessWidget {
   final String? date;
   final String? shiftName;
   final List<Wages>? wages;

  const WageAccountsCard(
      {Key? key,
       this.date,
       this.shiftName,
       this.wages})
       : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
       margin: const EdgeInsets.all(10),
       padding: const EdgeInsets.all(8),
       decoration: CustomBoxDecoration.cardDecoration(context, 
       shadow: true),
     child: Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
       children: [
      Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            shiftName ?? "",
            style: wageTextStyle,
          ),
          Text(
            date.toString(),
            style: wageTextStyle,
          ),
        ],
      ),
      SizedBox(
        height: Styles.height(context) * 0.01,
      ),
      Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            S.of(context).wage_type,
            style: cTextStyle,
          ),
          Text(
            S.of(context).balance,
            style: cTextStyle,
          ),
        ],
      ),
      const Divider(
        color: Colors.black,
      ),
      ]
  ),
);
}
}

kknvjkwl

kknvjkwl1#

shiftName ?? "",用于在空情况下返回默认值。
您正在获取空字符串,因此您可以

shiftName.isEmpty ? "onEmptyCaseValue" : shiftName
imzjd6km

imzjd6km2#

return shiftName.isEmpty ? Center(child: Text('Empty')) : Text(
  shiftName ?? "",
  style: wageTextStyle,
),
bgtovc5b

bgtovc5b3#

wageAccountsData[i].wages! 
         [i].shiftName!.isEmpty ? "":wageAccountsData[i].wages[i].shiftName,

而不是这样写

wageAccountsData[i].wages![i].shiftName ?? "Default Text",

相关问题