英文:
Display sqflite database data as table in flutter
问题
// 我创建了一个 sqflite 数据库,并创建了一个 `query()` 函数来在控制台中打印数据
_query() async {
final allRows = await dbHelper.queryAllRows();
debugPrint('查询所有行:');
for (final row in allRows) {
debugPrint(row.toString());
}
}
// 我需要以表格或表格视图的方式查看这些数据(可以通过列表方法或直接方法来实现)...
// 数据的格式。
Future _onCreate(Database db, int version) async {
await db.execute('''
CREATE TABLE $table (
$columnId INTEGER PRIMARY KEY,
$columnName TEXT NOT NULL,
$columnBatch TEXT NOT NULL,)
''');
}
英文:
I made a sqflite database and made query()
function to print the data in console
_query() async {
final allRows = await dbHelper.queryAllRows();
debugPrint('query all rows:');
for (final row in allRows) {
debugPrint(row.toString());
}
}
I need to view this data in a table or tableview (either through a list method or directly)...
format of data.
Future _onCreate(Database db, int version) async {
await db.execute('''
CREATE TABLE $table (
$columnId INTEGER PRIMARY KEY,
$columnName TEXT NOT NULL,
$columnBatch TEXT NOT NULL,)''');
答案1
得分: 1
class ModelClass {
int? id;
String? name;
String? batch;
ModelClass({this.id, this.name, this.batch});
ModelClass.fromJson(Map<String, dynamic> json) {
id = json['_id'];
name = json['name'];
batch = json['batch'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = {};
data['_id'] = id;
data['name'] = name;
data['batch'] = batch;
return data;
}
}
Future<List
final allRows = await dbHelper.queryAllRows();
debugPrint('query all rows:');
final List
allRows.forEach((row) {
modelClassList.add(ModelClass.fromJson(row));
});
return modelClassList;
}
FutureBuilder(
future: _query(),
builder: (context, snapshot) {
if (snapshot.hasData) {
final List
snapshot.data as List
return ListView.builder(
itemCount: modelClassList.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(modelClassList[index].name ?? " "),
subtitle: Text(modelClassList[index].batch ?? " "),
leading: CircleAvatar(
child: Text(modelClassList[index].id.toString()),
),
);
},
);
} else {
return const Center(
child: CircularProgressIndicator(),
);
}
},
)
英文:
first create a model class like this
> Model Class:
class ModelClass {
int? id;
String? name;
String? batch;
ModelClass({this.id, this.name, this.batch});
ModelClass.fromJson(Map<String, dynamic> json) {
id = json['_id'];
name = json['name'];
batch = json['batch'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = {};
data['_id'] = id;
data['name'] = name;
data['batch'] = batch;
return data;
}
}
now from the method when extracting data from the database and return the list of Model like this:
Future<List<ModelClass>> _query() async {
final allRows = await dbHelper.queryAllRows();
debugPrint('query all rows:');
final List<ModelClass> modelClassList = [];
allRows.forEach((row) {
modelClassList.add(ModelClass.fromJson(row));
});
return modelClassList;
}
and in then create a FutureBuilder in which you can display all the data in the list like this:
FutureBuilder(
future: _query(),
builder: (context, snapshot) {
if (snapshot.hasData) {
final List<ModelClass> modelClassList =
snapshot.data as List<ModelClass>;
return ListView.builder(
itemCount: modelClassList.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(modelClassList[index].name ?? " "),
subtitle: Text(modelClassList[index].batch ?? " "),
leading: CircleAvatar(
child: Text(modelClassList[index].id.toString()),
),
);
},
);
} else {
return const Center(
child: CircularProgressIndicator(),
);
}
},
),
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论