簡體   English   中英

無法將參數類型“jsObject”分配給“buildContext”參數?

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

當我嘗試在浮動操作按鈕小部件中放置一個需要將 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){

  }
}

希望這對你有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM