fix: 将View.of(context) 调用从initState移动到didChangeDependencies,修复报错#552
Open
zjsix wants to merge 1 commit intoLianjiaTech:3.xfrom
Open
fix: 将View.of(context) 调用从initState移动到didChangeDependencies,修复报错#552zjsix wants to merge 1 commit intoLianjiaTech:3.xfrom
zjsix wants to merge 1 commit intoLianjiaTech:3.xfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
_BrnRangeSelectionGroupWidgetState 类的一个问题:
在 initState() 方法中调用 View.of(context),导致 Flutter 抛出如下错误:
dependOnInheritedWidgetOfExactType<_ViewScope>() or dependOnInheritedElement() was called before initState() completed.
问题原因
Flutter 框架禁止在 initState() 中调用依赖 InheritedWidget 的方法
因为此时组件尚未完成构建树插入,无法正确注册依赖
解决方案
将 View.of(context) 相关调用从 initState() 中移至 didChangeDependencies() 方法中,
该方法在组件插入构建树后调用,能安全访问 context 和依赖的 InheritedWidget