Search code examples
androidflutterdartstyles

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


Solution

  • 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,
              )),
        ),
      ),