fetchPodPlaces static method

Future<List<Place>> fetchPodPlaces({
  1. bool forceRefresh = false,
})

Implementation

static Future<List<Place>> fetchPodPlaces({bool forceRefresh = false}) async {
  final places = <Place>[];
  final cm = PlacesCacheManager();
  try {
    if (!authStateNotifier.value) return places;
    if (!forceRefresh) {
      final mc = cm.podPlaces;
      if (mc != null) {
        _refreshPodPlacesInBackground();
        return mc;
      }
    }
    if (!forceRefresh) {
      final c = await PlacesCachePersistence.getCachedPodPlaces();
      if (c != null) {
        cm.cachePodPlaces(c);
        _refreshPodPlacesInBackground();
        return c;
      }
    }
    final content = await readPlacesJsonFile();
    if (content == null || content.trim().isEmpty) return places;
    final decoded = jsonDecode(content);
    if (decoded is List) {
      for (final i in decoded) {
        if (i is Map<String, dynamic>) {
          try {
            places.add(Place.fromJson(i, isLocalSource: false));
          } catch (_) {}
        }
      }
    } else if (decoded is Map<String, dynamic>) {
      try {
        places.add(Place.fromJson(decoded, isLocalSource: false));
      } catch (_) {}
    }
    places.sort((a, b) => b.timestamp.compareTo(a.timestamp));
    await PlacesCachePersistence.cachePodPlaces(content);
    cm.cachePodPlaces(places);
  } catch (_) {}
  return places;
}