m0f1us
m0f1us

Reputation: 376

Flutter,type error occurs when trying to sort a list

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: 좌석버스,

e.g)

busInfoList[0] = 
{arrprevstationcnt: 7, arrtime: 475, nodeid: ICB164000395, nodenm: 인천대입구역, routeid: ICB165000012, routeno: 8, routetp: 간선버스, vehicletp: 일반차량}
busInfoList[0]["arrtime"] =

386

Like this codes above,

busInfoList[index] indicates each bus's information. busInfoList[index]["arrtime"] indicates arrival time as a int type as you can see.

Since i wanted to sort the busInfoList by arrtime, I used this

busInfoList.sort((a, b) => a["arrtime"].compareTo(b["arrtime"]));

But this error occurs.

I/flutter (13902): Another exception was thrown: type '(dynamic, dynamic) => dynamic' is not a subtype of type '(dynamic, dynamic) => int' of 'compare'

I already checked busInfoList[i]["arrtime"] is int type. But why this error occurs?

Upvotes: 1

Views: 455

Answers (1)

Spatz
Spatz

Reputation: 20138

To understand the problem run this Dart code:

var arr;
arr = [1, 23, 10, 17, 2];
arr.sort((a, b) => a.compareTo(b));

Code throws exception:

Exception has occurred. _TypeError (type '(dynamic, dynamic) => dynamic' is not a subtype of type '(int, int) => int' of 'compare')

But this code works fine:

  (arr as List<int>).sort((a, b) => a.compareTo(b));

Conclusion: you need hint to compiler about your data type .

Upvotes: 2

Related Questions