Using dispose() when using StatefulBuilder in a StatelessWidget
up vote
0
down vote
favorite
I am using a StatefulBuilder
widget to update my values and it all works fine.
StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return Mywidget();
});
Since this widget is in a Tab view, when I scroll to the next tab, I get the below exception in console.
E/flutter ( 7147): [ERROR:flutter/shell/common/shell.cc(188)] Dart Error: Unhandled exception:
E/flutter ( 7147): setState() called after dispose(): _StatefulBuilderState#09a22(lifecycle state: defunct, not mounted)
E/flutter ( 7147): This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error can occur when code calls setState() from a timer or an animation callback. The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree.
E/flutter ( 7147): This error might indicate a memory leak if setState() is being called because another object is retaining a reference to this State object after it has been removed from the tree. To avoid memory leaks, consider breaking the reference to this object during dispose().
E/flutter ( 7147): #0 State.setState.<anonymous closure> (package:flutter/src/widgets/framework.dart:1097:9)
E/flutter ( 7147): #1 State.setState (package:flutter/src/widgets/framework.dart:1123:6)
E/flutter ( 7147): #2 ImageItem.actionColumn.<anonymous closure>.<anonymous closure> (package:silkthread/widgets/image_item.dart:197:19)
E/flutter ( 7147): #3 _RootZone.runUnary (dart:async/zone.dart:1379:54)
E/flutter ( 7147): #4 _FutureListener.handleValue (dart:async/future_impl.dart:129:18)
E/flutter ( 7147): #5 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:642:45)
E/flutter ( 7147): #6 Future._propagateToListeners (dart:async/future_impl.dart:671:32)
E/flutter ( 7147): #7 Future._complete (dart:async/future_impl.dart:476:7)
E/flutter ( 7147): #8 _SyncCompleter.complete (dart:async/future_impl.dart:51:12)
E/flutter ( 7147): #9 _RootZone.runUnary (dart:async/zone.dart:1379:54)
E/flutter ( 7147): #10 _FutureListener.handleValue (dart:async/future_impl.dart:129:18)
E/flutter ( 7147): #11 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:642:45)
E/flutter ( 7147): #12 Future._propagateToListeners (dart:async/future_impl.dart:671:32)
E/flutter ( 7147): #13 Future._complete (dart:async/future_impl.dart:476:7)
E/flutter ( 7147): #14 _SyncCompleter.complete (dart:async/future_impl.dart:51:12)
E/flutter ( 7147): #15 _AsyncAwaitCompleter.complete.<anonymous closure> (dart:async/runtime/libasync_patch.dart:33:20)
E/flutter ( 7147): #16 _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter ( 7147): #17 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
Since the page which has StatefulBuilder
widget is a StatelessWidget
, I can't call dispose()
method.
So while using StatefulBuilder
how can I avoid this exception?
Edit:
I am trying something same as mentioned in the article https://medium.com/flutter-community/stateful-widgets-be-gone-stateful-builder-a67f139725a0.
class ImageItem extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
setState(() {
likesCount = post.likesCount;
});
return ActionChip(label: Text(likesCount);
});
}
}
dart flutter
add a comment |
up vote
0
down vote
favorite
I am using a StatefulBuilder
widget to update my values and it all works fine.
StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return Mywidget();
});
Since this widget is in a Tab view, when I scroll to the next tab, I get the below exception in console.
E/flutter ( 7147): [ERROR:flutter/shell/common/shell.cc(188)] Dart Error: Unhandled exception:
E/flutter ( 7147): setState() called after dispose(): _StatefulBuilderState#09a22(lifecycle state: defunct, not mounted)
E/flutter ( 7147): This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error can occur when code calls setState() from a timer or an animation callback. The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree.
E/flutter ( 7147): This error might indicate a memory leak if setState() is being called because another object is retaining a reference to this State object after it has been removed from the tree. To avoid memory leaks, consider breaking the reference to this object during dispose().
E/flutter ( 7147): #0 State.setState.<anonymous closure> (package:flutter/src/widgets/framework.dart:1097:9)
E/flutter ( 7147): #1 State.setState (package:flutter/src/widgets/framework.dart:1123:6)
E/flutter ( 7147): #2 ImageItem.actionColumn.<anonymous closure>.<anonymous closure> (package:silkthread/widgets/image_item.dart:197:19)
E/flutter ( 7147): #3 _RootZone.runUnary (dart:async/zone.dart:1379:54)
E/flutter ( 7147): #4 _FutureListener.handleValue (dart:async/future_impl.dart:129:18)
E/flutter ( 7147): #5 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:642:45)
E/flutter ( 7147): #6 Future._propagateToListeners (dart:async/future_impl.dart:671:32)
E/flutter ( 7147): #7 Future._complete (dart:async/future_impl.dart:476:7)
E/flutter ( 7147): #8 _SyncCompleter.complete (dart:async/future_impl.dart:51:12)
E/flutter ( 7147): #9 _RootZone.runUnary (dart:async/zone.dart:1379:54)
E/flutter ( 7147): #10 _FutureListener.handleValue (dart:async/future_impl.dart:129:18)
E/flutter ( 7147): #11 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:642:45)
E/flutter ( 7147): #12 Future._propagateToListeners (dart:async/future_impl.dart:671:32)
E/flutter ( 7147): #13 Future._complete (dart:async/future_impl.dart:476:7)
E/flutter ( 7147): #14 _SyncCompleter.complete (dart:async/future_impl.dart:51:12)
E/flutter ( 7147): #15 _AsyncAwaitCompleter.complete.<anonymous closure> (dart:async/runtime/libasync_patch.dart:33:20)
E/flutter ( 7147): #16 _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter ( 7147): #17 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
Since the page which has StatefulBuilder
widget is a StatelessWidget
, I can't call dispose()
method.
So while using StatefulBuilder
how can I avoid this exception?
Edit:
I am trying something same as mentioned in the article https://medium.com/flutter-community/stateful-widgets-be-gone-stateful-builder-a67f139725a0.
class ImageItem extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
setState(() {
likesCount = post.likesCount;
});
return ActionChip(label: Text(likesCount);
});
}
}
dart flutter
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am using a StatefulBuilder
widget to update my values and it all works fine.
StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return Mywidget();
});
Since this widget is in a Tab view, when I scroll to the next tab, I get the below exception in console.
E/flutter ( 7147): [ERROR:flutter/shell/common/shell.cc(188)] Dart Error: Unhandled exception:
E/flutter ( 7147): setState() called after dispose(): _StatefulBuilderState#09a22(lifecycle state: defunct, not mounted)
E/flutter ( 7147): This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error can occur when code calls setState() from a timer or an animation callback. The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree.
E/flutter ( 7147): This error might indicate a memory leak if setState() is being called because another object is retaining a reference to this State object after it has been removed from the tree. To avoid memory leaks, consider breaking the reference to this object during dispose().
E/flutter ( 7147): #0 State.setState.<anonymous closure> (package:flutter/src/widgets/framework.dart:1097:9)
E/flutter ( 7147): #1 State.setState (package:flutter/src/widgets/framework.dart:1123:6)
E/flutter ( 7147): #2 ImageItem.actionColumn.<anonymous closure>.<anonymous closure> (package:silkthread/widgets/image_item.dart:197:19)
E/flutter ( 7147): #3 _RootZone.runUnary (dart:async/zone.dart:1379:54)
E/flutter ( 7147): #4 _FutureListener.handleValue (dart:async/future_impl.dart:129:18)
E/flutter ( 7147): #5 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:642:45)
E/flutter ( 7147): #6 Future._propagateToListeners (dart:async/future_impl.dart:671:32)
E/flutter ( 7147): #7 Future._complete (dart:async/future_impl.dart:476:7)
E/flutter ( 7147): #8 _SyncCompleter.complete (dart:async/future_impl.dart:51:12)
E/flutter ( 7147): #9 _RootZone.runUnary (dart:async/zone.dart:1379:54)
E/flutter ( 7147): #10 _FutureListener.handleValue (dart:async/future_impl.dart:129:18)
E/flutter ( 7147): #11 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:642:45)
E/flutter ( 7147): #12 Future._propagateToListeners (dart:async/future_impl.dart:671:32)
E/flutter ( 7147): #13 Future._complete (dart:async/future_impl.dart:476:7)
E/flutter ( 7147): #14 _SyncCompleter.complete (dart:async/future_impl.dart:51:12)
E/flutter ( 7147): #15 _AsyncAwaitCompleter.complete.<anonymous closure> (dart:async/runtime/libasync_patch.dart:33:20)
E/flutter ( 7147): #16 _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter ( 7147): #17 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
Since the page which has StatefulBuilder
widget is a StatelessWidget
, I can't call dispose()
method.
So while using StatefulBuilder
how can I avoid this exception?
Edit:
I am trying something same as mentioned in the article https://medium.com/flutter-community/stateful-widgets-be-gone-stateful-builder-a67f139725a0.
class ImageItem extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
setState(() {
likesCount = post.likesCount;
});
return ActionChip(label: Text(likesCount);
});
}
}
dart flutter
I am using a StatefulBuilder
widget to update my values and it all works fine.
StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return Mywidget();
});
Since this widget is in a Tab view, when I scroll to the next tab, I get the below exception in console.
E/flutter ( 7147): [ERROR:flutter/shell/common/shell.cc(188)] Dart Error: Unhandled exception:
E/flutter ( 7147): setState() called after dispose(): _StatefulBuilderState#09a22(lifecycle state: defunct, not mounted)
E/flutter ( 7147): This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error can occur when code calls setState() from a timer or an animation callback. The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree.
E/flutter ( 7147): This error might indicate a memory leak if setState() is being called because another object is retaining a reference to this State object after it has been removed from the tree. To avoid memory leaks, consider breaking the reference to this object during dispose().
E/flutter ( 7147): #0 State.setState.<anonymous closure> (package:flutter/src/widgets/framework.dart:1097:9)
E/flutter ( 7147): #1 State.setState (package:flutter/src/widgets/framework.dart:1123:6)
E/flutter ( 7147): #2 ImageItem.actionColumn.<anonymous closure>.<anonymous closure> (package:silkthread/widgets/image_item.dart:197:19)
E/flutter ( 7147): #3 _RootZone.runUnary (dart:async/zone.dart:1379:54)
E/flutter ( 7147): #4 _FutureListener.handleValue (dart:async/future_impl.dart:129:18)
E/flutter ( 7147): #5 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:642:45)
E/flutter ( 7147): #6 Future._propagateToListeners (dart:async/future_impl.dart:671:32)
E/flutter ( 7147): #7 Future._complete (dart:async/future_impl.dart:476:7)
E/flutter ( 7147): #8 _SyncCompleter.complete (dart:async/future_impl.dart:51:12)
E/flutter ( 7147): #9 _RootZone.runUnary (dart:async/zone.dart:1379:54)
E/flutter ( 7147): #10 _FutureListener.handleValue (dart:async/future_impl.dart:129:18)
E/flutter ( 7147): #11 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:642:45)
E/flutter ( 7147): #12 Future._propagateToListeners (dart:async/future_impl.dart:671:32)
E/flutter ( 7147): #13 Future._complete (dart:async/future_impl.dart:476:7)
E/flutter ( 7147): #14 _SyncCompleter.complete (dart:async/future_impl.dart:51:12)
E/flutter ( 7147): #15 _AsyncAwaitCompleter.complete.<anonymous closure> (dart:async/runtime/libasync_patch.dart:33:20)
E/flutter ( 7147): #16 _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter ( 7147): #17 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
Since the page which has StatefulBuilder
widget is a StatelessWidget
, I can't call dispose()
method.
So while using StatefulBuilder
how can I avoid this exception?
Edit:
I am trying something same as mentioned in the article https://medium.com/flutter-community/stateful-widgets-be-gone-stateful-builder-a67f139725a0.
class ImageItem extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
setState(() {
likesCount = post.likesCount;
});
return ActionChip(label: Text(likesCount);
});
}
}
dart flutter
dart flutter
edited Nov 11 at 12:20
asked Nov 11 at 11:24
Purus
3,63342561
3,63342561
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Wrap your code
if(mounted) {
setState(() { ... });
}
to avoid setState()
being called when the widget is not mounted.
It says undefined name when used inside builder method of StatefulBuilder.
– Purus
Nov 11 at 11:38
1
Hard to tell. Can you please add more code to your question.
– Günter Zöchbauer
Nov 11 at 11:40
It should be in yourState
class, the class which extends <State>
– CopsOnRoad
Nov 11 at 11:40
1
Please check the updated code.
– Purus
Nov 11 at 12:20
2
I'd suggest to not use stateful builder at all. This is rarely a good idea. Prefer a StatefulWidget instead
– Rémi Rousselet
Nov 11 at 13:57
|
show 10 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Wrap your code
if(mounted) {
setState(() { ... });
}
to avoid setState()
being called when the widget is not mounted.
It says undefined name when used inside builder method of StatefulBuilder.
– Purus
Nov 11 at 11:38
1
Hard to tell. Can you please add more code to your question.
– Günter Zöchbauer
Nov 11 at 11:40
It should be in yourState
class, the class which extends <State>
– CopsOnRoad
Nov 11 at 11:40
1
Please check the updated code.
– Purus
Nov 11 at 12:20
2
I'd suggest to not use stateful builder at all. This is rarely a good idea. Prefer a StatefulWidget instead
– Rémi Rousselet
Nov 11 at 13:57
|
show 10 more comments
up vote
0
down vote
Wrap your code
if(mounted) {
setState(() { ... });
}
to avoid setState()
being called when the widget is not mounted.
It says undefined name when used inside builder method of StatefulBuilder.
– Purus
Nov 11 at 11:38
1
Hard to tell. Can you please add more code to your question.
– Günter Zöchbauer
Nov 11 at 11:40
It should be in yourState
class, the class which extends <State>
– CopsOnRoad
Nov 11 at 11:40
1
Please check the updated code.
– Purus
Nov 11 at 12:20
2
I'd suggest to not use stateful builder at all. This is rarely a good idea. Prefer a StatefulWidget instead
– Rémi Rousselet
Nov 11 at 13:57
|
show 10 more comments
up vote
0
down vote
up vote
0
down vote
Wrap your code
if(mounted) {
setState(() { ... });
}
to avoid setState()
being called when the widget is not mounted.
Wrap your code
if(mounted) {
setState(() { ... });
}
to avoid setState()
being called when the widget is not mounted.
answered Nov 11 at 11:26
Günter Zöchbauer
309k64911860
309k64911860
It says undefined name when used inside builder method of StatefulBuilder.
– Purus
Nov 11 at 11:38
1
Hard to tell. Can you please add more code to your question.
– Günter Zöchbauer
Nov 11 at 11:40
It should be in yourState
class, the class which extends <State>
– CopsOnRoad
Nov 11 at 11:40
1
Please check the updated code.
– Purus
Nov 11 at 12:20
2
I'd suggest to not use stateful builder at all. This is rarely a good idea. Prefer a StatefulWidget instead
– Rémi Rousselet
Nov 11 at 13:57
|
show 10 more comments
It says undefined name when used inside builder method of StatefulBuilder.
– Purus
Nov 11 at 11:38
1
Hard to tell. Can you please add more code to your question.
– Günter Zöchbauer
Nov 11 at 11:40
It should be in yourState
class, the class which extends <State>
– CopsOnRoad
Nov 11 at 11:40
1
Please check the updated code.
– Purus
Nov 11 at 12:20
2
I'd suggest to not use stateful builder at all. This is rarely a good idea. Prefer a StatefulWidget instead
– Rémi Rousselet
Nov 11 at 13:57
It says undefined name when used inside builder method of StatefulBuilder.
– Purus
Nov 11 at 11:38
It says undefined name when used inside builder method of StatefulBuilder.
– Purus
Nov 11 at 11:38
1
1
Hard to tell. Can you please add more code to your question.
– Günter Zöchbauer
Nov 11 at 11:40
Hard to tell. Can you please add more code to your question.
– Günter Zöchbauer
Nov 11 at 11:40
It should be in your
State
class, the class which extends <State>– CopsOnRoad
Nov 11 at 11:40
It should be in your
State
class, the class which extends <State>– CopsOnRoad
Nov 11 at 11:40
1
1
Please check the updated code.
– Purus
Nov 11 at 12:20
Please check the updated code.
– Purus
Nov 11 at 12:20
2
2
I'd suggest to not use stateful builder at all. This is rarely a good idea. Prefer a StatefulWidget instead
– Rémi Rousselet
Nov 11 at 13:57
I'd suggest to not use stateful builder at all. This is rarely a good idea. Prefer a StatefulWidget instead
– Rémi Rousselet
Nov 11 at 13:57
|
show 10 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248230%2fusing-dispose-when-using-statefulbuilder-in-a-statelesswidget%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown