So basically what i want to do is to display the data i get from my DB in a ListView, but I’m only able to show one attribute of my Object Device.
This my Class for device
class Devices {
final int iD;
final String location;
final String type;
Devices({required this.iD, required this.location, required this.type});
factory Devices.fromJson(Map<String, dynamic> json) {
return Devices(
iD: json['id'] as int,
location: json['Ort'] as String,
type: json['Typ'] as String);
}
}
The json i get from my PHP script looks like this
{"id":1,"Ort":"Wohnzimmer","Typ":"Sensor"}
Code to show my Class
class DeviceList extends StatelessWidget {
List<Devices> dList;
DeviceList({super.key, required this.dList});
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: dList.length,
itemBuilder: (context, index) {
// return Text(dList[index].location,
// style: const TextStyle(color: Colors.white, fontSize: 40));
return (ListTile(title: Text(dList[index].iD.toString())));
},
);
}
}
I want to show all attributes of my Class at the same and not only the iD of it, but i have no clue how to do it.
2
Answers
It depends on how you want to display the other attributes
if you want to use
ListTile
you probably want to make the use of theleading
andtrailing
or use a
Row
inside thetitle
, it’s up to youin ListTile widget you can display your data like this
which will look like this
there are a lot of other ways to display that data.