在streamBuilder中使用提供程序

时间:2020-01-04 19:21:07

标签: flutter dart

当我使用以下代码时: 在audioService插件中

              StreamBuilder<ScreenState>(
                  stream: Rx.combineLatest3<List<MediaItem>, MediaItem,
                          PlaybackState, ScreenState>(
                      AudioService.queueStream,
                      AudioService.currentMediaItemStream,
                      AudioService.playbackStateStream,
                      (queue, mediaItem, playbackState) =>
                          ScreenState(queue, mediaItem, playbackState)),
                  builder: (context, snapshot) {
                    final screenState = snapshot.data;
                    return Consumer<AudioServiceSubscription>(
                      builder: (_, audioSubscription, ch) {
                        audioSubscription.setQueue(screenState?.queue);
                        audioSubscription.setMediaItem(screenState?.mediaItem);
                        audioSubscription
                            .setPlaybackState(screenState?.playbackState);
                        return ch;
                      },
                      child: _palyer(),
                    );
                  },
                ),

我得到了错误

setState() or markNeedsBuild() called during build.

我的提供程序类是

class AudioServiceSubscription with ChangeNotifier {
  List<MediaItem> _queue;
  MediaItem _mediaItem;
  PlaybackState _playbackState;

  List<MediaItem> get queue => _queue;
  MediaItem get mediaItem => _mediaItem;
  PlaybackState get playbackState => _playbackState;
  BasicPlaybackState get basicPlaybackState =>
      _playbackState?.basicState ?? BasicPlaybackState.none;

  void setQueue(List<MediaItem> queue) {
    _queue = queue;
    notifyListeners();
  }

  void setMediaItem(MediaItem mediaItem) {
    _mediaItem = mediaItem;
    notifyListeners();
  }

  void setPlaybackState(PlaybackState playbackState) {
    _playbackState = playbackState;
    notifyListeners();
  }
}

我认为我得到错误的原因是我们不能在streamBuilder的生成器中使用provider 而且我不知道如何正确执行此操作?

0 个答案:

没有答案