import 'package:flutter/material.dart';
import 'API.dart';
class BusInfomationScreen extends StatelessWidget {
final originToDestination;
final tableColor;
final nodeId;
var busInfo;
var busInfoList;
BusInfomationScreen({
this.originToDestination,
this.tableColor,
this.nodeId,
this.busInfoList,
}) {
GetAPI getAPI = GetAPI(nodeID: nodeId);
busInfo = getAPI.fetchPost();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body:
Container(
color: Colors.blueGrey,
child:
FutureBuilder<Map<String, dynamic>>(
future: busInfo, //fetchpost() == busInfoList
builder: (context, snapshot) {
if (snapshot.hasData) {
//return Text("${snapshot.data.values.toList()[0]["body"]["items"]["item"]}");
busInfoList = snapshot.data.values.toList()[0]["body"]["items"]["item"];
///
debugPrint("${busInfoList[0]["arrtime"].runtimeType}"); // int
debugPrint("${busInfoList[0].runtimeType}"); // _InternalLinkedHashMap<String, dynamic>
debugPrint("${busInfoList}");
debugPrint("${busInfoList.runtimeType}"); // List<dynamic>
busInfoList.sort((a, b) => a["arrtime"].compareTo(b["arrtime"]));
busInfoList =
[{arrprevstationcnt: 7, arrtime: 499, nodeid: ICB164000395, nodenm: 인천대입구역, routeid: ICB165000012, routeno: 8, routetp: 간선버스, vehicletp: 일반차량}, {arrprevstationcnt: 14, arrtime: 1582, nodeid: ICB164000395, nodenm: 인천대입구역, routeid: ICB165000020, routeno: 16, routetp: 간선버스, vehicletp: 일반차량}, {arrprevstationcnt: 17, arrtime: 1503, nodeid: ICB164000395, nodenm: 인천대입구역, routeid: ICB165000169, routeno: 780, routetp: 좌석버스, vehicletp: 일반차량}, {arrprevstationcnt: 7, arrtime: 1200, nodeid: ICB164000395, nodenm: 인천대입구역, routeid: ICB165000193, routeno: 909, routetp: 간선버스, vehicletp: 일반차량}, {arrprevstationcnt: 2, arrtime: 142, nodeid: ICB164000395, nodenm: 인천대입구역, routeid: ICB165000206, routeno: 91(순환), routetp: 지선버스, vehicletp: 일반차량}, {arrprevstationcnt: 9, arrtime: 726, nodeid: ICB164000395, nodenm: 인천대입구역, routeid: ICB165000225, routeno: 780-1, routetp: 좌석버스,
例如)
busInfoList[0] =
{arrprevstationcnt: 7, arrtime: 475, nodeid: ICB164000395, nodenm: 인천대입구역, routeid: ICB165000012, routeno: 8, routetp: 간선버스, vehicletp: 일반차량}
busInfoList[0]["arrtime"] =
386
就像上面的代码一样,
busInfoList[index]
指示每条总线的信息。
如您所见,busInfoList[index]["arrtime"]
将到达时间指示为int类型。
由于我想按busInfoList
对arrtime
进行排序,
我用了
busInfoList.sort((a, b) => a["arrtime"].compareTo(b["arrtime"]));
但是会发生此错误。
I/flutter (13902): Another exception was thrown: type '(dynamic, dynamic) => dynamic' is not a subtype of type '(dynamic, dynamic) => int' of 'compare'
我已经检查busInfoList [i] [“ arrtime”]为int类型。 但是为什么会发生此错误?
答案 0 :(得分:2)
要了解问题,请运行以下Dart代码:
var arr;
arr = [1, 23, 10, 17, 2];
arr.sort((a, b) => a.compareTo(b));
代码抛出异常:
发生异常。 _TypeError(类型'((dynamic,dynamic)=>动态'不是'compare'类型'(int,int)=> int'的子类型)
但是此代码可以正常工作:
(arr as List<int>).sort((a, b) => a.compareTo(b));
结论:您需要提示编译器有关您的数据类型。