错误:在初始值设定项中只能访问静态成员这是什么意思? [英] Error: Only static members can be accessed in initializers what does this mean?

查看:16
本文介绍了错误:在初始值设定项中只能访问静态成员这是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的东西.我很难理解这个错误.为什么在此处访问 filterController 会在此处出现此错误,但如果我在构建中移动当前整个 TextFormField 创建(在注释 A 和 B 之间),则不会出现此错误方法?如何在 build 方法中移动整个 TextFormField 使 filterController 成为静态然后解决这个问题?

I have something like this. I am having difficulty understanding this error. Why does accessing filterController here give this error here, but it doesn't give this error if I move the current entire TextFormField creation (between comments A and B) inside the build method? How does moving the entire TextFormField inside the build method make filterController static then and resolve this issue?

class AppHomeState extends State<AppHome> with SingleTickerProviderStateMixin
{

    TabController _tabController;
    final filterController = new TextEditingController(text: "Search");
        //----A
        TextFormField email = new TextFormField(
        keyboardType: TextInputType.emailAddress,
        controller: filterController,    ------>ERROR : Error: Only static members can be accessed in initializers
        );
       //----B

  @override
    Widget build(BuildContext context)
    {
        return new Scaffold(
                appBar: new AppBar(..),
        );
    }
}

我该如何解决这个问题?

How can I resolve this issue?

推荐答案

class AppHomeState extends State<AppHome> with SingleTickerProviderStateMixin {

    TabController _tabController;
    final filterController = new TextEditingController(text: "Search");
    TextFormField email = ...

... 是一个初始化程序,此时无法访问 this.初始化程序在构造函数之前执行,但 this 只允许在调用超级构造函数(在您的示例中隐含)完成后访问.因此,只允许在构造函数体中(或以后)访问 this.

... is an initializer and there is no way to access this at this point. Initializers are executed before the constructor, but this is only allowed to be accessed after the call to the super constructor (implicit in your example) was completed. Therefore only in the constructor body (or later) access to this is allowed.

这就是您收到错误消息的原因:

This is why you get the error message:

controller: filterController,

访问this.filterController(this 是隐式的,如果你不显式地写它).

accesses this.filterController (this is implicit if you don't write it explicit).

要解决您的问题(假设 email 需要是 final),您可以使用工厂构造函数和构造函数初始值设定项列表:

To work around your issue (assuming email needs to be final) you can use a factory constructor and a constructor initializer list:

class AppHomeState extends State<AppHome> with SingleTickerProviderStateMixin {
  factory SingleTickerProviderStateMixin() => 
      new SingleTickerProviderStateMixin._(new TextEditingController(text: "Search"));

  SingleTickerProviderStateMixin._(TextEditingController textEditingController) : 
      this.filterController = textEditingController,   
      this.email = new TextFormField(
        keyboardType: TextInputType.emailAddress,
        controller: textEditingController);

  TabController _tabController;
  final filterController;
  final TextFormField email;

或者当email字段不需要是final时email可以在构造器初始化列表中初始化:

or when the email field does not need to be final email can be initialized in the constructor initializer list:

class AppHomeState extends State<AppHome> with SingleTickerProviderStateMixin {

  SingleTickerProviderStateMixin() {
    email = new TextFormField(
        keyboardType: TextInputType.emailAddress,
        controller: filterController,
    );
  }

  TabController _tabController;
  final filterController = new TextEditingController(text: "Search");
  TextFormField email;

但在 Flutter 小部件中 initState 通常用于此

but in Flutter widgets initState is usually used for that

class AppHomeState extends State<AppHome> with SingleTickerProviderStateMixin {

  @override
  void initState() {
    super.initState();
    email = new TextFormField(
        keyboardType: TextInputType.emailAddress,
        controller: filterController,
    );
  }

  TabController _tabController;
  final filterController = new TextEditingController(text: "Search");
  TextFormField email; 

这篇关于错误:在初始值设定项中只能访问静态成员这是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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