【问题标题】:Dart - Only static members can accessed in initializersDart - 在初始化程序中只能访问静态成员
【发布时间】:2018-11-13 21:38:57
【问题描述】:

我正在尝试使用以下代码

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,),
      ),
    ),
  );

这里已经有一个问题了, Error: Only static members can be accessed in initializers what does this mean? 关于这个问题,但是,我真的应该为此使用“SingleTickerProviderStateMixin”吗?另外,这与单身人士有关吗? (我还在学习编程)

尝试以下答案后出错:

【问题讨论】:

标签: dart flutter


【解决方案1】:
final itemImage = ...

初始化一个类级别的字段。此代码在构造函数完成和对象完全初始化之前执行,因此禁止访问this.(隐式或显式),因为不能保证您尝试访问的内容已经初始化。

无论如何,以这种方式创建和缓存小部件通常是个坏主意。 而是让它成为一种方法:

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,
        ),
      ),
    ),
);

这样代码在调用buildItemImage(context) 时首先执行,而不是在创建对象实例时。此时保证保存到访问this.

【讨论】:

  • 我仍然收到原始帖子中所附的错误。
  • 对不起,我忘了删除final(替换为Widget
  • 谢谢,很好的解释
【解决方案2】:

试着剪掉

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

); 并将其粘贴到children&lt;Widget&gt;[//here] 并添加new

body:
  Center  (
      child: ListView(
        padding: EdgeInsets.only(left: 24.0, right: 24.0),
        shrinkWrap: true,
        children: <Widget>[
        ------------/*there it is*/---------------------------
          new 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,),
          ),
        ),
     -----------------------------------------------------
          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),
        ],
      )
  )

希望它的工作:)

【讨论】:

  • 代码块外有一个尾随);。如果您解释一下这是如何解决问题的,也会很有帮助
猜你喜欢
  • 2020-01-05
  • 2019-01-23
  • 1970-01-01
  • 2020-08-31
  • 2020-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多