Repository navigation
Description
I ran into an interesting issue while using flutter sounds to play some sounds
Basically, when SoundPlayerUI is disposed, it needs to stop playing the current sound.
@override
Future<void> dispose() async {
Log.d('stopping Player on dispose');
await _stop(supressState: true);
await _player.release();
super.dispose();
}The above execution will cause the following assertion throw
════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown while finalizing the widget tree:
SoundPlayerUIState.dispose failed to call super.dispose.dispose() implementations must always call their superclass dispose() method, to ensure that all the resources used by the widget are fully released.
If I swap the call to super.dispose() to the first line of the method, no assertion error will be thrown. I guessed it was because of the await calls which would take a split second to execute. It seems that calling super.dispose() at the beginning of the method wouldn't cause much trouble also. It just doesn't feel right. Even flutter doc suggested this:
If you override this, make sure to end your method with a call to super.dispose().
(No assertion thrown for the below order)
@override
Future<void> dispose() async {
super.dispose(); // call super.dispose() here doesn't seem to hurt, but feel weird
Log.d('stopping Player on dispose');
await _stop(supressState: true);
await _player.release();
}Steps to Reproduce
- Override state's
dispose()withasync/await - Calling
super.dispose()afterawaitcause assertion to be thrown
Expected results:
Calling super.dispose() before await is working without assertion thrown. Expecting that position of super.dispose() at the beginning or at the end of the @override dispose() wouldn't cause different behaviour.
Actual results:
Calling super.dispose() at the end of the method causes assertion to be thrown.
(flutter doctor below)
$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 1.20.1, on Microsoft Windows [Version 10.0.19041.450], locale en-AU)
[!] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
X Android license status unknown.
Try re-installing or updating your Android SDK Manager.
See https://developer.android.com/studio/#downloads or visit https://flutter.dev/docs/get-started/install/windows#android-setup for detailed instructions.
[√] Android Studio (version 4.0)
[!] IntelliJ IDEA Community Edition (version 2017.3)
X Flutter plugin not installed; this adds Flutter specific functionality.
X Dart plugin not installed; this adds Dart specific functionality.
[√] VS Code, 64-bit edition (version 1.48.1)
[√] Connected device (1 available)
! Doctor found issues in 2 categories.
(I don't think the 2 issues highlighted above by doctor would have any impact ;)
Activity
TahaTesser commentedon Aug 31, 2020
Hi @mrducnguyen
Can you please provide your
flutter doctor -vyourflutter run --verboseand a complete reproducible minimal code sampleThank you
mrducnguyen commentedon Aug 31, 2020
As you wished @TahaTesser :), here's a minimal runnable
main.dart,flutter doctor -vandflutter run --verbosecame after that.[-]Async calls in state override dispose()[/-][+]Async calls in override dispose() - stateful widget[/+]TahaTesser commentedon Sep 1, 2020
Hi @mrducnguyen
You should use Future.delayed with zero duration to execute futures in
initordisposePlease try this code sample
code sample
This won't throw an exception and it will print value after you navigate back
Hope this resolves your issue
mrducnguyen commentedon Sep 1, 2020
Thanks @TahaTesser for your comment but that was not what I was asking. Using
.then()syntax basically convertsdispose()back to a normal synchronous function. We don't really need aFuture.delayed(Duration.zero..., just chain.then()directly to theasyncfunction.I'm keening on using
async/awaitsyntax and not chaining.then()if possible.[-]Async calls in override dispose() - stateful widget[/-][+]Async calls in override dispose() throws exception when dispose function is async [/+]jason-simmons commentedon Sep 4, 2020
super.dispose()must be called synchronously from the state'sdispose()method.StatefulElement.unmountis checking whethersuper.dispose()was called immediately after the state'sdispose()returns. The asyncdispose()in the example will return a future to its caller and schedule tasks for deferred execution.When the future is returned
StatefulElement.unmountthen continues and runs the check. But the call tosuper.dispose()will happen in a deferred task and has not yet executed, so the check failsdispose()can callsuper.dispose()and then start the asynchronous operations:TahaTesser commentedon Sep 4, 2020
Hi @mrducnguyen
Please at look this above #64935 (comment)
Let me know if this answers your question
Thank you
mrducnguyen commentedon Sep 7, 2020
Thanks for all the comments 👍
So, calling
super.dispose()before any calls to async methods is how it is supposed to be.mhbdev commentedon Jan 30, 2021
I tried to stop tracking media streams in flutter web after calling super.dispose() but I do not have any access to streams after calling super.dispose().
So what to do?
J-Ridgwell commentedon Mar 10, 2021
@TahaTesser
Slightly off topic but wouldn't it make more sense to use
Future.microTaskinstead of aFuture.delayedwith 0 seconds of delay? I see them both as accomplishing the same thing but the microTask I'd say is more straightforwardTahaTesser commentedon Mar 11, 2021
@J-Ridgwell
Thanks!
github-actions commentedon Aug 4, 2021
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -vand a minimal reproduction of the issue.