title: How to handle "Bad state: No element" in flutter DataTable row
body: <p>I have DataTable in flutter which is working fine until I got an empty list. In case the <code>widget.detailDate.brands</code> is empty, I get &quot;Bad state: No element&quot; error, now my question is, how can I handle the error, I've tried something like</p>
  <pre><code>widget.detailDate.brands.map((brand) =&gt; {
    if (brand != ' '){
      // do the DataRow stuff
    }else{
      print('brnad not found')
    }
  }).toList();
  
  </code></pre>
  <p>But it's not working. I wanna add something like &quot;-&quot;, if the list is empty</p>
  <p>My whole code looks like this:</p>
  <pre><code>DataTable buildBrandTable(context) {
    return DataTable(
      columnSpacing: 0,
      showCheckboxColumn: false,
      columns: const &lt;DataColumn&gt;[
        DataColumn(
          label: Text(
            'Brand',
          ),
        ),
        DataColumn(
          label: Text(
            'Mandant',
          ),
        ),
        DataColumn(
            label: Expanded(
          child: Text(
            'Januar',
            textAlign: TextAlign.end,
          ),
        )),
        DataColumn(
            label: Expanded(
          child: Text(
            'Februar',
            textAlign: TextAlign.end,
          ),
        )),
        DataColumn(
            label: Expanded(
          child: Text(
            'März',
            textAlign: TextAlign.end,
          ),
        )),
        DataColumn(
            label: Expanded(
          child: Text(
            'April',
            textAlign: TextAlign.end,
          ),
        )),
        DataColumn(
            label: Expanded(
          child: Text(
            'Mai',
            textAlign: TextAlign.end,
          ),
        )),
        DataColumn(
            label: Expanded(
          child: Text(
            'Juni',
            textAlign: TextAlign.end,
          ),
        )),
        DataColumn(
            label: Expanded(
          child: Text(
            'Juli',
            textAlign: TextAlign.end,
          ),
        )),
        DataColumn(
            label: Expanded(
          child: Text(
            'August',
            textAlign: TextAlign.end,
          ),
        )),
        DataColumn(
            label: Expanded(
          child: Text(
            'September',
            textAlign: TextAlign.end,
          ),
        )),
        DataColumn(
            label: Expanded(
          child: Text(
            'Oktober',
            textAlign: TextAlign.end,
          ),
        )),
        DataColumn(
            label: Expanded(
          child: Text(
            'November',
            textAlign: TextAlign.end,
          ),
        )),
        DataColumn(
          label: Expanded(
            child: Text(
              'Dezember',
              textAlign: TextAlign.end,
            ),
          ),
        ),
      ],
      rows: [
        ...widget.detailData.brands!
            .map(
              (brand) =&gt; DataRow(
                onSelectChanged: (bool) {
                  Navigator.of(context)
                      .pushNamed(DetailScreen.routeName, arguments: {
                    'pageType': 'Brand',
                    'id': brand['brand_slug'].toString(),
                  });
                },
                color: (brand['brand'] == 'Gesamt')
                    ? MaterialStateProperty.resolveWith(
                        (Set&lt;MaterialState&gt; states) =&gt; Colors.grey[300])
                    : null,
                cells: [
                  DataCell(Text(brand['brand'])),
                  DataCell(Text(brand['mandant'])),
                  ..._month
                      .map(
                        (month) =&gt; DataCell(
                          Row(
                            mainAxisAlignment: MainAxisAlignment.end,
                            children: [
                              Text(formatter.format(brand[month])),
                            ],
                          ),
                        ),
                      )
                      .toList(),
                  DataCell(
                    Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: [
                        Text(
                          formatter.format(
                            _month
                                .map((month) =&gt; brand[month])
                                .toList()
                                .reduce((a, b) =&gt; a + b),
                          ),
                        ),
                      ],
                    ),
                  ),
                  DataCell(
                    Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: [
                        Text((brand['rate'] != null)
                            ? formatterPercent.format(brand['rate'] / 100)
                            : 'N/A'),
                      ],
                    ),
                  ),
                  DataCell(
                    Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: [
                        Text((brand['rate_letztes_jahr'] != null)
                            ? formatterPercent
                                .format(brand['rate_letztes_jahr'] / 100)
                            : 'N/A')
                      ],
                    ),
                  ),
                ],
              ),
            )
            .toList(),
  
      ],
    );
  }
  
  
  </code></pre>