[英]FLUTTER+SQL: display Data from Database in ListViewBuilder
Im using MS SQL and I want to read data and display it in a ListViewBuilder, but Im having some difficulties.我正在使用 MS SQL,我想读取数据并将其显示在 ListViewBuilder 中,但我遇到了一些困难。
I dont know how to find itemCount (biggest problem)
我不知道如何找到itemCount (最大的问题)
I dont know how to display Data in ListTile
我不知道如何在 ListTile 中显示数据
Below I will show a part of code and how I want to be shown in app:下面我将展示一部分代码以及我希望如何在应用程序中显示:
// This is to get and display in terminal data from db Future<void> read(String query) async { var res = await SqlConn.readData(query); debugPrint(res.toString()); }
Data that I need to put in ListTile我需要放入 ListTile 的数据
This is how I want to show data (Red underlines are main problem)这就是我想要显示数据的方式(红色下划线是主要问题)
try this:尝试这个:
FutureBuilder(
future: read(query),
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return Text('Loading....');
default:
if (snapshot.hasError)
return Text('Error: ${snapshot.error}');
else
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
return Text(snapshot.data[index]['name']);
});
return Text('Result: ${snapshot.data}');
}
})
First at all, I would recommend you to map the data into a model, its considered a good practice and avoid possible type casting related bugs.首先,我建议您将 map 数据转换为 model,这被认为是一种很好的做法,可以避免可能的类型转换相关错误。
Second, you could use a FutureBuilder to make the asynchronous call to the SQL database.其次,您可以使用FutureBuilder对 SQL 数据库进行异步调用。 I also would modify your
read
method to return to retrieved data of the database and work together with FutureBuilder.我还将修改您的
read
方法以返回检索到的数据库数据并与 FutureBuilder 一起工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.