Skip to content

Conversation

@zjsix
Copy link

@zjsix zjsix commented Jul 14, 2025

_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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant