skip to Main Content

Following problem:

I have Hive in my project and there I save Lists of Objects. When I store something while I use the App, and want to get the data from Hive(still same session), then everything is fine and I got the data which I previously stored in Hive. When I look in my document Folder, there is also a .Hive file, where my data is stored. But After I close the App, and when I want to get the Data from Hive, then it tells me:

" type 'Unhandled exception:
type 'List<dynamic>' is not a subtype of type 'List<Bookingday>?' in type cast
#0      BoxImpl.get (package:hive/src/box/box_impl.dart:44:26)
#1      BookingDAO.Eval ()
#2      BookingDAO.getStoredWeek (package:workplace/utils/booking_dao.dart:23:36)
#3      _ReservationsState.initState (package:workplace/pages/reservations.dart:44:30)

I can’t understand such behaviour. Why does it works well, when I store and get the data in the same session, but after restarting the App, it says the List is of type dynamic?
Can it have something to do with how I open and close Hive?

my Method:

    Box<List<Bookingday>> boxList = Hive.box<List<Bookingday>>(bookingDayBoxName);

    List<Bookingday> getStoredWeek(DateTime firstJan, DateTime date) {
        String key = getCalenderWeek(firstJan, date);
        try {
          List<Bookingday>? bookList = boxList.get(key);
          if (bookList != null) {
            bookingdays = bookList;
            return bookList;
          } else {
            return List.generate(
                getWeek(dateNow).length,
                (index) => Bookingday(
                    day: dateNow,
                    parkingSlotReserved: false,
                    capacityCounter: 0,
                    maxCapacity: 4));
          }
        } catch (e) {
            if (e is TypeError) {}
        }
        return bookingdays;
     } 

2

Answers


  1. Try to cast

    Box<List<Bookingday>> boxList = Hive.box<List<Bookingday>>(bookingDayBoxName);
    
        List<Bookingday> getStoredWeek(DateTime firstJan, DateTime date) {
            String key = getCalenderWeek(firstJan, date);
            try {
              List<Bookingday>? bookList = boxList.get(key);
              if (bookList != null) {
                bookingdays = bookList!;
                return bookList;
              } else {
                return List.generate(
                    getWeek(dateNow).length,
                    (index) => Bookingday(
                        day: dateNow,
                        parkingSlotReserved: false,
                        capacityCounter: 0,
                        maxCapacity: 4)).cast<Bookingday>();
              }
            } catch (e) {
                if (e is TypeError) {}
            }
            return bookingdays;
         } 
    
    Login or Signup to reply.
  2. Box<List> box = Hive.box<List>(bookingDayBoxName);
    var list = box.get(key, defaultValue: [])?.cast<Bookingday>();
    

    because a limitation of Dart

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search