简体   繁体   English

Flutter 错误:使用不包含 MediaQuery 的上下文调用 MediaQuery.of()

[英]Flutter Error : MediaQuery.of() called with a context that does not contain a MediaQuery

I have just started learning app development via Flutter. I ran into a tutorial to build a simple app.我刚刚开始通过 Flutter 学习应用程序开发。我遇到了一个教程来构建一个简单的应用程序。

My task is to add appbar and I did this:我的任务是添加appbar ,我这样做了:

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    home: Scaffold(
      appBar: AppBar(
        title: Text('Hello '),
      ),
    ),
  ));
}

I am expecting a simple app bar with the title "Hello ".我期待一个标题为“Hello”的简单应用栏。

I get this error in console:我在控制台中收到此错误:

-I/flutter (17741): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (17741): The following assertion was thrown building MyApp:
I/flutter (17741): MediaQuery.of() called with a context that does not contain a MediaQuery.
I/flutter (17741): No MediaQuery ancestor could be found starting from the context that was passed to MediaQuery.of().
I/flutter (17741): This can happen because you do not have a WidgetsApp or MaterialApp widget (those widgets introduce
I/flutter (17741): a MediaQuery), or it can happen if the context you use comes from a widget above those widgets.
I/flutter (17741): The context used was:
I/flutter (17741):   Scaffold
I/flutter (17741):
I/flutter (17741): The relevant error-causing widget was:
I/flutter (17741):   MyApp file:///E:/Flutter/i_am_rich/lib/main.dart:3:23
I/flutter (17741): 
I/flutter (17741): When the exception was thrown, this was the stack:
I/flutter (17741): #0      MediaQuery.of (package:flutter/src/widgets/media_query.dart:798:5)
I/flutter (17741): #1      ScaffoldState.didChangeDependencies (package:flutter/src/material/scaffold.dart:1993:50)
I/flutter (17741): #2      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4376:12)
I/flutter (17741): #3      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
I/flutter (17741): #4      Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
I/flutter (17741): #5      Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
I/flutter (17741): #6      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)

Reloaded 1 of 478 libraries in 437ms.
I/flutter (17741): #7      Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
I/flutter (17741): #8      BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2432:33)
I/flutter (17741): #9      WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:773:20)
I/flutter (17741): #10     RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:283:5)
I/flutter (17741): #11     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1102:15)
I/flutter (17741): #12     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1041:9)
I/flutter (17741): #13     SchedulerBinding.scheduleWarmUpFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:850:7)
I/flutter (17741): #15     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:384:19)
I/flutter (17741): #16     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:418:5)
I/flutter (17741): #17     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)
I/flutter (17741): (elided one frame from package dart:async-patch)
I/flutter (17741):
I/flutter (17741): 

════════════════════════════════════════════════════════════════════════════════════════════════════

It would be great if you could provide me some insights into this.如果您能给我提供一些对此的见解,那就太好了。

Issue was solved on suggestion of @VirenVVarasadiya by running:通过运行以下@VirenVVarasadiya 的建议解决了问题:

flutter clean
flutter run

暂无
暂无

声明:本站的技术帖子网页,遵循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 使用不包含 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.of(),该错误使用不包含MediaQuery的上下文调用 - Testing in flutter gives 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 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