FutureBuilder(
future: getData,
builder: (BuildContext, AsyncSnapshot snapshot) {
var sn = snapshot.data;
var tp1 = snapshot.data[0]["currentprice"];
var ent = snapshot.data[0]["entryprice"];
Color col = Colors.blue;
if (ent > tp1) {
col = Colors.red;
} else {
col = Colors.grey;
}
**if语句只适用于=条件,当我使用>继续运行这个错误
类"String"没有实例方法">"。接收者:";1〃;尝试调用:>("1.25454"(
**
将String
值转换为double
FutureBuilder(
future: getData,
builder: (BuildContext, AsyncSnapshot snapshot) {
var sn = snapshot.data;
//convert your String to double
var tp1 = double.parse(snapshot.data[0]["currentprice"]);
var ent = double.parse(snapshot.data[0]["entryprice"]);
Color col = Colors.blue;
if (ent > tp1) {
col = Colors.red;
} else {
col = Colors.grey;
}
});
ent
或tp1
都是字符串。请记住,>
运算符仅适用于双精度和整数。因此,您可以做的是将该String解析为double或int,然后继续执行其余的操作。像这样:
double.parse(ent);
int.parse(tp1);
然后,它会很好地工作!:(