withTranslations method Null safety

Section withTranslations(
  1. String path,
  2. String? condition,
  3. List<Translation> translations
)

A new section with the translation inserted at path.

Implementation

Section withTranslations(
  String path,
  String? condition,
  List<Translation> translations,
) {
  path = path.trim();
  assert(path.isNotEmpty);
  var pathSplits = path.split('.');

  final label = Label(
    key: pathSplits.last,
    cases: [
      Case(
        condition: Condition.parse(condition),
        translations: translations,
      ),
    ],
  );

  pathSplits = pathSplits.take(pathSplits.length - 1).toList();

  if (pathSplits.isEmpty) {
    return Section.merge(
      this,
      Section(
        key: key,
        labels: [label],
        children: [],
      ),
    );
  }

  var child = Section(
    key: pathSplits.last,
    labels: [label],
    children: [],
  );

  pathSplits = pathSplits.take(pathSplits.length - 1).toList();

  while (pathSplits.isNotEmpty) {
    child = Section(
      key: pathSplits.last,
      labels: [],
      children: [child],
    );
    pathSplits = pathSplits.take(pathSplits.length - 1).toList();
  }

  return Section.merge(
    this,
    Section(
      key: key,
      labels: [],
      children: [child],
    ),
  );
}