开始使用这段代码已有一段时间,试图创建一个历史数据库。似乎找不到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(dirty,state:_HistoryPageState#3d447)时引发了以下断言: I / flutter(25281):“ Future”类型不是“ Future>”类型的子类型
答案 0 :(得分:1)
问题是您没有指定getAllHistoryItems
方法的返回类型。
Future<List<History>> getAllHistoryItems() async {