简体   繁体   English

在颤动中进行测试会给出错误MediaQuery.of(),该错误使用不包含MediaQuery的上下文调用

[英]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.

相关问题 Flutter 错误:使用不包含 MediaQuery 的上下文调用 MediaQuery.of() - Flutter Error: MediaQuery.of() called with a context that does not contain a MediaQuery 在使用不包含 MediaQuery 的上下文调用的 flutter MediaQuery.of() 中出现错误 - Getting Error in flutter MediaQuery.of() called with a context that does not contain a MediaQuery Flutter 错误:使用不包含 MediaQuery 的上下文调用 MediaQuery.of() - Flutter Error : MediaQuery.of() called with a context that does not contain a MediaQuery 使用不包含 MediaQuery 的上下文调用 MediaQuery.of() - MediaQuery.of() called with a context that does not contain MediaQuery MediaQuery.of()使用不包含MediaQuery的上下文调用 - MediaQuery.of() called with a context that does not contain a MediaQuery 使用不包含 MediaQuery 的上下文调用 MediaQuery.of()。 错误 - MediaQuery.of() called with a context that does not contain a MediaQuery. error 使用上下文调用的 Mediaquery.of 不包含 mediaquery 错误 - Mediaquery.of called with a context does not contain a mediaquery error (使用不包含 MediaQuery 的上下文调用 MediaQuery.of()。)错误 - (MediaQuery.of() called with a context that does not contain a MediaQuery.) error 使用不包含 MediaQuery 的上下文调用 MediaQuery.of()。 (紧急援助) - MediaQuery.of() called with a context that does not contain a MediaQuery. (emergency aid) 使用不包含 MediaQuery 的上下文(来自 MaterialApp)调用 MediaQuery.of() - MediaQuery.of() called with a context (from MaterialApp) that does not contain a MediaQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM