Flutter 使用本地 sqlite 文件创建列表视图
Flutter create listview with local sqlite file
使用 sqflite 从本地 sql 文件 (chinook.db) 创建列表视图
初始问题已解决:
I/flutter ( 5084): 'Future' 的实例
参考代码:https://github.com/tekartik/sqflite/blob/master/sqflite/doc/opening_asset_db.md
感谢@aakash 的帮助
main.dart
body: Container(
child: FutureBuilder(
future: getSQL("albums"),
builder: (BuildContext context, AsyncSnapshot snapshot) {
print(snapshot.data);
if (snapshot.data == null) {
return Container(child: Center(child: Text("Loading...")));
} else {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(snapshot.data[index].title),
);
},
);
}
},
),
),
getSQL.dart
Future getSQL(String tableName) async {
var databasesPath = await getDatabasesPath();
var path = join(databasesPath, "chinook.db");
// Check if the database exists
var exists = await databaseExists(path);
if (!exists) {
// Should happen only the first time you launch your application
print("Creating new copy from asset");
// Make sure the parent directory exists
try {
await Directory(dirname(path)).create(recursive: true);
} catch (_) {}
// Copy from asset
ByteData data = await rootBundle.load(join("assets", "chinook.db"));
List<int> bytes =
data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
// Write and flush the bytes written
await File(path).writeAsBytes(bytes, flush: true);
} else {
print("Opening existing database");
}
// open the database
var db = await openDatabase(path, readOnly: true);
List<Map> list = await db.rawQuery('SELECT * FROM $tableName');
List theList = [];
for (var n in list) {
theList.add(MyCategoryFinal(n["Title"]));
}
return (theList);
}
class MyCategoryFinal {
final String title;
MyCategoryFinal(this.title);
}
我通过为 sqflite table 创建一个 class 解决了这个问题,运行 在该地图列表上循环并将这些地图项转换为对象列表。
示例代码;
List<ItemBean> items = new List();
list.forEach((result) {
ItemBean story = ItemBean.fromJson(result);
items.add(story);
});
要创建对象,您可以使用 https://app.quicktype.io/。这里可以传json为其生成class
之后你可以像这样使用 FutureBuilder 创建你的列表视图
FutureBuilder(
future: MyService.getAllItems(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(child: CircularProgressIndicator());
}
return ListView.builder(
controller: listScrollController,
itemCount: snapshot.data.length,
reverse: true,
itemBuilder: (context, index) {
return Text(snapshot.data[index].itemName);
},
);
},
),
使用 sqflite 从本地 sql 文件 (chinook.db) 创建列表视图 初始问题已解决: I/flutter ( 5084): 'Future' 的实例 参考代码:https://github.com/tekartik/sqflite/blob/master/sqflite/doc/opening_asset_db.md 感谢@aakash 的帮助
main.dart
body: Container(
child: FutureBuilder(
future: getSQL("albums"),
builder: (BuildContext context, AsyncSnapshot snapshot) {
print(snapshot.data);
if (snapshot.data == null) {
return Container(child: Center(child: Text("Loading...")));
} else {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(snapshot.data[index].title),
);
},
);
}
},
),
),
getSQL.dart
Future getSQL(String tableName) async {
var databasesPath = await getDatabasesPath();
var path = join(databasesPath, "chinook.db");
// Check if the database exists
var exists = await databaseExists(path);
if (!exists) {
// Should happen only the first time you launch your application
print("Creating new copy from asset");
// Make sure the parent directory exists
try {
await Directory(dirname(path)).create(recursive: true);
} catch (_) {}
// Copy from asset
ByteData data = await rootBundle.load(join("assets", "chinook.db"));
List<int> bytes =
data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
// Write and flush the bytes written
await File(path).writeAsBytes(bytes, flush: true);
} else {
print("Opening existing database");
}
// open the database
var db = await openDatabase(path, readOnly: true);
List<Map> list = await db.rawQuery('SELECT * FROM $tableName');
List theList = [];
for (var n in list) {
theList.add(MyCategoryFinal(n["Title"]));
}
return (theList);
}
class MyCategoryFinal {
final String title;
MyCategoryFinal(this.title);
}
我通过为 sqflite table 创建一个 class 解决了这个问题,运行 在该地图列表上循环并将这些地图项转换为对象列表。
示例代码;
List<ItemBean> items = new List();
list.forEach((result) {
ItemBean story = ItemBean.fromJson(result);
items.add(story);
});
要创建对象,您可以使用 https://app.quicktype.io/。这里可以传json为其生成class
之后你可以像这样使用 FutureBuilder 创建你的列表视图
FutureBuilder(
future: MyService.getAllItems(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(child: CircularProgressIndicator());
}
return ListView.builder(
controller: listScrollController,
itemCount: snapshot.data.length,
reverse: true,
itemBuilder: (context, index) {
return Text(snapshot.data[index].itemName);
},
);
},
),