From 35e95088e3764328f92f842004218d6f8241ce4b Mon Sep 17 00:00:00 2001 From: Lucas SAUDON Date: Tue, 2 Jun 2026 11:58:21 +0200 Subject: [PATCH] fix: complete future with current value instead of null Completing _futureCompleter with null fails for non-nullable TResult. Use the current value (already typed as TResult) so the completion is type-correct for every result type, nullable or not. --- lib/command_it.dart | 2 +- test/flutter_command_test.dart | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/command_it.dart b/lib/command_it.dart index dde69a3..56fc3d9 100644 --- a/lib/command_it.dart +++ b/lib/command_it.dart @@ -817,7 +817,7 @@ abstract class Command extends CustomValueNotifier { _errors.dispose(); _handle?.dispose(); if (!(_futureCompleter?.isCompleted ?? true)) { - _futureCompleter!.complete(null); + _futureCompleter!.complete(value); _futureCompleter = null; } diff --git a/test/flutter_command_test.dart b/test/flutter_command_test.dart index 8836a46..a48d390 100644 --- a/test/flutter_command_test.dart +++ b/test/flutter_command_test.dart @@ -2135,6 +2135,25 @@ void main() { final result = await future; expect(result, null); }); + + test('Dispose while future is pending completes it with current value', + () async { + final command = Command.createAsyncNoParam( + () async { + await Future.delayed(const Duration(milliseconds: 200)); + return 'result'; + }, + initialValue: 'initial', + ); + + final future = command.runAsync(); + + await Future.delayed(const Duration(milliseconds: 50)); + command.dispose(); + + final result = await future; + expect(result, 'initial'); + }); }); group('Command Utilities and Properties', () {