当我尝试在浮动操作按钮小部件中尝试放置需要将buildcontext作为参数的方法时,就会发生该错误。
我尝试将相同的方法移到具有构建器的不同小部件上,并且效果很好,所以为什么FA按钮没有父上下文?这是发生的地方:
runApp(MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: Text('policies list'),
),
body: getList(),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () => showSB(context),
))));`
您上面的代码没有上下文,因为您没有为home创建有状态或无状态类
我认为你应该这样做
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Home(),
);
}
}
class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: (){
someMethodThatTakesContextAsParameter(context);
},
),
);
}
void someMethodThatTakesContextAsParameter(BuildContext context){
}
}
希望这对您有用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句