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]);
}