类型 'Future<dynamic>' 不是类型 'Future<List<History>>' 的子类型
type 'Future<dynamic>' is not a subtype of type 'Future<List<History>>'
使用这段代码已经有一段时间了,试图在 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>'
的子类型
问题是您没有指定 getAllHistoryItems
方法的 return 类型。
Future<List<History>> getAllHistoryItems() async {
使用这段代码已经有一段时间了,试图在 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>'
的子类型问题是您没有指定 getAllHistoryItems
方法的 return 类型。
Future<List<History>> getAllHistoryItems() async {