Word.fromEntry constructor Null safety

Word.fromEntry(
  1. DictionaryEntries entry,
  2. [bool isCached = false]
)

Implementation

Word.fromEntry(remote.DictionaryEntries entry, [this.isCached = false])
    : id = entry.id,
      word = entry.word,
      language = entry.results?.firstOrNull?.language,
      variants = SplayTreeMap.of(
        Map.fromEntries(
          entry.results
                  ?.map(
                    (e) => e.lexicalEntries.map(
                      (e) => MapEntry(
                        e.lexicalCategory,
                        e.entries
                            .map(
                              (e) => Variant.fromLexicalEntry(e),
                            )
                            .toList(),
                      ),
                    ),
                  )
                  .expand((element) => element)
                  .fold<
                      List<MapEntry<remote.LexicalCategory, List<Variant>>>>(
                [],
                (reduced, element) {
                  if (reduced.map((e) => e.key).contains(element.key)) {
                    final list = reduced
                        .firstWhere((reducedElement) =>
                            reducedElement.key == element.key)
                        .value;
                    list.addAll([...element.value]);
                    list.sort(
                      (e1, e2) => e1.grammaticalFeatures == null ? -1 : 1,
                    );
                  } else {
                    reduced.add(MapEntry(element.key, element.value));
                  }
                  return reduced;
                },
              ) ??
              [],
        ),
        (k1, k2) => k1.compareTo(k2),
      ),
      phrases = Map.fromEntries(
        entry.results
                ?.map(
                  (e) => e.lexicalEntries.map(
                    (e) => MapEntry(
                      e.lexicalCategory,
                      e.phrases?.map((e) => e.text).toList() ?? [],
                    ),
                  ),
                )
                .expand((element) => element) ??
            [],
      ),
      rawResponse = entry;