Flutter 오류해결: WebviewScaffold 사용시

김찬혁·2021년 7월 7일
0
class Webs extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return WebviewScaffold(
      url: MYURL,
      withJavascript: true,
      javascriptChannels: Set.from([
        JavascriptChannel(
          name: "james",
          onMessageReceived: (JavascriptMessage result){
            print("message ${result.message}");
          }
        ),
      ]),
    );
  }
}

(수정 전 코드)

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: WebviewScaffold(
        url: 'http://localhost:3000/web',
        withJavascript: true,
        javascriptChannels: Set.from([
          JavascriptChannel(
              name: "jams",
              onMessageReceived: (JavascriptMessage result){
                print("message ${result.message}");
              }
          ),
        ]),
      ),
    );
  }
}

(수정 후 코드)

바로 webviewscaffold로 리턴하는것이 아니라
materialapp으로 감싸고 home: ,으로 해야 잘 나왔다.

(오류사진)

Launching lib/main.dart on iPhone 12 Pro Max in debug mode...
Running Xcode build...
Xcode build done.                                           14.7s
Debug service listening on ws://127.0.0.1:57681/J1BctD86rDI=/ws
Syncing files to device iPhone 12 Pro Max...

======== Exception caught by widgets library =======================================================
The following assertion was thrown building WebviewScaffold(state: _WebviewScaffoldState#5aa3d):
No MediaQuery widget ancestor found.

Scaffold widgets require a MediaQuery widget ancestor.
The specific widget that could not find a MediaQuery ancestor was: Scaffold
  dirty
  state: ScaffoldState#4e545(lifecycle state: initialized, tickers: tracking 2 tickers)
The ownership chain for the affected widget is: "Scaffold ← WebviewScaffold ← MyApp2 ← [root]"

No MediaQuery ancestor could be found starting from the context that was passed to MediaQuery.of(). This can happen because you have not added a WidgetsApp, CupertinoApp, or MaterialApp widget (those widgets introduce a MediaQuery), or it can happen if the context you use comes from a widget above those widgets.

The relevant error-causing widget was: 
  WebviewScaffold file:///Users/kim/StudioProjects/webview/lib/main.dart:30:12
When the exception was thrown, this was the stack: 
#0      debugCheckHasMediaQuery.<anonymous closure> (package:flutter/src/widgets/debug.dart:219:7)
#1      debugCheckHasMediaQuery (package:flutter/src/widgets/debug.dart:234:4)
#2      MediaQuery.of (package:flutter/src/widgets/media_query.dart:820:12)
#3      ScaffoldState.didChangeDependencies (package:flutter/src/material/scaffold.dart:2831:50)
#4      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4732:11)
...
====================================================================================================

오류코드를 보면 메터리얼로 감싸라고 나온다.. 오류를 잘 읽어보자

0개의 댓글