getVariable method

dynamic getVariable(
  1. String variableName
)

Returns the variable from the controller. This will first iterate through each VariableResolver given to the controller and will return the value from the first resolver that did not throw an UnknownVariableException.

If there are no resolvers on the controller, or all resolvers throw an UnknownVariableException, then this will next check the reserved variable names and the associated values.

Finally, if neither check resolves the variable, this will return the value from the variables map that has been set, and will return null if no value had been set for variableName.

Implementation

dynamic getVariable(String variableName) {
  var resolved = false;
  dynamic result;

  for (var resolver in _variableResolvers) {
    try {
      result = resolver(variableName);
      resolved = true;
    } catch (e) {
      if (e is UnknownVariableException) {
        // no-op
      } else {
        rethrow;
      }
    }
  }

  if (resolved != true) {
    switch (variableName) {
      case '_now':
        result = DateTime.now().toUtc();
        break;
      case '_platform':
        if (kIsWeb) {
          result = 'web';
        } else if (Platform.isAndroid) {
          result = 'android';
        } else if (Platform.isFuchsia) {
          result = 'fuchsia';
        } else if (Platform.isIOS) {
          result = 'ios';
        } else if (Platform.isMacOS) {
          result = 'macos';
        } else if (Platform.isWindows) {
          result = 'windows';
        } else {
          result = 'unknown';
        }
        break;
      default:
        result = _testVariables.containsKey(variableName)
            ? _testVariables[variableName]
            : _globalVariables[variableName];
    }
  }

  return result;
}