Home > Software design >  how to check value in list empty or not and if not get the value
how to check value in list empty or not and if not get the value

Time:11-02

I am getting a list from servers like this

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

I want to get ShiftName if it is not empty and shows at FE. shift name is in Wages List

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

I try to use List.any((any) => any.containsValue());

but I do not know which value I get from server because shiftName can be changed

my API calling method from provider Consumer

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

wage accounts card to displaying data to user

 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,
      ),
      ListView.builder(
        itemBuilder: (context, index) {
          return Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                wages![index].name!,
                style: cTextStyle,
              ),
              Text(
                wages![index].balance.toString(),
                style: cTextStyle,
              ),
            ],
          );
        },
        itemCount: wages!.length,
        shrinkWrap: true,
      ),
      SizedBox(
        height: Styles.height(context) * 0.01,
      ),
    ],
  ),
);

} }

enter image description here enter image description here

CodePudding user response:

shiftName ?? "", used to return default value on null case.

You are getting empty String, therefore you can do

shiftName.isEmpty ? "onEmptyCaseValue" : shiftName

CodePudding user response:

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

CodePudding user response:

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

insted of this just write like this

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