0

I am using hive db to my flutter project. i want to save in favs some elemnets from the list, the main issue in about this error: As i understand this error occurs because dart thinks that i am trying to save not list of elements, but just a one element. i don't inderstand why i got this error. Also inside my app the behaviour of saving elements to favourites is like so: i can press the button "like" but when I try to remove chosen element from favourite it gives me this error:

 [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: NoSuchMethodError: The getter 'routes' was called on null.
E/flutter (15302): Receiver: null
E/flutter (15302): Tried calling: routes
E/flutter (15302): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:63:5)
E/flutter (15302): #1      TransportService.getMarshrutWithStops (package:fl_app/service/TransportService.dart:75:55)
E/flutter (15302): #2      _FavsSavePageState.initState (package:fl_app/screens/FavoritesPage.dart:45:13)

My code from TransportService where stacktrace leads:

 Future<List<RouteWithStops>> getMarshrutWithStops(int ttId) async {
    if (routesbyTransportType.isEmpty) {
      await fetchTransportWithRoutes();
    }
    List<Routes> routes = routesbyTransportType[ttId].routes;
    List<ScheduleVariants> variants = [];

    variants.addAll(await api.fetchSchedule());

    List<RouteWithStops> routesWithStops = [];

    for (Routes route in routes) {
      final routeWithStops = RouteWithStops();

      routesWithStops.add(routeWithStops);
      routeWithStops.route = route;

      routeWithStops.variant =
          variants.where((variant) => variant.mrId == route.mrId).first;
    }
    return routesWithStops;
  }

My code in the UI:

 @override
  void initState() {
    service.getMarshrutWithStops(widget.ttId).then((value) {
      setState(() {
        _routes.addAll(value);
      });
    });
    favoriteRoutesBox = Hive.box(favoritesBox);
    _editingController = TextEditingController();
    super.initState();
  }


Widget getIcon(int index) {
    if (favoriteRoutesBox.containsKey(index)) {
      return Icon(Icons.favorite, color: Colors.red);
    }
    return Icon(Icons.favorite_border);
  }

  void onFavoritePress(int index) {
    if (favoriteRoutesBox.containsKey(index)) {
      favoriteRoutesBox.delete(_routes[index].route.ttId);
      return;
    }
    favoriteRoutesBox.put(index, _routes[index]);
  }
leftjoin
  • 36,950
  • 8
  • 57
  • 116
inkwelll075
  • 494
  • 4
  • 19
  • your routesbyTransportType[ttId] is null so you can't call routesbyTransportType[ttId].routes; this will throw an error of null Reciever – Diwyansh Nov 12 '21 at 12:41
  • @Diwyansh hmmm, strange, because the whole logic is working fine but here I get the error when try to remove elements from DB. Anyway, thanks you – inkwelll075 Nov 12 '21 at 12:48
  • okay, but I prefer to check whether routesbyTransportType[ttId] has data or not? you can also add more details to the question. – Diwyansh Nov 12 '21 at 12:55
  • Finally, i found out what the issue was it: I just used the wrong hive method for deleting elements I previously put into the favs. I should use `deleteAt` method if I would like to delete items by it's Id. So simple solution and so much efforts to find out what is wrong – inkwelll075 Nov 15 '21 at 12:46

0 Answers0