我试图检查设备的语言以显示/隐藏某些元素我试着类似于
@override
initState() {
super.initState();
if (Localizations.localeOf(context).countryCode == 'en') {
en = true;
} else {
en = false;
}
}
但是回报
The following assertion was thrown building Builder:
I/flutter ( 8804): inheritFromWidgetOfExactType(_LocalizationsScope) or inheritFromElement() was called before
I/flutter ( 8804): _FormViewPage.initState() completed.
I/flutter ( 8804): When an inherited widget changes, for example if the value of Theme.of() changes, its dependent
I/flutter ( 8804): widgets are rebuilt. If the dependent widget's reference to the inherited widget is in a constructor
I/flutter ( 8804): or an initState() method, then the rebuilt dependent widget will not reflect the changes in the
I/flutter ( 8804): inherited widget.
I/flutter ( 8804): Typically references to inherited widgets should occur in widget build() methods. Alternatively,
I/flutter ( 8804): initialization based on inherited widgets can be placed in the didChangeDependencies method, which
I/flutter ( 8804): is called after initState and whenever the dependencies change thereafter.
如果我用
didChangeDependencies
不返回错误,但不运行if..else语句,我需要在开始时运行。
所以:有人知道我该如何正确检查语言吗?