shinonome-inc / qiita_client_yukiy

2 stars 0 forks source link

[FeedPage最終課題]・ページネーション エラーの対処 #10

Closed Yuki0316 closed 1 year ago

Yuki0316 commented 1 year ago

概要

ページネーションmerge後にFeedPage内にエラーが発生したので対処しました。

エラー文

Unhandled Exception: setState() called after dispose(): _FeedPageState#7895b(lifecycle state: defunct, not mounted) 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. 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().

対処方法

エラー文をchatGPTに投げてみたところ、回答が返ってきたのでその通り実装しました。以下画像です。

スクリーンショット 2023-03-04 14 21 57 スクリーンショット 2023-03-04 14 22 10 スクリーンショット 2023-03-04 14 22 19

結果

FeedPage内の全てのsetStateを"if(mounted)"で囲んだところ、エラー文が消えました。

Yoshida-koshi commented 1 year ago

LGTM!