代码之家  ›  专栏  ›  技术社区  ›  Kartik Nhm

颤振中是否有一个“看不见”的常数式关键词?

  •  0
  • Kartik Nhm  · 技术社区  · 10 月前

    我是新来的,当我遇到'OnTap'函数时,我正潜伏在listview中,但当我实现时,我收到了错误“无效常数值”,我每次都必须点击QuickFix来解决这个问题?我已经附上了Widget的图片和代码

    image of code snippet

    class navDrawer extends StatelessWidget {
      const navDrawer({
        super.key,
      });
    
      @override
      Widget build(BuildContext context) {
        return Drawer(
          child: Column(children: <Widget>[
            Expanded(
              child: ListView(padding: EdgeInsets.zero, children: <Widget>[
                const DrawerHeader(
                  decoration: BoxDecoration(
                    color: Colors.amber,
                  ),
                  child: const Text(
                    'Labels',
                    style: TextStyle(fontSize: 50, color: Colors.black),
                  ),
                ),
                ListTile(
                  selectedColor: Colors.deepOrange,
                  selected: true,
                  focusColor: Colors.deepOrange,
                  leading: const Icon(Icons.label_important_outline),
                  title: const Text(
                    'Temporary',
                    style: TextStyle(fontSize: 30, color: Colors.blueGrey),
                  ),
                  onTap: () {
                    //made it work by quickfix
                    print('Temporary');
                  },
                ),
                const ListTile(
                  selectedColor: Colors.lightBlue,
                  leading: Icon(Icons.label_important),
                  title: const Text(
                    'Important',
                    style: TextStyle(fontSize: 30, color: Colors.blueGrey),
                  ),
                  focusColor: Colors.cyan,
                  onTap:(){
                    //the one in the image
                  },
                ),
                const ListTile(
                  leading: Icon(Icons.label_important),
                  title: const Text(
                    'Health',
                    style: TextStyle(fontSize: 30, color: Colors.blueGrey),
                  ),
                  focusColor: Colors.cyan,
                ),
                const ListTile(
                  leading: Icon(Icons.label_important),
                  title: const Text(
                    'Personal',
                    style: TextStyle(fontSize: 30, color: Colors.blueGrey),
                  ),
                  focusColor: Colors.cyan,
                )
              ]),
            ),
          ]),
        );
      }
    }
    

    我试图理解这个问题,并希望有一个解决方案。

    2 回复  |  直到 10 月前
        1
  •  1
  •   Kentukyyo    10 月前

    这个 const 要删除的是之前的那个 ListTile 基本上, ListTile 有一个常量构造函数,因为 onTap 默认情况下为null(null是一个常数),但是当您在 onTap 参数,它将不再是常量(因为函数不是常量)。现在你的ListTile不再是常量,因此你必须删除 const 声明之前 ListTile .

    别担心,在使用Flutter时,这种情况会经常发生。

        2
  •  1
  •   Frank van Puffelen    10 月前

    这个提示指的是 const ListTile(... 在您共享的代码中。从外观上看,一旦你添加了 onTap 对于列表互动程序小部件,它不能再 const .