Home > database >  I am getting RangeError (index): Invalid value: Valid value range is empty: 0 in my flutter code
I am getting RangeError (index): Invalid value: Valid value range is empty: 0 in my flutter code

Time:05-26

I am getting RangeError (index): Invalid value: Valid value range is empty: 0 The error is pointing to this code below ...........................................................................................................................................................................................................

child: FutureBuilder<Map>(
            future: _allRepos.getStats(),
            builder: (context, snapshot) {
              if (!snapshot.hasData) {
                return Center(child: CircularProgressIndicator());
              } else {
                Map datas = snapshot.data!;
                Map userStats = datas['users'][0];  
                Map trxnStats = datas['transactions'][0];
                List countries = datas['countries'];

                countries.sort((a, b) => b["count"].compareTo(a["count"]));

                statsBx.put('users', userStats);
                statsBx.put('trxnStats', trxnStats);
                statsBx.put('countries', countries);

                return Column(
                  children: [
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Expanded(
                          flex: 5,
                          child: Column(
                            children: [
                              UserStats(),
                              SizedBox(height: defaultPadding),
                              CustTable(
                                title: _allRepos.translation(
                                    context, "Top Countries")!,
                                columns: columnElements(),
                                rows: List.generate(
                                  countries.length,
                                  (index) => recentFileDataRow(
                                      countries[index], index),
                                ),
                              ),
                              if (Responsive.isMobile(context))
                                SizedBox(height: defaultPadding),
                              if (Responsive.isMobile(context))
                                TransactionsOverview(),
                            ],
                          ),
                        ),
                        if (!Responsive.isMobile(context))
                          SizedBox(width: defaultPadding),
                        // On Mobile means if the screen is less than 850 we dont want to show it
                        if (!Responsive.isMobile(context))
                          Expanded(
                            flex: 2,
                            child: TransactionsOverview(),
                          ),
                      ],
                    )
                  ],
                );
              }
            }),

CodePudding user response:

Make sure the incoming variables have a value.

if (datas['users'].isNotEmpty) {
Map userStats = datas['users'][0];
}
if (datas['transactions'].isNotEmpty) { 
Map trxnStats = datas['transactions'][0];
}
  • Related