@@ -140,9 +140,27 @@ class CodeRunner extends ChangeNotifier {
140140 }
141141
142142 Future <void > _runReal () async {
143- final sdk = snippetEditingController! .sdk;
144- final parsedPipelineOptions =
145- parsePipelineOptions (snippetEditingController! .pipelineOptions);
143+ // Capture once. Never use snippetEditingController! directly in async code.
144+ final controller = snippetEditingController;
145+ if (controller == null ) {
146+ return ; // Nothing to run (widget disposed / controller cleared)
147+ }
148+
149+ // Guard client (this is the typical null on Web Server).
150+ final client = codeClient;
151+ if (client == null ) {
152+ _setResult (
153+ RunCodeResult (
154+ errorMessage: kUnknownErrorText, // or a clearer message
155+ sdk: controller.sdk,
156+ status: RunCodeStatus .unknownError,
157+ ),
158+ );
159+ return ;
160+ }
161+
162+ final sdk = controller.sdk;
163+ final parsedPipelineOptions = parsePipelineOptions (controller.pipelineOptions);
146164 if (parsedPipelineOptions == null ) {
147165 _setResult (
148166 RunCodeResult (
@@ -170,10 +188,11 @@ class CodeRunner extends ChangeNotifier {
170188 ),
171189 );
172190
191+ // Capture request inputs up front too (controller can change/dispose later).
173192 final request = RunCodeRequest (
174- datasets: snippetEditingController ? .example? .datasets ?? [],
175- files: snippetEditingController ! .getFiles (),
176- sdk: snippetEditingController ! . sdk,
193+ datasets: controller .example? .datasets ?? [],
194+ files: controller .getFiles (),
195+ sdk: sdk,
177196 pipelineOptions: parsedPipelineOptions,
178197 );
179198
@@ -184,16 +203,15 @@ class CodeRunner extends ChangeNotifier {
184203 // Cancelled while trying to start.
185204 final pipelineUuid = runResponse? .pipelineUuid;
186205 if (pipelineUuid != null ) {
187- await codeClient ? .cancelExecution (pipelineUuid);
206+ await client .cancelExecution (pipelineUuid);
188207 }
189208 return ;
190209 }
191210
192211 await Future .delayed (_statusCheckInterval);
193212
194213 while (! _result! .isFinished) {
195- final statusResponse =
196- await codeClient! .checkStatus (runResponse.pipelineUuid);
214+ final statusResponse = await client.checkStatus (runResponse.pipelineUuid);
197215
198216 final result = await _getPipelineResult (
199217 runResponse.pipelineUuid,
0 commit comments