getNextNode method

BaseNode? getNextNode(
  1. BaseNode curr,
  2. dynamic action
)

Gets the next node based on the current node and action.

This method implements Python's action-based transition logic.

Logic:

  1. If action is a String, it looks for a successor with that key.
  2. If action is not a String (e.g., null), it looks for 'default'.
  3. If no successor is found for the determined key, the flow ends.

Implementation

BaseNode? getNextNode(BaseNode curr, dynamic action) {
  BaseNode? next;
  // Determine the key to look for in the successors map.
  // If action is a string, use it. Otherwise, use 'default'.
  final actionToFind = (action is String) ? action : 'default';

  if (curr.successors.containsKey(actionToFind)) {
    next = curr.successors[actionToFind];
  } else {
    next = null; // No successor found for the action.
  }

  // Log a warning if the flow terminates but other paths were available.
  // This is useful for debugging and mirrors Python's behavior.
  if (next == null && curr.successors.isNotEmpty) {
    // We only log if the specific action we were looking for was not found.
    if (!curr.successors.containsKey(actionToFind)) {
      curr.log(
        "Warning: Flow ends: '$action' not found in "
        '${curr.successors.keys.toList()}',
      );
    }
  }

  return next;
}