[英]Testing in flutter gives error MediaQuery.of() called with a context that does not contain a MediaQuery
I am trying to test the code for the login page in a flutter. 我正在尝试在混乱中测试登录页面的代码。 This is the first time for me and I am just following the template provided by them. 这是我的第一次,我只是遵循他们提供的模板。 But whatever widget I try to push gives an error saying MediaQuery.of() called with a context that does not contain a MediaQuery. 但是我尝试推送的任何小部件都会出现一个错误,提示MediaQuery.of()在不包含MediaQuery的上下文中调用。
I have tried out by pumping different widgets in my app. 我已经尝试通过在应用程序中添加不同的小部件来进行尝试。 But each and every widget gives the same error as mentioned above, on the other hand, my app is working fine on the device but while testing it gives the error for just pumping the widget. 但是,每个小部件都会产生与上述相同的错误,另一方面,我的应用在设备上运行正常,但在测试时会给出仅抽取小部件的错误。
await tester.pumpWidget(Login());
and Login page is simple Scaffold with appbar and body. 登录页面是简单的脚手架,带有appbar和body。
Add this helper method : 添加此辅助方法:
Widget buildTestableWidget(Widget widget) {
return MediaQuery(data: MediaQueryData(), child: MaterialApp(home: widget));
}
Then you can use inside your Test: 然后,您可以在测试内部使用:
await tester.pumpWidget(buildTestableWidget(Login()));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.