asMap<T> method

Map<String, T> asMap<T>({
  1. Map<String, T>? onError,
})

Get value as Map.

This method will try to cast to your return type If errors occur, this method will return onError if it's not null or throw an errors if onError is null.

If you met ArgumentError then you're using unsupported return types.

Implementation

Map<String, T> asMap<T>({Map<String, T>? onError}) {
  try {
    final json = jsonDecode(asString()) as Map<String, dynamic>?;
    FirebaseRemoteHelper._printDebug('asMap json: $json');

    if (json != null) {
      return json.cast<String, T>();
    }
  } catch (e) {
    FirebaseRemoteHelper._printDebug('asMap ERROR: $e');
    if (onError != null) return onError;
    rethrow;
  }

  return {};
}