Check if Stateless widget is disposed in flutter

南楼画角 提交于 2020-05-14 19:11:08

问题


When my stateless widget built I play some sounds in sequence order by using this code:

await _audioPlayer.play(contentPath1, isLocal: true);
await Future.delayed(Duration(seconds: 4));
await _audioPlayer.play(contentPath2, isLocal: true);
await Future.delayed(Duration(seconds: 4));
await _audioPlayer.play(contentPath3, isLocal: true);

when the user closes the current widget before finish playing the sounds, The sounds still work even after closing the current route by using this code:

Navigator.pop(context);

my workaround is to use a boolean variable to indicate if the closing action has done.

Playing sound code:

await _audioPlayer.play(contentPath1, isLocal: true);
if (closed) return;
await Future.delayed(Duration(seconds: 4));
if (closed) return;
await _audioPlayer.play(contentPath2, isLocal: true);
if (closed) return;
await Future.delayed(Duration(seconds: 4));
if (closed) return;
await _audioPlayer.play(contentPath3, isLocal: true);

Closing the current widget:

closed = true;
_audioPlayer.stop();

Are there a better way to stop the async methods if my widget closed?


回答1:


If you change your widget to a StatefulWidget then you can have a function like the following:

void _playSounds() {
  await _audioPlayer.play(contentPath1, isLocal: true);
  await Future.delayed(Duration(seconds: 4));
  if (!mounted) return;

  await _audioPlayer.play(contentPath2, isLocal: true);
  await Future.delayed(Duration(seconds: 4));
  if (!mounted) return;

  await _audioPlayer.play(contentPath3, isLocal: true);
}

and then in the dispose method just dispose of the player:

@override
void dispose() {
  super.dispose();
  _audioPlayer?.dispose();
}


来源:https://stackoverflow.com/questions/52917847/check-if-stateless-widget-is-disposed-in-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!