简体   繁体   English

无法将参数类型“jsObject”分配给“buildContext”参数?

[英]The argument type 'jsObject' can't be assigned to 'buildContext' parameter?

that error happen when i tried putting a method that need to take buildcontext as an argument in floating action button widget.当我尝试在浮动操作按钮小部件中放置一个需要将 buildcontext 作为参数的方法时,就会发生该错误。

I tried moving the same method to different widgets that have builders and it worked fine so why don't the FA button have the parent context ?我尝试将相同的方法移动到具有构建器的不同小部件,并且运行良好,那么为什么 FA 按钮没有父上下文? here is where it happens:这是它发生的地方:

runApp(MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
          appBar: AppBar(
            title: Text('policies list'),
          ),
          body: getList(),
          floatingActionButton: FloatingActionButton(
            child: Icon(Icons.add),
            onPressed: () => showSB(context),
          ))));`

the code you have above does not have context because you didn't create a Stateful or Stateless class for home您上面的代码没有上下文,因为您没有为home创建有状态无状态

I think You should do this instead我认为你应该这样做

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){

  }
}

Hope this works for you.希望这对你有用。

暂无
暂无

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

相关问题 无法将参数类型“ JsObject”分配给参数类型“ BuildContext” - The argument type 'JsObject' can't be assigned to the parameter type 'BuildContext 参数类型“JsObject”不能分配给参数类型“BuildContext”。 - flutter - The argument type 'JsObject' can't be assigned to the parameter type 'BuildContext'. - flutter 错误:无法将参数类型“JsObject”分配给参数类型“BuildContext” - Error: The argument type 'JsObject' can't be assigned to the parameter type 'BuildContext' 参数类型“JsObject”不能分配给参数类型“BuildContext”。dart - The argument type 'JsObject' can't be assigned to the parameter type 'BuildContext'.dart 参数类型“JsObject”不能分配给参数类型“BuildContext” - The argument type 'JsObject' can't be assigned to the parameter type 'BuildContext' 导航器错误在颤振导航器错误参数类型'JsObject'不能分配给参数类型'BuildContext' - navigator error in flutter navigator error The argument type 'JsObject' can't be assigned to the parameter type 'BuildContext' 使用简单代码时,无法将参数类型“JsObject”分配给参数类型“BuildContext” - The argument type 'JsObject' can't be assigned to the parameter type 'BuildContext' while using simple code 出现错误“无法将参数类型‘JsObject’分配给参数类型‘BuildContext’” - Getting error "The argument type 'JsObject' can't be assigned to the parameter type 'BuildContext'" 类型“JsObject”不能分配给类型“BuildContext” - Type 'JsObject' can't be assigned to type 'BuildContext' 参数类型“BuildContext?” 不能分配给参数类型“BuildContext” - The argument type 'BuildContext?' can't be assigned to the parameter type 'BuildContext'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM