这个常量表达式的计算抛出一个表达式 [英] Evaluation of this constant expression throws an expression
本文介绍了这个常量表达式的计算抛出一个表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的 dart 分析在代码中显示以下错误:
My dart analysis is showing the following error in the code :
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,
),
],
),
],
),
),
);
}
}
推荐答案
去掉 ListTile
前的 const
关键字.
Remove the const
keyword before the ListTile
.
一般来说,为了有一个编译时常量的小部件,它的所有属性也应该是编译时常量.在您的情况下,这是不可能的,因为 title
属性只能通过评估 name
来实现,这仅在运行时才知道.
Generally, in order to have a widget that is compile-time constant, all of its properties should also be compile-time constants. In your case it isn't possible because the title
property can only be achieved by evaluating name
, which is only known at runtime.
这篇关于这个常量表达式的计算抛出一个表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文