Dart - 只能在初始化程序中访问静态成员

时间:2018-06-04 14:05:56

标签: dart flutter

我正在尝试使用以下代码

if (RuntimeEnabledFeatures::sharedFeatures().mediaPreloadingEnabled() && (equalLettersIgnoringASCIICase(as, "video") || equalLettersIgnoringASCIICase(as, "audio")))
    return CachedResource::MediaResource;
if (equalLettersIgnoringASCIICase(as, "font"))
    return CachedResource::FontResource;
#if ENABLE(VIDEO_TRACK)
if (equalLettersIgnoringASCIICase(as, "track"))
    return CachedResource::TextTrackResource;
#endif
return std::nullopt;

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

尝试以下答案后出错: enter image description here

2 个答案:

答案 0 :(得分:6)

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.

答案 1 :(得分:0)

尝试切割

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<Widget>[//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),
        ],
      )
  )

希望它能起作用:)