简体   繁体   English

处理手势时引发了以下断言: Scaffold.of() 使用不包含 Scaffold 的上下文调用

[英]The following assertion was thrown while handling a gesture: Scaffold.of() called with a context that does not contain a Scaffold

import 'package:flutter/material.dart';

import 'dashboard.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: SafeArea(
        child: Scaffold(
          appBar: AppBar(
            leading: IconButton(
              icon: Icon(Icons.accessible),
              onPressed: () => Scaffold.of(context).openEndDrawer(),
            ),
            title: Text('Sorted.'),
            backgroundColor: Color(0xff0A3D62),
          ),
          drawer: Drawer(
            child: ListView(
              padding: EdgeInsets.zero,
              children: <Widget>[
                new UserAccountsDrawerHeader(
                  accountName: new Text('XYZ'),
                  accountEmail: new Text('XYZ@gmail.com'),
                  currentAccountPicture: new CircleAvatar(),
                )
              ],
            ),
          ),
          body: Center(child: Home()),
        ),
      ),
    );
  }
}

Error:错误:

The following assertion was thrown while handling a gesture:
Scaffold.of() called with a context that does not contain a Scaffold.

No Scaffold ancestor could be found starting from the context that was passed to Scaffold.of(). This usually happens when the context provided is from the same StatefulWidget as that whose build function actually creates the Scaffold widget being sought.

The error fires when I try to open the drawer from the icon.当我尝试从图标打开抽屉时会触发错误。 Please help me resolve this.请帮我解决这个问题。 Thanks in advance!提前致谢!

@rkdupr0n is correct in saying that your context variable doesn't have a Scaffold ancestor. @rkdupr0n 说您的context变量没有Scaffold祖先是正确的。 context is currently referring to what's being passed by the outer build function, which doesn't have Scaffold as an ancestor, it only contains it. context当前指的是外部build function 传递的内容,它没有Scaffold作为祖先,它只包含它。 So you need to get a BuildContext that has Scaffold the scaffold you already have in your tree as it's ancestor.因此,您需要获得一个BuildContext ,它具有Scaffold您在树中已经拥有的脚手架,因为它是祖先。 To do this you can use a Builder widget to wrap the area that needs this context:为此,您可以使用Builder小部件来包装需要此上下文的区域:

AppBar(
  leading: Builder(
    builder: (context) {//NEW CONTEXT OBTAINED HERE
      return IconButton(
        icon: Icon(Icons.accessible),
        onPressed: () => Scaffold.of(context).openEndDrawer(),
      );
    }
  ),
  ...
),  

暂无
暂无

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

相关问题 使用不包含脚手架的上下文调用的 Scaffold.of() - Scaffold.of() called with a context that does not contain a Scaffold 未处理的异常:使用不包含 Scaffold 的上下文调用 Scaffold.of() - Unhandled Exception: Scaffold.of() called with a context that does not contain a Scaffold 处理手势时抛出以下断言:未找到脚手架小部件 - The following assertion was thrown while handling a gesture: No Scaffold widget found Flutter :此错误出现在使用不包含 Scaffold 的上下文调用的 Scaffold.of() 。 当我尝试展示小吃店时 - Flutter : this error appear Scaffold.of() called with a context that does not contain a Scaffold. when i try to show a snackbar 处理手势时抛出以下断言:使用不包含导航器的上下文请求导航器操作 - The following assertion was thrown while handling a gesture: Navigator operation requested with a context that does not include a Navigator 上下文的颤振脚手架“不包含脚手架” - Flutter scaffold of context giving "does not contain a Scaffold" Snackbar 和异常:scaffold.of() - Snackbar and exception: scaffold.of() 错误:没有名为“nullOk”的命名参数 Scaffold.of(context, nullOk: true) - Error: No named parameter with the name 'nullOk' Scaffold.of(context, nullOk: true) Flutter Scaffold.of(context).openDrawer() 不起作用 - Flutter Scaffold.of(context).openDrawer() doesn't work 手势捕获的异常:处理手势时引发以下 NoSuchMethodError: - Exception caught by gesture : The following NoSuchMethodError was thrown while handling a gesture:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM