Listview 颤振-具有服务和模型的未来构建器
我正在尝试使用FutureBuilder显示无限列表。我想在不同的.dart文件中使用模型和服务,所以我需要在FutureBuilder中注入服务。我认为问题在于服务没有返回列表,但我不知道为什么。代码是 模型Listview 颤振-具有服务和模型的未来构建器,listview,flutter,Listview,Flutter,我正在尝试使用FutureBuilder显示无限列表。我想在不同的.dart文件中使用模型和服务,所以我需要在FutureBuilder中注入服务。我认为问题在于服务没有返回列表,但我不知道为什么。代码是 模型 class Datameter { int idAgrupacion; int nroSerie; String fecha; /// double latitud; double longitud; String contador; String se
class Datameter {
int idAgrupacion;
int nroSerie;
String fecha;
///
double latitud;
double longitud;
String contador;
String sensor;
Datameter(
{this.idAgrupacion,
this.nroSerie,
this.fecha,
///
this.latitud,
this.longitud,
this.contador,
this.sensor});
factory Datameter.fromJson(json) => new Datameter(
nroSerie: json["nro_serie"],
idAgrupacion: json["id_agrupacion"],
fecha: json["fecha"],
//
latitud: json["latitud"],
longitud: json["longitud"],
contador: json["contador"],
sensor: json["sensor"],
);
}
服务
static Future<Datameter> getDatameters(dropdown) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
final token = prefs.getString('token');
final response = await http.get(
'http://192.168.3.64:8080/datameter/agrupacion/info?agrupacion=' +
dropdown +
'&limite=0',
headers: {
HttpHeaders.authorizationHeader: "Token " + token
}).catchError((error) {
print(error.toString());
});
if (response.statusCode == 200) {
var responseJson = json.decode(response.body);
return responseJson["body"].map((item) => Datameter.fromJson(item)).toList();
}
return null;
}
问题:Return'type DatameterList不是'Widget'类型的子类型
那么:使用FutureBuilder显示列表的正确方式是什么呢?生成器函数应该返回一个小部件,就像您注释掉的那样:
FutureBuilder(
future: DatameterService.getInstalations("2"),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return ListView.separated(
separatorBuilder: (context, index) => Divider(
color: Colors.grey,
),
itemCount: snapshot.data.length,
itemBuilder: (context, i) {
return ListTile(
leading: Icon(FontAwesomeIcons.tint, size: 24, color: Colors.black),
trailing: Text(DateFormat('yyyy/MM/dd HH:mm')
.format(DateTime.fromMillisecondsSinceEpoch(
snapshot.data[i]['fecha'])
).toString()
),
onTap: null,
title: Text(
snapshot.data[i]['nro_serie'],
),
);
});
} else {
return CircularProgressIndicator();
}
}),
我评论是因为return;'类DatameterList'没有实例获取程序'Length'。接收器:“DatameterList”的实例。尝试呼叫:长度。当然,这是因为您的未来不会返回一个列表:“returndatameterlist.fromJson(responseJson);”如何将元素转换为列表?我是颤振方面的新手…这将取决于如何接收响应,你能提供一个有效载荷的例子吗?。但基本上,如果responseJson类似于“[{},{}…]”,您可以将值映射到一个列表:responseJson.map((item)=>DatameterList.fromJson(item)).toList()返回(u TypeError(type)(dynamic)=>Datameter'不是类型的子类型(String,dynamic)=>transform的MapEntry)
FutureBuilder(
future: DatameterService.getInstalations("2"),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return ListView.separated(
separatorBuilder: (context, index) => Divider(
color: Colors.grey,
),
itemCount: snapshot.data.length,
itemBuilder: (context, i) {
return ListTile(
leading: Icon(FontAwesomeIcons.tint, size: 24, color: Colors.black),
trailing: Text(DateFormat('yyyy/MM/dd HH:mm')
.format(DateTime.fromMillisecondsSinceEpoch(
snapshot.data[i]['fecha'])
).toString()
),
onTap: null,
title: Text(
snapshot.data[i]['nro_serie'],
),
);
});
} else {
return CircularProgressIndicator();
}
}),