【发布时间】:2022-11-10 09:25:56
【问题描述】:
我想在推送页面时显示对话框。所以我在 initState 中显示对话框:
class _Page2State extends State<_Page2> {
@override
void initState() {
super.initState();
_showDialog(context: context);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(color: Colors.red),
);
}
}
void _showDialog({required BuildContext context}) {
showCupertinoModalPopup(
context: context,
builder: (context) {
return Container(
width: 300,
height: 100,
color: Colors.green,
);
},
);
}
毫不奇怪,错误显示:
dependOnInheritedWidgetOfExactType<_InheritedCupertinoTheme>() or dependOnInheritedElement() was called before _Page2State.initState() completed.
为了解决它,我更改了代码:
@override
void initState() {
super.initState();
WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
_showDialog(context: context);
});
}
有用。但我想知道有没有更优雅的方式?
【问题讨论】: