简体   繁体   English

Flutter 错误:(Webview) Media Query.of() 使用不包含媒体查询的上下文调用

[英]Flutter Error:(Webview) Media Query.of() called with a context that does not contain a media query

I am new to flutter so maybe this question is repetitively asked.我是新手,所以可能会反复问这个问题。 Basically i am using webview for flutter.基本上我正在使用 webview 进行颤振。 Version webview: webview_flutter: ^0.3.16 My code is:版本 webview: webview_flutter: ^0.3.16我的代码是:

import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
void main() => runApp(WebViewTest());

class WebViewTest extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _WebViewTestState();
  }
}

class _WebViewTestState extends State<WebViewTest> {

  WebViewController _webViewController;
  // String filePath = 'files/test.html';

  String filePath = 'https://flutter.dev/';

  @override
  Widget build(BuildContext context) {

return MaterialApp(
  home: Scaffold(
  appBar: AppBar(title: Text('Webview Demo')),
  body: WebView(
    initialUrl: filePath,
    javascriptMode: JavascriptMode.unrestricted,
    onWebViewCreated: (WebViewController webViewController) {
      _webViewController = webViewController;
      //_loadHtmlFromAssets();
    },
  ),
 
)
);
  }
}

Note: Sometime it runs successfully but most of time gives me error like mentioned.注意:有时它会成功运行,但大部分时间都会给我提到的错误。 And why its high in size 100MB just for this code?为什么它的大小高达 100MB 仅用于此代码?

Error is:错误是:

I/flutter ( 753): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ I/flutter ( 753): The following assertion was thrown building WebViewTest(state: _WebViewTestState#72524): I/flutter ( 753): MediaQuery.of() called with a context that does not contain a MediaQuery. I/flutter (753):==╡ 小部件库的例外情况 ╞============================== ==========================I/flutter(753):以下断言被抛出构建WebViewTest(状态:_WebViewTestState#72524):I/flutter (753):使用不包含 MediaQuery 的上下文调用 MediaQuery.of()。 I/flutter ( 753): No MediaQuery ancestor could be found starting from the context that was passed to MediaQuery.of(). I/flutter (753):从传递给 MediaQuery.of() 的上下文开始,找不到 MediaQuery 祖先。 I/flutter ( 753): This can happen because you do not have a WidgetsApp or MaterialApp widget (those widgets introduce I/flutter ( 753): a MediaQuery), or it can happen if the context you use comes from a widget above those widgets. I/flutter ( 753) :这可能是因为您没有 WidgetsApp 或 MaterialApp 小部件(这些小部件引入了 I/flutter ( 753): MediaQuery),或者如果您使用的上下文来自上面的小部件,则可能会发生这种情况小部件。 I/flutter ( 753): The context used was: I/flutter ( 753): Scaffold(dirty, state: ScaffoldState#58c7d(lifecycle state: initialized, tickers: tracking 2 I/flutter ( 753): tickers)) I/flutter ( 753): 使用的上下文是: I/flutter ( 753): Scaffold(dirty, state: ScaffoldState#58c7d(lifecycle state: initialized, tickers: tracking 2 I/flutter (753): tickers))
I/flutter ( 753): When the exception was thrown, this was the stack: I/flutter ( 753): #0 MediaQuery.of (package:flutter/src/widgets/media_query.dart:798:5) I/flutter ( 753): #1 ScaffoldState.didChangeDependencies (package:flutter/src/material/scaffold.dart:1972:50) I/flutter ( 753): #2 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4106:12) I/flutter ( 753): #3 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3936:5) I/flutter ( 753) :当抛出异常时,这是堆栈: I/flutter ( 753): #0 MediaQuery.of (package:flutter/src/widgets/media_query.dart:798:5) I/flutter (753): #1 ScaffoldState.didChangeDependencies (package:flutter/src/material/scaffold.dart:1972:50) I/flutter (753): #2 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart :4106:12) I/flutter (753): #3 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3936:5)

MediaQuery is an inherited widget, in the example, it is taking the context of WebViewTest, not the MaterialApp. MediaQuery 是一个继承的小部件,在示例中,它采用 WebViewTest 的上下文,而不是 MaterialApp。 MaterialApp needs to be the parent of your class to provide it with a context. MaterialApp 需要是您的类的父类才能为其提供上下文。 This will work:这将起作用:

import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
void main() => runApp(MyApp());


class MyApp extends StatelessWidget {
  const MyApp({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: WebViewTest(),
    );
  }
}

class WebViewTest extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _WebViewTestState();
  }
}

class _WebViewTestState extends State<WebViewTest> {

  WebViewController _webViewController;
  // String filePath = 'files/test.html';

  String filePath = 'https://flutter.dev/';

  @override
  Widget build(BuildContext context) {

return  Scaffold(
  appBar: AppBar(title: Text('Webview Demo')),
  body: WebView(
    initialUrl: filePath,
    javascriptMode: JavascriptMode.unrestricted,
    onWebViewCreated: (WebViewController webViewController) {
      _webViewController = webViewController;
      //_loadHtmlFromAssets();
    },


)
);
  }
}

As for the size of the app, in debug mode all the necessary components for Just-In-Time (JIT) are loaded, so the debug mode has a large size, but as soon as you create the release mode, you will see that in code example will have an application with about 2.87mb至于app的大小,在debug模式下,Just-In-Time(JIT)的所有必要组件都被加载了,所以debug模式有很大的大小,但是一旦你创建了release模式,你就会看到在代码示例中将有一个大约 2.87mb 的应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM