I am using bloc. But I found very rare issue i.e Stream is working well on one page but at the same time when we navigate to another page it is showing in ConnectionState.waiting
Here is my piece of code:
In bloc:
late StreamController<String> _timeRemainingController=StreamController.broadcast();
Stream<String> get timeRemainingStream => _timeRemainingController.stream;
// While adding value:
_timeRemainingController.add("");
Here is streambuilder which I am using
StreamBuilder<String>(
stream: _clockTimerBloc.timeRemainingStream,
builder: (context, snapshot) {
if (snapshot.hasData) {
//code
}
Common code which used everywhere. I am stuck there Please try to check. And thanks for any response.
Related
I am a beginner in flutter, I want to retrieve a list of announcements from the database and display it in a listView but I have a compilation error under snapshot.data saying that A value of type 'List<Annonce>?' can't be assigned to a variable of type 'List<Annonce>'. Try changing the type of the variable, or casting the right-hand type to 'List<Annonce>'.
The code is :
future: AnnonceDataBase.instance.annonces(),
builder: (BuildContext context,
AsyncSnapshot<List<Annonce>> snapshot) {
if (snapshot.hasData) {
List<Annonce> annonces = snapshot.data;
return ListView.separated(... ```
Change like this:
future: AnnonceDataBase.instance.annonces(),
builder: (BuildContext context,
AsyncSnapshot snapshot) {
if (snapshot.hasData) {
List<Annonce> annonces = snapshot.data! as List<Annonce>;
return ListView.separated(... ```
Oh, by the way, Please consider to vote up and give it as accepted answer if an answer solves your problem. The reason is that other people with same issue can easily find the right answer easily. Have fun!
I am using just_audio plugin for flutter by Ryan Heise. I am able to get events of buffering, ready, loading, completed, etc.
Is there a way to get an event of a song completing in a playlist? I can't seem to find one in the documentation. We need to update the UI to show the next song is playing.
Thank you for your help with this.
You should be able to use a StreamBuilder which listens to just_audio's currentIndexStream. This way, there will always be up-to-date info: If the state updates, your build triggers and the widget showing the data is rebuilt.
In the setup on pub, different available streams for you to listen to are listed, among which is the currentIndexStream, which I assume solves your problem by streaming the current value (which, in conjunction with your list of songs should be able to get you the info you need)
StreamBuilder(
stream: player.currentIndexStream,
builder: (context, AsyncSnapshot snapshot) {
int currentIndex = snapshot.data ?? 0;
print(currentIndex.toString());
return MySongInfoWidget(song: mySongList[currentIndex]);
}
);
The code above should at least give you an idea of how to approach this problem, take a look at the StreamBuilder docs for more info.
A bit more complete example (to show how a streambuilder could be used inside build):
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(...),
body: Column(children: [
myStaticWidget(...),
StreamBuilder(
stream: player.currentIndexStream,
builder: (context, AsyncSnapshot snapshot) {
int currentIndex = snapshot.data ?? 0;
return Column(children: [
Text('Current song #$currentIndex'),
Text('Song name: ${mySongs[currentIndex].name}');
]);
}
),
]),
);
}
I'm trying get a callback after my context build, i already tried with "WidgetsBinding.instance!.addPostFrameCallback", but not works because i have a StreamBuilder and the first load of snapshot data it is null.
I expected exists a callback of StreamBuilder after data completely load and build loaded, here is my code:
StreamBuilder(
stream: this.paymentInfoFormPresenter.key,
builder: (context, snapshot) {
final _formKey = snapshot.data;
if (_formKey == null) {
return Container();
}
return Text('example');
}
);
And i don't find anything about on internet... i hope someone can help me.
To resolve this issue, i needed change the "Text('example')" to an external widget, and added "WidgetsBinding.instance!.addPostFrameCallback" inside of this external widget
I have a simple function that returns a Stream. Works great:
Stream<List<User>> streamPeopleToFollow({#required User startAfterUser}) {
Query query = _usersRef.orderBy('dateTime', descending: false).limit(20);
if (startAfterUser != null)
query = query.startAfter([startAfterUser.dateTime.toIso8601String()]);
return query.snapshots().map((snapshot) {
return snapshot.docs.map((doc) {
return User.fromJson(doc.data());
}).toList();
});
}
I give to a StreamBuilder:
:
return StreamBuilder(
stream: streamPeopleToFollow(startAfterUser),
builder: (context, snapshot) {
:
When "startAfterUser" changes, the widget with the StreamBuilder rebuilds and I get my updated list, so the old stream is abondoned. This all is working fine. My question is about the disposed Stream--I don't see any "close" or "cancel" functions in the Stream class. Does it close when its disposed? I obviously want to avoid dangling streams.
a Flutter beginner here so if my question is stupid don't mind it...
How can I convert a Stream to a Future?
I have a Stream that just calls the requested URL multiple times because it's a Stream. I want to be able to get the data and not the loading state... Because I always just get loading forever
Is there something like Future.fromStream() function somewhere and I'm missing it?
Can I achieve this?
I didn't provide any code because I think it's not needed if you need the code, I can edit the question
Stream has firstWhere method, this will return Future
_profileService.profileStream.firstWhere((element) => false);
Stream and Future are two different concepts.
As you are using, stream is keeping the data updated, and Future is just one time.
Example of using stream: Shopping Cart, listing of items
Example of using future: getUserDetails after login.
If you're using stream, then you can use streambuilder to build your UI
StreamBuilder<User>(
stream: userBloc.author, // your stream here
builder:
(BuildContext context, AsyncSnapshot<User> snapshot) {
if (snapshot.connectionState == ConnectionState.active && snapshot.hasData) {
// do your work here
} else {
return CircularprogressIndicator();
}
},
)