我使用这段代码已经有一段时间了,试图在flutter中创建一个历史数据库。似乎找不到Future的问题。
任何帮助都将不胜感激!
提前谢谢你!
import 'dart:convert';
History historyFromJson(String str) {
final jsonData = json.decode(str);
return History.fromJson(jsonData);
}
String historyToJson(History data) {
final dyn = data.toJson();
return json.encode(dyn);
}
class History {
int id;
String historyItems;
int favorite;
History({
this.id,
this.historyItems,
this.favorite,
});
factory History.fromJson(Map<String, dynamic> json) => new History(
id: json["id"],
historyItems: json["historyItems"],
favorite: json["favorite"],
);
Map<String, dynamic> toJson() => {
"id": id,
"historyItems": historyItems,
"favorite": favorite,
};
}
getAllHistoryItems() async {
final db = await database;
var res = await db.query(TABLE_NAME);
List<History> list =
res.isNotEmpty ? res.map((c) => History.fromJson(c)).toList() : [];
return list;
}
return Scaffold(
appBar: AppBar(title: Text("Flutter SQLite")),
body: FutureBuilder<List<History>>(
future: DBProvider.db.getAllHistoryItems(),
builder: (BuildContext context, AsyncSnapshot<List<History>> snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
History item = snapshot.data[index];
return ListTile(
title: Text(item.historyItems),
leading: Text(item.id.toString()),
);
},
);
} else {
return Center(child: CircularProgressIndicator());
}
},
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () async {
History rnd = testClients[Random().nextInt(testClients.length)];
await DBProvider.db.newHistoryItem(rnd);
setState(() {});
},
),
);
在生成HistoryPage时引发了以下断言(脏,状态:_HistoryPageState#3d447):I/flutter (25281):类型'Future‘不是类型'Future>’的子类型
转载请注明出处:http://www.cxd6.com/article/20230521/1437906.html