1

一段时间以来一直在使用此代码,尝试在颤振中创建历史数据库。似乎无法找到 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): type 'Future' is not a subtype of type 'Future>'

4

1 回答 1

1

问题是您没有指定getAllHistoryItems方法的返回类型。

Future<List<History>> getAllHistoryItems() async {
于 2019-06-06T15:24:15.657 回答