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', () {