此代码出现运行时错误。
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());
},
),
);
}
}
请让我知道此代码可能是什么问题。
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());
}
════════(6)手势捕获异常 ══════════════════════════════════════════════════ ═════════ 方法'compareTo'在null上被调用。 接收者:null 尝试调用:compareTo(0.020232447817836813)
答案 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());