Skip to content

Commit

Permalink
fix crash if local variable is used as provider, instead try to get a…
Browse files Browse the repository at this point in the history
…ctual provider
  • Loading branch information
Alexqwesa committed Sep 1, 2023
1 parent 4158086 commit 6f1e224
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/riverpod_graph/lib/src/analyze.dart
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,22 @@ VariableElement parseProviderFromExpression(
if (target != null) return parseProviderFromExpression(target, context);
}

if (providerExpression is SimpleIdentifier) {
// if local variable is used as provider, try to get actual provider
final block = providerExpression.thisOrAncestorOfType<Block>();
if (block != null) {
final vars = block.statements.whereType<VariableDeclarationStatement>();
for (final vv in vars) {
final element = vv.variables.variables.firstWhereOrNull(
(element) => element.name.toString() == providerExpression.toString(),
);
if (element != null) {
return element.declaredElement!;
}
}
}
}

throw UnsupportedError(
'unknown expression $providerExpression ${providerExpression.runtimeType}',
);
Expand Down

0 comments on commit 6f1e224

Please sign in to comment.