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);
});
}
}









share|improve this question




























    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);
    });
    }
    }









    share|improve this question


























      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);
      });
      }
      }









      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 12:20

























      asked Nov 11 at 11:24









      Purus

      3,63342561




      3,63342561
























          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.






          share|improve this answer





















          • 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 your State 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











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          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

























          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.






          share|improve this answer





















          • 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 your State 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















          up vote
          0
          down vote













          Wrap your code



          if(mounted) {
          setState(() { ... });
          }


          to avoid setState() being called when the widget is not mounted.






          share|improve this answer





















          • 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 your State 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













          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.






          share|improve this answer












          Wrap your code



          if(mounted) {
          setState(() { ... });
          }


          to avoid setState() being called when the widget is not mounted.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          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 your State 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






          • 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 your State 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


















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues