TaskPhase.custom constructor

TaskPhase.custom(
  1. int index,
  2. String name
)

Create or return a custom phase with the given parameters.

It's an error to attempt to get a phase with an existing index but different name, or with an existing name but different index, but calling this method with the same name and index as an existing phase will return the existing phase.

To get all the existing phases, use TaskPhase.builtInPhases for the immutable, built-in phases, or TaskPhase.currentZoneTaskPhases for the current Zone's custom phases.

See TaskPhase.zonePhasesKey for information on isolating task phases modifications to Dart Zones.

Implementation

factory TaskPhase.custom(int index, String name) {
  final phases = currentZoneTaskPhases;
  final existingPhaseByIndex =
      phases.firstWhereOrNull((p) => p.index == index);
  final existingPhaseByName = phases.firstWhereOrNull((p) => p.name == name);
  if (existingPhaseByIndex != null &&
      existingPhaseByIndex == existingPhaseByName) {
    return existingPhaseByIndex;
  }
  if (existingPhaseByIndex != null) {
    throw DartleException(
        message: "Attempting to create new phase '$name' "
            "with existing index $index, which is used for phase "
            "'${existingPhaseByIndex.name}'");
  }
  if (existingPhaseByName != null) {
    throw DartleException(
        message: "Attempting to create new phase '$name' "
            "with existing name '$name'");
  }
  final phase = TaskPhase._(index, name);
  phases.add(phase);
  phases.sort();
  return phase;
}