dart 在android studio中,我更喜欢将const文字作为@immutable类的构造函数的参数

pb3skfrl  于 2023-02-27  发布在  Android
关注(0)|答案(1)|浏览(227)

在下面的代码中,我收到了一个检查警告,“prefer const literal as parameters of constructors on @immutable classes”,这很烦人。我该怎么做才能让它消失呢?
screenshot from ide

Container(
        margin: const EdgeInsets.all(20),
        width: double.infinity,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            TextField(
              decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  hintText: 'Enter a search term'),
            ),
            Text("these are the search results", textAlign: TextAlign.left),
          ],
        ),
      )

我试过让每个构造函数都调用'const',但没有用。

Container(
        margin: const EdgeInsets.all(20),
        width: double.infinity,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const TextField(
              decoration: const InputDecoration(
                  border: const OutlineInputBorder(),
                  hintText: 'Enter a search term'),
            ),
            const Text("these are the search results", textAlign: TextAlign.left),
          ],
        ),
      ),
bmp9r5qi

bmp9r5qi1#

只需将列表标记为const:

Container(
    margin: const EdgeInsets.all(20),
    width: double.infinity,
    child: Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: const [
        TextField(
          decoration: InputDecoration(
              border: OutlineInputBorder(),
              hintText: 'Enter a search term'),
        ),
        Text("these are the search results", textAlign: TextAlign.left),
      ],
    ),
  ),

相关问题