繁体   English   中英

参数类型“上下文”不能分配给参数类型“BuildContext”。dartargument_type_not_assignable)

[英]The argument type 'Context' can't be assigned to the parameter type 'BuildContext'.dartargument_type_not_assignable)

情况:

I have a function in the simple class (not stateful or stateless) but that function is called in the first Screen of the stateful widget, inside the function I have used Navigator.push to go to the second screen of the stateful widget.

代码:

功能代码

containerAdder(initialI) {
    List<Widget> listViewContainer = List();

    for (var j = 0; j < mainListAllPlantDetailsList1.length; j++) {
      if (tabsText[initialI] == mainListAllPlantDetailsList1[j].ca) {
        listViewContainer.add(
          FlatButton(
            onPressed: () {
              Navigator.push(
                context, **(MENTIONED ERROR GIVES OVERHERE )**
                MaterialPageRoute(builder: (context) => ParticularPlant2()),
              );
            },
            child: Container(
                height: 320,
                width: 200,
                child: Stack(
                  children: [
                    Positioned(
                      bottom: 10,
                      child: Container(
                        padding: EdgeInsets.fromLTRB(25, 50, 20, 20),
                        height: 150,
                        width: 180,
                        decoration: BoxDecoration(
                          color: Colors.lightGreen,
                          borderRadius: BorderRadiusDirectional.only(
                            bottomEnd: Radius.circular(70),
                            topEnd: Radius.circular(70),
                            topStart: Radius.circular(70),
                          ),
                        ),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(mainListAllPlantDetailsList1[j].ca),
                            Text(mainListAllPlantDetailsList1[j].pN),
                            Text(mainListAllPlantDetailsList1[j].pr.toString()),
                          ],
                        ),
                      ),
                    )
                  ],
                )),
          ),
        );
      } else {
        continue;
      }
    }
    return listViewContainer;
  }

在另外两个屏幕上还有另外两个有状态小部件,而在第一个屏幕中,我称之为 function。

问题:

帮助我了解为什么会发生上述错误以及如何解决?

让我们将上下文传递给您的 function 以使其工作:

containerAdder(initialI, BuildContext context) { 
    // Your all of the codes
}

现在,像这样调用 containerAdder:

objectName.containerAdder(intialI, context);

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM