PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, September 11, 2022

[FIXED] how to dispose setState properly to avoid memory leak?

 September 11, 2022     cross-platform, flutter     No comments   

Issue

It shows me this error.

E/flutter (22343): 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.
E/flutter (22343): 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 (22343): 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().
   @override
      void initState() {
         super.initState();
        if(mounted){
        getProfilePosts();
        getFollowers();
        getFollowing();
        checkIfFollowing();
        }
       
      }
    
      checkIfFollowing() async {
        if(mounted){
        setState(() {
          _isLoading = true;
        });
        DocumentSnapshot doc = await followersRef
            .document(widget.profileId)
            .collection('userFollowers')
            .document(currentUserId)
            .get();
        setState(() {
          _isFollowing = doc.exists;
        });
        setState(() {
          _isLoading = false;
        });
        }
      }
    
      getFollowers() async {
        if(mounted){
         setState(() {
          _isLoading = true;
        });
        QuerySnapshot snapshot = await followersRef
            .document(widget.profileId)
            .collection('userFollowers')
            .getDocuments();
        setState(() {
          followerCount = snapshot.documents.length;
        });
        setState(() {
          _isLoading = false;
        });
        }
      }
    
      getFollowing() async {
        if(mounted){
         setState(() {
          _isLoading = true;
        });
        QuerySnapshot snapshot = await followingRef
            .document(widget.profileId)
            .collection('userFollowing')
            .getDocuments();
        setState(() {
          followingCount = snapshot.documents.length;
        });
        setState(() {
          _isLoading = false;
        });
        }
      }
    
      getProfilePosts() async {
        if(mounted){
        setState(() {
          _isLoading = true;
        });
        QuerySnapshot snapshot = await postsRef
            .document(widget.profileId)
            .collection('userPosts')
            .orderBy('timestamp', descending: true)
            .getDocuments();
        setState(() {
          postCount = snapshot.documents.length;
          posts = snapshot.documents.map((doc) => Post.fromDocument(doc)).toList();
          _isLoading = false;
        });
        }
      }

Solution

Try to do this in all the setState calls, it seems you check that only once, but the following calls to setState are unprotected

if (mounted == true) {
    setState(() {})
}


Answered By - Jav T
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,217,249

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing