在Flutter中将sqflite数据库数据显示为表格:

huangapple go评论74阅读模式
英文:

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> _query() async {
final allRows = await dbHelper.queryAllRows();
debugPrint('query all rows:');
final List modelClassList = [];
allRows.forEach((row) {
modelClassList.add(ModelClass.fromJson(row));
});
return modelClassList;
}

FutureBuilder(
future: _query(),
builder: (context, snapshot) {
if (snapshot.hasData) {
final List modelClassList =
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&lt;String, dynamic&gt; json) {
    id = json[&#39;_id&#39;];
    name = json[&#39;name&#39;];
    batch = json[&#39;batch&#39;];
  }

  Map&lt;String, dynamic&gt; toJson() {
    final Map&lt;String, dynamic&gt; data = {};
    data[&#39;_id&#39;] = id;
    data[&#39;name&#39;] = name;
    data[&#39;batch&#39;] = batch;
    return data;
  }
}

now from the method when extracting data from the database and return the list of Model like this:

Future&lt;List&lt;ModelClass&gt;&gt; _query() async {
  final allRows = await dbHelper.queryAllRows();
  debugPrint(&#39;query all rows:&#39;);
  final List&lt;ModelClass&gt; 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&lt;ModelClass&gt; modelClassList =
            snapshot.data as List&lt;ModelClass&gt;;
        return ListView.builder(
          itemCount: modelClassList.length,
          itemBuilder: (context, index) {
            return ListTile(
              title: Text(modelClassList[index].name ?? &quot; &quot;),
              subtitle: Text(modelClassList[index].batch ?? &quot; &quot;),
              leading: CircleAvatar(
                child: Text(modelClassList[index].id.toString()),
              ),
            );
          },
        );
      } else {
        return const Center(
          child: CircularProgressIndicator(),
        );
      }
    },
  ),

huangapple
  • 本文由 发表于 2023年3月7日 23:39:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75664077.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定