convertFrom method

  1. @override
DynamicMap? convertFrom(
  1. String key,
  2. Object? value,
  3. DynamicMap original, [
  4. FirestoreModelAdapterBase? adapter,
])
override

Convert from Firestore manageable type to ModelFieldValue.

Generate and return a DynamicMap value from key and value. original is passed the DynamicMap before conversion.

Return Null if there are no changes.

FirestoreModelAdapterBase is passed to adapter.

Firestoreで管理可能な型からModelFieldValueに変換します。

keyvalueからDynamicMapの値を生成して返してください。originalは変換前のDynamicMapを渡します。

変更がない場合はNullを返してください。

adapterFirestoreModelAdapterBaseが渡されます。

Implementation

@override
DynamicMap? convertFrom(
  String key,
  Object? value,
  DynamicMap original, [
  FirestoreModelAdapterBase? adapter,
]) {
  if (value is List) {
    final targetKey = "#$key";
    final targetList = original
        .getAsList(targetKey)
        .whereType<DynamicMap>()
        .cast<DynamicMap>();
    if (targetList.isNotEmpty &&
        targetList.every((e) => e.get(_kTypeKey, "") == type)) {
      return {
        key: value.whereType<num>().cast<num>().mapAndRemoveEmpty<DynamicMap>(
              (e) => ModelCounter(e.toInt()).toJson(),
            ),
        targetKey: null,
      };
    }
  } else if (value is Map) {
    final targetKey = "#$key";
    final targetMap = original
        .getAsMap(targetKey)
        .where((key, value) => value is DynamicMap)
        .cast<String, DynamicMap>();
    if (targetMap.isNotEmpty &&
        targetMap.values.every((e) => e.get(_kTypeKey, "") == type)) {
      return {
        key: value
            .where((k, v) => v is num)
            .cast<String, num>()
            .map<String, DynamicMap>(
              (k, v) => MapEntry(
                k,
                ModelCounter(v.toInt()).toJson(),
              ),
            ),
        targetKey: null,
      };
    }
  } else if (value is num) {
    final targetKey = "#$key";
    final targetMap = original.getAsMap(targetKey);
    final type = targetMap.get(_kTypeKey, "");
    if (type == this.type) {
      return {
        key: ModelCounter(value.toInt()).toJson(),
        targetKey: null,
      };
    }
  }
  return null;
}