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

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

问题描述

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

I have something like this. I am having difficulty understanding this error. Why does accessing filterController here give this error here ? but it doesnt 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(..),
        );
    }
}

如何解决此问题?

推荐答案

class AppHomeState extends State<AppHome> with SingleTickerProviderStateMixin {

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

... 是一个初始化程序,目前无法访问 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 是隐式的

要解决您的问题(假设电子邮件必须为 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 字段时最终的电子邮件可以在构造函数初始化列表中初始化:

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天全站免登陆