首页 > 解决方案 > 通过 ID Dart 在多个数组中查找匹配的算法?

问题描述

我需要编写一个算法,为每个站点搜索自己的路线。现在我使用几个模型来执行此操作:首先,路线变体正在寻找自己的路线(通过 mv_id),然后在每个航班卡上搜索不同的站点(通过 st_id)。所有这些模型都通过一个公共 ID 链接。我知道我需要将找到的所有内容放在一个数组中,现在我有了这样的算法,它会加载停止列表,但过了一会儿它给了我这个错误。我知道我的算法不正确,但我无法弄清楚如何按照上面的描述进行操作。

我的模型:

class RouteandStopsSearch{
  Routes route;
  List <StopList> stop;
  List <RaceCard> cards;
  ScheduleVariants variant;
}

我的算法:

Future<List<RouteandStopsSearch>> searchStops(int mv_id) async {
    List<FlightCard> cards = [];
    List<StopList> stopList = [];
    List<Routes> routes = [];
    List<ScheduleVariants> variants = [];

    cards.addAll(await api.fetchRaceCard(mv_id));
    stopList.addAll(await api.fetchStops());
    routes.addAll(await api.fetchroutes());
    variants.addAll(await api.fetchSchedule());

    print(cards);
    print(stopList);
    List<RouteandStopsSearch> searchStops = [];

    for (Routes routelist in routes) {
      final searchStop = RouteandStopsSearch();

      searchStops.add(searchStop);
      searchStop.route = routelist;

      searchStop.variant = variants.where((element) => element.mrId == routelist.mrId).first;
      List<StopList> currentRoutes = [];
      stopList.forEach((stop) {
        cards.forEach((cards) {
          if(stop.stId == cards.stId){
            return currentRoutes.add(stop);
          }
        });
      });
      return searchStops;
    }
  }

}

错误是:

I/flutter (23064): [Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'StopList', Instance of 'S

======== Exception caught by widgets library =======================================================
The following NoSuchMethodError was thrown building:
The method '[]' was called on null.
Receiver: null
Tried calling: [](0)

When the exception was thrown, this was the stack: 

标签: flutterdart

解决方案


推荐阅读