简体   繁体   中英

Error: Method invocation is not a constant expression

Error: Method invocation is not a constant expression. color: Colors.black.withOpacity(0.6), i get this error when i try run this:

body: ListView.builder(
        itemCount: 15,
        itemBuilder: (context, i) => const ListTile(
          title: Text("Bitcoin",
              style: TextStyle(
                fontWeight: FontWeight.w500,
                fontSize: 24,
              )),
          subtitle: Text("\$20000",
              style: TextStyle(
                color: Colors.grey.withOpacity(0.6), //error in this line
                fontWeight: FontWeight.w700,
                fontSize: 14,
              )),
        ),
      ),

Please helpto solve this

You need to remove the const before ListTile, because Colors.grey.withOpacity(0.6) isn't a constant value.

body: ListView.builder(
    itemCount: 15,
    itemBuilder: (context, i) => ListTile(
      title: const Text("Bitcoin",
          style: TextStyle(
            fontWeight: FontWeight.w500,
            fontSize: 24,
          )),
      subtitle: Text("\$20000",
          style: TextStyle(
            color: Colors.grey.withOpacity(0.6), //error in this line
            fontWeight: FontWeight.w700,
            fontSize: 14,
          )),
    ),
  ),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM