replace method Null safety

  1. @override
void replace(
  1. Parser source,
  2. Parser target

Changes the receiver by replacing source with target. Does nothing if source does not exist in Parser.children.

The following example creates a letter parser and then defines a parser called example that accepts one or more letters. Eventually the parser example is modified by replacing the letter parser with a new parser that accepts a digit. The resulting example parser accepts one or more digits.

final letter = letter();
final example =;
example.replace(letter, digit());

Override this method and Parser.children in all subclasses that reference other parsers.


void replace(Parser source, Parser target) {
  super.replace(source, target);
  if (delegate == source) {
    delegate = target as Parser<T>;