android-studio - 对该常量表达式的求值会抛出一个表达式
问题描述
我的飞镖分析在代码中显示以下错误:
error: Evaluation of this constant expression throws an exception.
(const_eval_throws_exception at [laundry]
lib\pick_drop_ui\pages\works.dart:97) error: Arguments of a constant creation must be constant expressions.
(const_with_non_constant_argument at [laundry]
lib\pick_drop_ui\pages\works.dart:98) error: Arguments of a constant creation must be constant expressions.
(const_with_non_constant_argument at [laundry]
lib\pick_drop_ui\pages\works.dart:104) error: Evaluation of this constant expression throws an exception.
(const_eval_throws_exception at [laundry]
lib\pick_drop_ui\pages\works.dart:104)
在以下代码中:
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
class work extends StatefulWidget {
@override
_workState createState() => _workState();
}
class _workState extends State<work> {
var workdata; //Variable to get the snapshort of the works available in the firestore
@override
void initState() {
// TODO: implement initState
super.initState();
print("Called work");
setState(() {
workdata = getData();
});
}
getworkdetails(){
if(workdata != null){
return StreamBuilder(
stream: workdata,
builder: (context,snapshot){
if(snapshot.data != null){
return ListView.builder(
shrinkWrap: true,
itemCount: snapshot.data.documents.length,
itemBuilder: (context,i){
return workcards(snapshot.data.documents[i].data['Name of customer'],snapshot.data.documents[i].data['Address']);
},
);
}else{
return Text("Malfunction");
}
},
);
}else{
print("getting workdata");
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Jobs Assigned"),
),
body: getworkdetails(),
);
}
}
getData() {
return Firestore.instance.collection('Jobs').snapshots();
}
class workcards extends StatelessWidget{
final name;
final address;
workcards(this.name,this.address);
@override
Widget build(BuildContext context) {
// TODO: implement build
return Card(
color: Colors.blueGrey[50],
child: InkWell(
splashColor: Colors.blue[100].withAlpha(100),
onTap: () {
},
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const ListTile(
leading: Icon(Icons.view_module),
// Error: invalid constant value
title: Text(
name,
style: TextStyle(
fontWeight: FontWeight.w600,
letterSpacing: .5,
),
),
// Error: invalid constant value
subtitle: Text(address),
),
ButtonBar(
children: <Widget>[
RaisedButton(
child: const Text('OPEN'),
onPressed: () {/* ... */},
focusElevation: 10,
),
RaisedButton(
child: const Text('SHARE'),
onPressed: () {/* ... */},
focusElevation: 20,
),
],
),
],
),
),
);
}
}
解决方案
去掉const
前面的关键字ListTile
。
通常,为了拥有一个编译时常量的小部件,它的所有属性也应该是编译时常量。在您的情况下,这是不可能的,因为该title
属性只能通过评估来实现name
,这仅在运行时才知道。
推荐阅读
- powershell - PowerShell Out-GridView 未显示所有输出
- python - 如何将用户限制在某些特定的 url
- mayavi - Python3.0 Mayavi旋转立方体字形
- sapui5 - SAPUI5 为什么全局 BusyIndicator 隐藏得太早?
- javascript - 函数内部的笑话模拟函数
- r - 无需注册即可在 R 中获取 gsheet 的最后更新时间
- 3d - 如何将相机发送到默认铯
- java - Java:如何回到循环的开头
- javascript - 使用 Apollo 本地状态存储返回数据的不同表示是否合适
- ffmpeg - 使用 FFMPEG 捕获视频而没有冻结图像效果