【问题标题】:The argument type 'jsObject' can't be assigned to 'buildContext' parameter?参数类型“jsObject”不能分配给“buildContext”参数?
【发布时间】:2020-03-17 21:37:58
【问题描述】:

当我尝试在浮动操作按钮小部件中放置需要将 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),
          ))));`

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您上面的代码没有 context,因为您没有为 homeStatefulStateless 类/strong>

    我认为你应该这样做

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

    希望这对你有用。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-12
    • 2020-06-25
    • 1970-01-01
    • 2019-11-17
    • 2021-08-13
    • 1970-01-01
    相关资源
    最近更新 更多