I want to ask if there is any other way than using SetState() or notifyListener() to change the value of bool. Because using setState() or notifyListener() crashes app for some seconds. actually i m using NotificationListener to change the value of bool on user scroll. Please help
Asked
Active
Viewed 594 times
-5
-
1you should look for reason of app crashes after call setState() or notifyListener(). – eamirho3ein Oct 12 '22 at 06:44
-
Actually i'm using FutureBuilder inside NotificationListeners and on user scroll i am changing value of bool... i figured out that app only crashed when setstate or notify listeners is called – Usman Hafeez Oct 12 '22 at 06:47
-
Simply not using a method because it crashes your app is not the right way to approach it. Find out why it crashes your app. Try to figure out what exactly is happening. If necessary ask a question here regarding the specific crash, while providing enough details of the crash – Ivo Oct 12 '22 at 06:56
-
@Ivo https://stackoverflow.com/questions/74028523/flutter-notificationlistener-crashed-app-for-some-seconds ....... here is a link to question i Asked yesterday, can you please check it? I also addeed a video of crash – Usman Hafeez Oct 12 '22 at 07:11
-
use `ValueListenableBuilder` for example – pskink Oct 12 '22 at 07:23
-
@pskink i tried but exact same thing happened with that too – Usman Hafeez Oct 12 '22 at 07:29
1 Answers
2
use stream.it is like a pipe you add value from a side (Sink) and recieve it on the other side (Stream).i will try to explain it:
//make a stream controller
StreamController<bool> valueController = StreamController();
//this will listen to every new value you add
Stream valueOutput = valueController.stream;
//you can add new values throw the sink
Sink valueInput = valueController.sink;
so when you want to add a value to the stream just call
valueInput.add(true);//add whatever booleon value you want
and then recieve it by calling
valueOutput.listen((value) {
//this function will be invoked every time you add a new value.
});
or you can add StreamBuilder if you want to rebuild a specific widget based on that boolean value
StreamBuilder(
stream: valueOutput,
builder: (context,snapshot) {
bool value = snapshot.data;
log(value.toString());//true or false
return yourWidget();
})
i hope i explained it well, good luck with your problem.
i think when you use setState it rebuilds the entire screen and if you are using scroll controller to listen to user`s scroll then the screen will rebuild every pixel user scrolls so that is a lot of rendering. maybe share some code snippet of your code so we can help.

ibrahim Eltayfe
- 118
- 2
- 5
-
Thanks for nice explaination brother, can we have a little meeting over zoom or google meet? – Usman Hafeez Oct 14 '22 at 13:11