小部件库扑朔迷离的颤抖:构建PhotosList(dirty,state:_PhotosListState#c298b)时引发了以下断言:

时间:2020-01-28 12:51:24

标签: flutter dart

此代码出现运行时错误。

     appBar: AppBar(
        title: Text(title),

      ),

      body: FutureBuilder<List<Photo>>(
        future: fetchPhotos(http.Client()),
        builder: (context, snapshot) {
          if (snapshot.hasError) print(snapshot.error);

          return snapshot.hasData
              ? PhotosList(photos: snapshot.data)
              : Center(child: CircularProgressIndicator());

        },
      ),
    );
  }
}

请让我知道此代码可能是什么问题。

  • Ran Flutter医生
  • 清除缓存
Widget bodyData() => DataTable(

      sortColumnIndex: 1,
      sortAscending: true,

      columns: <DataColumn>[

        DataColumn(
          label: Text("Company Name"),
          onSort: (_, __) {
            setState(() {
              widget.photos.sort((a, b) => a.data["quote"]["companyName"]
                  .compareTo(b.data["quote"]["companyName"]));
            });
          },
        ),
        DataColumn(
          label: Text("Dividend Yield TT"),
          onSort: (_, __) {
            setState(() {
              widget.photos.sort((a, b) => a.data["stats"]["dividendYield"]
                  .compareTo(b.data["stats"]["dividendYield"]));
            });
          },
        ),
        DataColumn(
          label: Text("IEX Bid Price"),
          onSort: (_, __) {
            setState(() {
              widget.photos.sort((a, b) => a.data["quote"]["iexBidPrice"]
                  .compareTo(b.data["quote"]["iexBidPrice"]));
            });
          },
        ),
        DataColumn(
          label: Text("Latest Price"),
          onSort: (_, __) {
            setState(() {
              widget.photos.sort((a, b) => a.data["quote"]["latestPrice"]
                  .compareTo(b.data["quote"]["latestPrice"]));
            });
          },
        ),
      ],
      rows: widget.photos
          .map(
            (photo) => DataRow(
          cells: [
            DataCell(
              Text('${photo.data["quote"]["companyName"] ?? ""}'),
            ),
            DataCell(
              Text("Last Price:"
                  '${photo.data["stats"]["latestPrice"] ?? ""}'),
            ),
            DataCell(
              Text("Dividend Yield22:"
                  '${photo.data["stats"]["dividendYield"] ?? ""}'),
            ),
            DataCell(
              Text("Last Price:"
                  '${photo.data["quote"]["iexBidPrice"] ?? ""}'),
            ),
            DataCell(
              Text("Last Price:"
                  '${photo.data["quote"]["latestPrice"] ?? ""}'),
            ),
          ],
        ),
      )
          .toList());
}

enter image description here

════════(6)手势捕获异常 ══════════════════════════════════════════════════ ═════════ 方法'compareTo'在null上被调用。 接收者:null 尝试调用:compareTo(0.020232447817836813)

enter image description here

1 个答案:

答案 0 :(得分:0)

每行DataColumn(标题)的编号和DataCell的编号应该相同。

在您的代码中,您缺少列标题“状态最新价格”(第二列标题)

尝试一下,

Widget bodyData() =>
      DataTable(
          sortColumnIndex: 1,
          sortAscending: true,
          columns: <DataColumn>[
            DataColumn(
              label: Text("Company Name"),
              onSort: (_, __) {
                setState(() {
                  widget.photos.sort((a, b) =>
                      a.data["quote"]["companyName"]
                          .compareTo(b.data["quote"]["companyName"]));
                });
              },
            ),
            DataColumn(
              label: Text("Stats Latest Price"),
              onSort: (_, __) {
                setState(() {
                  widget.photos.sort((a, b) =>
                      a.data["stats"]["latestPrice"]
                          .compareTo(b.data["stats"]["latestPrice"]));
                });
              },
            ),
            DataColumn(
              label: Text("Dividend Yield TT"),
              onSort: (_, __) {
                setState(() {
                  widget.photos.sort((a, b) =>
                      a.data["stats"]["dividendYield"]
                          .compareTo(b.data["stats"]["dividendYield"]));
                });
              },
            ),
            DataColumn(
              label: Text("IEX Bid Price"),
              onSort: (_, __) {
                setState(() {
                  widget.photos.sort((a, b) =>
                      a.data["quote"]["iexBidPrice"]
                          .compareTo(b.data["quote"]["iexBidPrice"]));
                });
              },
            ),
            DataColumn(
              label: Text("Latest Price"),
              onSort: (_, __) {
                setState(() {
                  widget.photos.sort((a, b) =>
                      a.data["quote"]["latestPrice"]
                          .compareTo(b.data["quote"]["latestPrice"]));
                });
              },
            ),
          ],
          rows: widget.photos
              .map(
                (photo) =>
                DataRow(
                  cells: [
                    DataCell(
                      Text('${photo.data["quote"]["companyName"] ?? ""}'),
                    ),
                    DataCell(
                      Text("Last Price:"
                          '${photo.data["stats"]["latestPrice"] ?? ""}'),
                    ),
                    DataCell(
                      Text("Dividend Yield22:"
                          '${photo.data["stats"]["dividendYield"] ?? ""}'),
                    ),
                    DataCell(
                      Text("Last Price:"
                          '${photo.data["quote"]["iexBidPrice"] ?? ""}'),
                    ),
                    DataCell(
                      Text("Last Price:"
                          '${photo.data["quote"]["latestPrice"] ?? ""}'),
                    ),
                  ],
                ),
          )
              .toList());
相关问题