简体   繁体   English

Dart(/ Flutter):在初始化列表中创建函数

[英]Dart(/Flutter): Create function in initializer list

I am implementing a class with multiple constructors, which is internally build around a IndexedWidgetBuilder (a function object) 我正在用多个构造函数实现一个类,该类在内部围绕IndexedWidgetBuilder(一个函数对象)构建

typedef IndexedWidgetBuilder = Widget Function(BuildContext context, int index);

Now, one of the constructors, call it MyWidget.list shall receive a list myList and create the IndexedWidgetBuilder myBuilder from it: 现在,一个MyWidget.list的构造MyWidget.list将收到一个列表myListmyList创建IndexedWidgetBuilder myBuilder

IndexedWidgetBuilder myBuilder
  = (BuildContext context, int index) => list[index % list.length];

While this code snippet alone works perfectly, I am not able to use this inside the initializer list of the constructor. 虽然仅此代码段即可完美工作,但我无法在构造函数的初始化列表中使用此代码段。 A minimal working example reads 一个最小的工作示例如下

class MyApp {
  // Default constructor goes here

  MyApp.list(List<int> myList) :
    myBuilder = (BuildContext context, int index) => list[index % list.length];

  final IndexedWidgetBuilder myBuilder;
}

In Android studio, this snippet produces the error: 在Android Studio中,此代码段会产生错误:

The initializer type 'Type' can't be assigned to the field type '(BuildContext, int) → Widget'. 初始化类型'Type'不能分配给字段类型'(BuildContext,int)→Widget'。

I did not find anything related on google and the language documentation also did not provide useful information. 我没有在Google上找到任何相关信息,语言文档也未提供有用的信息。 Removing the final keyword and moving everything into the code block of the constructor would be a solution, albeit one I would only consider a last resort. 删除final关键字并将所有内容移到构造函数的代码块中将是一个解决方案,尽管我只考虑万不得已。


Note: This is not directly a flutter problem, since it occurs with every function objects. 注意:这不是直接的抖动问题,因为它发生在每个功能对象上。

Enclosing the function in parentheses seems to make the warning go away; 将函数括在括号中似乎可以消除警告。 though changes it to saying the parentheses are redundant! 虽然将其改为说括号是多余的!

  MyApp.list(List<Widget> list)
      : myBuilder =
            ((_, int index) => list[index % list.length]);

Notice how the context is unused. 注意上下文是如何未使用的。 That means that your pre-built widgets do not have access to it, which means they can't use it for any of the .of() derived uses. 这意味着您的预构建窗口小部件无法访问它,这意味着它们不能将其用于任何.of()派生用途。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 dart 或 flutter 中正确创建 class - how create a class in dart or flutter correctly 在构造函数初始化列表中调用函数是否可以? - Is it ok to call a function in constructor initializer list? Flutter 2.0 / Dart - 如何创建带有可选参数的构造函数? - Flutter 2.0 / Dart - How to create a constructor with optional parameters? 如何在初始化列表内创建一个非空的boost ublas :: vector? - How to create a nonempty boost ublas::vector inside an initializer list? 如何从 Dart/Flutter 中的通用 function 调用命名构造函数 - How to call a named constructor from a generic function in Dart/Flutter 初始化列表:基类的构造函数和成员函数 - initializer list: a constructor from the base class and a member function 使用成员初始值设定项列表没有匹配的函数调用错误 - No matching function call error using member initializer list 在初始化列表中使用复杂函数来初始化const成员一个好的设计? - Is using a complex function in initializer list to initialize const member a good design? C ++函数重载和initializer_list构造函数 - C++ function overload and initializer_list constructor 在 Dart 中混合初始化器和构造器主体 - Mixing Up an Initializer and a Constructor Body in Dart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM