ListTile onTap: () -> 触发错误:错误:不是常量表达式

jac*_*ace 0 flutter

我有下面的代码:

\n\n
drawer: Drawer(\n  child: ListView(\n    ...\n    children: const <Widget>[\n\n    SizedBox(\n      ...\n    ),\n\n      ListTile(\n        leading: Icon(Icons.message),\n        title: Text('Reportes de da\xc3\xb1os recibidos'),\n        ***onTap: () {\n          Navigator.push(\n              context,\n              MaterialPageRoute(builder: (context) => ReportsToDo()));\n        },***\n      ),\n
Run Code Online (Sandbox Code Playgroud)\n\n

而 ListTile onTap:() 行会导致以下两个问题。我错过了什么吗?从文档中获取此示例。

\n\n
lib/PropertyAgentMain.dart:38:27: Error: Method invocation is not a constant expression.\n                Navigator.push(\n                          ^^^^\nlib/PropertyAgentMain.dart:37:22: Error: Not a constant expression.\n              onTap: () {\n                 ^^\n
Run Code Online (Sandbox Code Playgroud)\n\n

谢谢,

\n\n

哈维尔·卡塞雷斯

\n

小智 8

我刚刚通过删除 listview 子属性的 CONST 关键字在一个最小的应用程序中重现了您的代码,并且它工作得很好。在这里检查

\n\n
import 'package:flutter/material.dart';\nimport 'package:ui_learning/appScreen.dart';\nvoid main() {\nrunApp(MaterialApp(\ntitle: 'Flutter Tutorial',\nhome: TutorialHome(),\n));\n}\n\nclass TutorialHome extends StatelessWidget {\n@override\nWidget build(BuildContext context) {\n// Scaffold is a layout for the major Material Components.\nreturn Scaffold(\n  drawer: Drawer(\n      child: ListView(children: <Widget>[\n    SizedBox(\n      height: 10,\n    ),\n    ListTile(\n      leading: Icon(Icons.message),\n      title: Text('Reportes de da\xc3\xb1os recibidos'),\n      onTap: () {\n        Navigator.push(\n            context, MaterialPageRoute(builder: (context) => AppScreen()));\n      },\n    )\n  ])),\n  appBar: AppBar(\n    leading: IconButton(\n      icon: Icon(Icons.menu),\n      tooltip: 'Navigation menu',\n      onPressed: null,\n    ),\n    title: Text('Example title'),\n    actions: <Widget>[\n      IconButton(\n        icon: Icon(Icons.search),\n        tooltip: 'Search',\n        onPressed: null,\n      ),\n    ],\n  ),\n  // body is the majority of the screen.\n  body: Center(\n    child: Text('Hello, world!'),\n  ),\n  floatingActionButton: FloatingActionButton(\n    tooltip: 'Add', // used by assistive technologies\n    child: Icon(Icons.add),\n      onPressed: null,\n     ),\n  );\n }\n }\n
Run Code Online (Sandbox Code Playgroud)\n