Search code examples
androidfluttereventscalendar

Flutter How to convert List <string>to List<custom type>? Event type


I try to persist an Event data type in my project

loadEventList() async {
  await _init();
  final _ev = _prefs.getStringList('eventList');
  List<Event> listToEvent = _ev as List<Event>;
  return _events = listToEvent;
}

I've got this error:

[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type 'List<String>' is not a subtype of type 'List<Event>' in type cast

Can somebody help me find a way to convert my list into an event list?


Solution

  • I think using the map function would help

    _events = _ev.map((String element){return  Event(param);});