【发布时间】:2021-08-02 21:19:49
【问题描述】:
我有使用包Asset Audio Player 和Flutter Riverpod 作为状态管理的音乐播放器应用程序。我想听两件事:
- 收听歌曲的当前时长
- 收听当前播放的歌曲
final currentSongPosition = StreamProvider.autoDispose<Map<String, dynamic>>((ref) async* {
final AssetsAudioPlayer player = ref.watch(globalAudioPlayers).state;
ref.onDispose(() => player.dispose());
final Stream<double> _first = player.currentPosition.map((event) => event.inSeconds.toDouble());
final Stream<double> _second =
player.current.map((event) => event?.audio.duration.inSeconds.toDouble() ?? 0.0);
final maps = {};
maps['currentDuration'] = _first;
maps['maxDurationSong'] = _second;
return maps; << Error The return type 'Map<dynamic, dynamic>' isn't a 'Stream<Map<String, dynamic>>', as required by the closure's context.
});
如何将 2 个流返回到 1 个 StreamProvider 然后我可以像这样简单地使用:
Consumer(
builder: (_, watch, __) {
final _currentSong = watch(currentSongPosition);
return _currentSong.when(
data: (value) {
final _currentDuration = value['currentDuration'] as double;
final _maxDuration = value['maxDuration'] as double;
return Text('Current : $_currentDuration, Max :$_maxDuration');
},
loading: () => Center(child: CircularProgressIndicator()),
error: (error, stackTrace) => Text('Error When Playing Song'),
);
},
),
【问题讨论】:
-
您应该将答案添加为答案,而不是作为问题的编辑。
-
另外,我的回答没有用吗?它解决了您对使用 rxdart 的解决方案提出的担忧。
-
@AlexHartford 你的回答和我预期的一样,我只是在我的问题中添加了另一种方法。
-
@AlexHartford 我对我的案例的最佳实践感到好奇,最好像您的方法那样分离 StreamProvider ,或者我们可以使用我的方法吗?我的问题的重点是,它更多是关于代码效率
-
我不能保证我的答案是“最佳实践”,但它是解决问题的持久、易于理解的方法。