getLocal method
Returns a variable that references the name
local.
Implementation
@override
o.Expression? getLocal(String name) {
if (name == EventHandlerVars.event.name) {
return EventHandlerVars.event;
}
if (!_state.localDeclarations.containsKey(name)) {
// Check if a local for `name` exists.
var currView = _state.view!;
var result = _state.locals[name];
while (result == null && currView.declarationElement.view != null) {
currView = currView.declarationElement.view!;
result = currView.nameResolver._state.locals[name];
}
if (result == null) return null; // No local for `name`.
var expression = getPropertyInView(result, _state.view!, currView);
final type = currView.nameResolver._state.localTypes[name];
if (type != null && type != o.DYNAMIC_TYPE) {
expression = unsafeCast(expression, type);
}
final modifiers = [o.StmtModifier.Final];
// Cache in shared view state for reuse if requested in other scopes.
// Since locals are view wide, the variable name is guaranteed to be
// unique in any generated method.
_state.localDeclarations[name] = o.DeclareVarStmt(
'local_$name',
expression,
null,
modifiers,
);
}
_localsInScope.add(name); // Cache local in this method scope.
return o.ReadVarExpr(_state.localDeclarations[name]!.name);
}