customObjectsFromPin<T> static method

Future<List<T>> customObjectsFromPin<T>(
  1. String classname,
  2. FutureOr<T> fromJson(
    1. Map<String, dynamic> json
    )
)

Implementation

static Future<List<T>> customObjectsFromPin<T>(String classname,
    FutureOr<T> Function(Map<String, dynamic> json) fromJson) async {
  List<T> objectList = [];

  SharedPreferences prefs = await SharedPreferences.getInstance();
  String? objectsJson = prefs.getString(classname);

  if (objectsJson != null) {
    List<String> objectJsonList = objectsJson.split(';');
    if (objectJsonList.isNotEmpty) {
      // Remove last because split create at last one element = ""
      objectJsonList.removeLast();
    }

    for (String objectJson in objectJsonList) {
      var json = jsonDecode(objectJson);
      var obj = await fromJson(json);
      objectList.add(obj);
    }
  }

  return objectList;
}