const 列表文字中的值必须是常量

4 dart flutter

我正在尝试创建一个FocusNode文本列表,但我未能这样做。

用户名节点是:

 final FocusNode userNameNode =FocusNode();
Run Code Online (Sandbox Code Playgroud)

导致错误的 userNameNode 的使用:

Stack(
     fit: StackFit.passthrough,
      textDirection: TextDirection.ltr,
      children: const [
        _Image(),
        _ListOfInputs(
          userNameNode:  userNameNode,
)]);
Run Code Online (Sandbox Code Playgroud)

userNameNode的错误是:

The values in a const list literal must be constants.
Try removing the keyword 'const' from the list literal.dart(non_constant_list_element)
The element type 'dynamic' can't be assigned to the list type 'Widget'.
Run Code Online (Sandbox Code Playgroud)

_ListOfInputs 类是:

class _ListOfInputs extends StatelessWidget {
      final FocusNode userNameNode;
      const ListOfInputs(
        this.userNameNode,
      );
}
Run Code Online (Sandbox Code Playgroud)

小智 12

你的代码是:

Stack(
      fit: StackFit.passthrough,
      textDirection: TextDirection.ltr,
      children: const [
                _ListOfInputs(userNameNode:  userNameNode,)]);
Run Code Online (Sandbox Code Playgroud)

所以改变它:

Stack(
      fit: StackFit.passthrough,
      textDirection: TextDirection.ltr,
      children: [
               _ListOfInputs(userNameNode:  userNameNode,)]);
Run Code Online (Sandbox Code Playgroud)

因为列表不是 const。