resolveEntityFromMap<E extends Object> static method

FutureOr<E?> resolveEntityFromMap<E extends Object>({
  1. Object? entityMap,
  2. Map<String, dynamic>? parentMap,
  3. String? entityField,
  4. E? fromMap(
    1. Map<String, dynamic> map
    )?,
  5. E? empty()?,
  6. Type? entityType,
  7. EntityRepositoryProvider? entityRepositoryProvider,
  8. EntityHandlerProvider? entityHandlerProvider,
  9. EntityProvider? entityProvider,
  10. EntityCache? entityCache,
  11. EntityResolutionRules? resolutionRules,
})

Implementation

static FutureOr<E?> resolveEntityFromMap<E extends Object>(
    {Object? entityMap,
    Map<String, dynamic>? parentMap,
    String? entityField,
    E? Function(Map<String, dynamic> map)? fromMap,
    E? Function()? empty,
    Type? entityType,
    EntityRepositoryProvider? entityRepositoryProvider,
    EntityHandlerProvider? entityHandlerProvider,
    EntityProvider? entityProvider,
    EntityCache? entityCache,
    EntityResolutionRules? resolutionRules}) {
  entityCache ??= JsonEntityCacheSimple();

  if (entityMap == null) {
    if (parentMap == null || entityField == null) {
      throw ArgumentError(
          'If `entityValue` is null, `parentMap` and `entityField` should be provided');
    }

    entityMap = parentMap[entityField];
  }

  FutureOr<E?> entity;

  if (entityMap is Map<String, dynamic>) {
    if (fromMap != null) {
      entity = fromMap(entityMap);
    }

    if (entity == null) {
      var entityRepository = _resolveEntityRepository<E>(entityField,
          entityType, entityRepositoryProvider, entityHandlerProvider);
      entity = entityRepository?.fromMap(entityMap,
          entityProvider: entityProvider,
          entityCache: entityCache,
          resolutionRules: resolutionRules);
    }

    if (entity == null) {
      entityHandlerProvider ??= EntityHandlerProvider.globalProvider;
      var entityHandler =
          entityHandlerProvider.getEntityHandler<E>(type: entityType);
      entity = entityHandler?.createFromMap(entityMap,
          entityProvider: entityProvider,
          entityCache: entityCache,
          resolutionRules: resolutionRules);
    }
  } else if (entityMap is num || entityMap is String) {
    var entityRepository = _resolveEntityRepository<E>(entityField,
        entityType, entityRepositoryProvider, entityHandlerProvider);
    entity = entityRepository?.selectByID(entityMap,
        resolutionRules: resolutionRules);
  }

  if (entity == null && empty != null) {
    return empty();
  } else {
    return entity;
  }
}