тип 'Future <dynamic>' не является подтипом типа 'Future' - PullRequest
0 голосов
/ 06 июня 2019

Некоторое время мы работали с этим кодом, пытаясь создать базу данных истории во флаттере. Не могу найти проблему с 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>'

1 Ответ

1 голос
/ 06 июня 2019

Проблема в том, что вы не указываете тип возврата вашего getAllHistoryItems метода.

Future<List<History>> getAllHistoryItems() async {
...