Dart-在初始化器中只能访问静态成员 [英] Dart - Only static members can accessed in initializers

查看:182
本文介绍了Dart-在初始化器中只能访问静态成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以下代码

  class NewItemCreate扩展了StatefulWidget {

@override
NewItemCreateState createState()=> new NewItemCreateState();
}

类NewItemCreateState扩展了State< NewItemCreate>
{

File _image;
Future getImage()async {
var image = await ImagePicker.pickImage(source:ImageSource.camera);
setState((){
_image = image;
});
print(_image.path);
}
@override
Widget build(BuildContext context){

return Scaffold(
backgroundColor:Colors.yellow,

appBar:新的AppBar(
标题:new Text( Report),
海拔:5.0,
),
正文:
中心(
子级:ListView(
padding:EdgeInsets.only(左:24.0,右:24.0),
收缩包装:真,
子级:< Widget> [
itemImage,
SizedBox(高度:18.0),
itemName,
SizedBox(高度:18.0),
itemLocation,
SizedBox(高度:18.0),
itemLocation,
SizedBox(高度:18.0),
itemTime,
SizedBox(高度:18.0),
报告,
SizedBox(高度:38.0),
],


);

}

最终商品图片=填充(
填充:EdgeInsets.symmetric(vertical:25.0),
子项:Material(
borderRadius :BorderRadius.circular(30.0),
shadowColor:Colors.lightBlueAccent.shade100,
高程:5.0,
子级:MaterialButton(
minWidth:200.0,
高度: 300.0,
onPressed:(){
getImage();
},
颜色:Colors.lightGreenAccent,
子对象:
new Icon(Icons。 add_a_photo,大小:150.0,颜色:Colors.blue,),
),
),
);

这里已经有一个问题,

解决方案

  final itemImage = ... 

初始化一个类级字段。此代码在构造函数完成且对象完全初始化之前执行,因此禁止访问 this。(隐式或显式)是禁止的,因为无法保证尝试的操作访问已初始化。



以这种方式创建和缓存小部件通常不是一个好主意。
使其成为一种方法:

  Widget buildItemImage(BuildContext context)=>填充(
padding:EdgeInsets.symmetric(vertical:25.0),
child:Material(
borderRadius:BorderRadius.circular(30.0),
shadowColor:Colors.lightBlueAccent.shade100,
高程:5.0,
子级:MaterialButton(
minWidth:200.0,
高度:300.0,
onPressed:(){
getImage();
},
颜色:Colors.lightGreenAccent,
孩子:new Icon(Icons.add_a_photo,尺寸:150.0,颜色:Colors.blue,
),
),
),
);

这样,当 buildItemImage(context)被调用,而不是在创建对象实例时调用。目前,确保可以保存该文件以访问 this。


I am trying with the following code

class NewItemCreate extends StatefulWidget{

  @override
  NewItemCreateState createState() => new NewItemCreateState();
}

class NewItemCreateState extends State<NewItemCreate>
{

  File _image;
  Future getImage() async {
    var image = await ImagePicker.pickImage(source: ImageSource.camera);
    setState(() {
      _image = image;
    });
    print(_image.path);
  }
  @override
  Widget build(BuildContext context) {

    return Scaffold(
      backgroundColor: Colors.yellow,

        appBar: new AppBar(
          title: new Text("Report"),
          elevation: 5.0,
        ),
      body:
      Center  (
          child: ListView(
            padding: EdgeInsets.only(left: 24.0, right: 24.0),
            shrinkWrap: true,
            children: <Widget>[
              itemImage,
              SizedBox(height: 18.0),
              itemName,
              SizedBox(height: 18.0),
              itemLocation,
              SizedBox(height: 18.0),
              itemLocation,
              SizedBox(height: 18.0),
              itemTime,
              SizedBox(height: 18.0),
              Report,
              SizedBox(height: 38.0),
            ],
          )
      )
    );

  }

  final itemImage =       Padding(
    padding: EdgeInsets.symmetric(vertical: 25.0),
    child: Material(
      borderRadius: BorderRadius.circular(30.0),
      shadowColor: Colors.lightBlueAccent.shade100,
      elevation: 5.0,
      child: MaterialButton(
        minWidth: 200.0,
        height: 300.0,
        onPressed: (){
          getImage();
        },
        color: Colors.lightGreenAccent,
        child:
        new Icon(Icons.add_a_photo, size: 150.0,color: Colors.blue,),
      ),
    ),
  );

There is already a question here, Error: Only static members can be accessed in initializers what does this mean? regarding this very issue, but, should I really be using "SingleTickerProviderStateMixin" for this? Also, is this something to do with singletons? (I am still learning programming)

Error after trying out the below answer:

解决方案

final itemImage = ...

initializes a class-level field. This code is executed before the constructor is completed and the object fully initialized, therefore accessing this. (implicit or explicit) is forbidden because it can't be guaranteed that what you attempt to access is already initialized.

Creating and caching widgets this way is a bad idea in general anyway. Instead make it a method:

Widget buildItemImage(BuildContext context) => Padding(
    padding: EdgeInsets.symmetric(vertical: 25.0),
    child: Material(
        borderRadius: BorderRadius.circular(30.0),
        shadowColor: Colors.lightBlueAccent.shade100,
        elevation: 5.0,
        child: MaterialButton(
            minWidth: 200.0,
            height: 300.0,
            onPressed: () {
                getImage();
            },
            color: Colors.lightGreenAccent,
            child: new Icon(Icons.add_a_photo, size: 150.0,color: Colors.blue,
        ),
      ),
    ),
);

This way the code is first executed when buildItemImage(context) is called, not when the object instance is created. At this time it is guaranteed to be save to access this..

这篇关于Dart-在初始化器中只能访问静态成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆