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