'Use provider in streamBuilder
When I use code below : In audioService plugin
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(),
);
},
),
I get the error
setState() or markNeedsBuild() called during build.
my provider class is
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();
}
}
I think the reason I get error is that we can't use provider in builder of streamBuilder And I don't know any idea that how to do this correctly?
Solution 1:[1]
We can use provider in streambuilder buid we cannot set listenable value in it becouse before building UI we cannot set value which is listenable. We can also not use SetState() in it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Mubashar |