【问题标题】:The values in a const list literal must be constantsconst 列表文字中的值必须是常量
【发布时间】:2021-06-29 07:00:28
【问题描述】:

我正在尝试创建一个 FocusNode 来列出文本,但我没有这样做。

userNameNode 是:

 final FocusNode userNameNode =FocusNode();

导致错误的userNameNode的用法:

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

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'.

_ListOfInputs 类是:

class _ListOfInputs extends StatelessWidget {
      final FocusNode userNameNode;
      const ListOfInputs(
        this.userNameNode,
      );
}

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    你的代码是:

    Stack(
          fit: StackFit.passthrough,
          textDirection: TextDirection.ltr,
          children: const [
                    _ListOfInputs(userNameNode:  userNameNode,)]);
    

    所以改变它:

    Stack(
          fit: StackFit.passthrough,
          textDirection: TextDirection.ltr,
          children: [
                   _ListOfInputs(userNameNode:  userNameNode,)]);
    

    因为列表不是 const。

    【讨论】:

      【解决方案2】:

      删除 Stack.children 之前的 const 关键字,并为 Widget 提供 _Image_ListOfInputs 方法的数据类型,而不是动态。

      Stack(
        fit: StackFit.passthrough,
        textDirection: TextDirection.ltr,
        children: [
          _Image(),
          _ListOfInputs(userNameNode:  userNameNode),
      ]);
      

      【讨论】:

        猜你喜欢
        • 2021-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多