jacace
jacace

Reputation: 259

ListTile onTap: () -> trigger error: Error: Not a constant expression

I have the code below:

drawer: Drawer(
  child: ListView(
    ...
    children: const <Widget>[

    SizedBox(
      ...
    ),

      ListTile(
        leading: Icon(Icons.message),
        title: Text('Reportes de daños recibidos'),
        ***onTap: () {
          Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => ReportsToDo()));
        },***
      ),

And the line with ListTile onTap: () causes the following two issues. Am I missing something? T ook this example from the documentation.

lib/PropertyAgentMain.dart:38:27: Error: Method invocation is not a constant expression.
                Navigator.push(
                          ^^^^
lib/PropertyAgentMain.dart:37:22: Error: Not a constant expression.
              onTap: () {
                 ^^

Thanks,

Javier Caceres

Upvotes: 0

Views: 4578

Answers (1)

Houssem Selmi
Houssem Selmi

Reputation: 150

I have just reproduced your code in a minimal app by removing the CONST keyword for the listview children property, and it works perfectly. check it here

import 'package:flutter/material.dart';
import 'package:ui_learning/appScreen.dart';
void main() {
runApp(MaterialApp(
title: 'Flutter Tutorial',
home: TutorialHome(),
));
}

class TutorialHome extends StatelessWidget {
@override
Widget build(BuildContext context) {
// Scaffold is a layout for the major Material Components.
return Scaffold(
  drawer: Drawer(
      child: ListView(children: <Widget>[
    SizedBox(
      height: 10,
    ),
    ListTile(
      leading: Icon(Icons.message),
      title: Text('Reportes de daños recibidos'),
      onTap: () {
        Navigator.push(
            context, MaterialPageRoute(builder: (context) => AppScreen()));
      },
    )
  ])),
  appBar: AppBar(
    leading: IconButton(
      icon: Icon(Icons.menu),
      tooltip: 'Navigation menu',
      onPressed: null,
    ),
    title: Text('Example title'),
    actions: <Widget>[
      IconButton(
        icon: Icon(Icons.search),
        tooltip: 'Search',
        onPressed: null,
      ),
    ],
  ),
  // body is the majority of the screen.
  body: Center(
    child: Text('Hello, world!'),
  ),
  floatingActionButton: FloatingActionButton(
    tooltip: 'Add', // used by assistive technologies
    child: Icon(Icons.add),
      onPressed: null,
     ),
  );
 }
 }

Upvotes: 8

Related Questions