类型 'Future<dynamic>' 不是 Flutter 数据库创建中的“Future<int>”类型异常的子类型

Posted

技术标签:

【中文标题】类型 \'Future<dynamic>\' 不是 Flutter 数据库创建中的“Future<int>”类型异常的子类型【英文标题】:type 'Future<dynamic>' is not a subtype of type 'Future<int>' exception in flutter database creation类型 'Future<dynamic>' 不是 Flutter 数据库创建中的“Future<int>”类型异常的子类型 【发布时间】:2018-12-31 15:17:42 【问题描述】:

我是 Flutter 应用程序开发的新手。在我的颤振应用程序中,我使用路径提供程序插件和 SQFLite 创建了一个数据库。但它不起作用,它显示一条异常消息 “未来”类型不是“未来”类型的子类型

我在这里添加我的代码

 static final DatabaseHelper _instance = new DatabaseHelper.internal();
  factory DatabaseHelper() => _instance;

  static Database _db;

  Future<Database> get db async 
    if (_db != null) 
      return _db;
    
    _db = await initDb();
    return _db;
  

  DatabaseHelper.internal();

  initDb() async 
    Directory documentDirectory = await getApplicationDocumentsDirectory();
    String path = join(documentDirectory.path, "tododatabase.db");
    var ourDb = await openDatabase(path, version: 1, onCreate: _onCreate);
    return ourDb;
  

  Future _onCreate(Database db, int version) async 
    await db.execute(
        "CREATE TABLE TodoList(id INTEGER PRIMARY KEY, todoText TEXT)");
    // print("Table is created");
  

//insertion
  Future<int> saveTodo(Todo todo) async 
    var dbClient = await db;
    int res = await dbClient.insert(" TodoList", todo.toMap());
    return res;
  

请帮助我。请给个建议。提前致谢。

0631/com.stunntech.fluttertodoapp E/flutter: [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
                                                                   type '(Database, int) => void' is not a subtype of type '(Database, int) => Future<dynamic>'
                                                                   #0      DatabaseHelper.initDb (package:flutter_todo_app/database/database.dart:29:64)
                                                                   <asynchronous suspension>
                                                                   #1      DatabaseHelper.db (package:flutter_todo_app/database/database.dart:20:17)
                                                                   <asynchronous suspension>
                                                                   #2      DatabaseHelper.saveTodo (package:flutter_todo_app/database/database.dart:40:26)
                                                                   <asynchronous suspension>
                                                                   #3      _MyTodoListState._submitTodo (package:flutter_todo_app/todo_list.dart:144:30)
                                                                   <asynchronous suspension>
                                                                   #4      _MyTodoListState._showAlert.<anonymous closure> (package:flutter_todo_app/todo_list.dart:97:19)
                                                                   #5      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
                                                                   #6      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:161:9)
                                                                   #7      TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:94:7)
                                                                   #8      PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:315:9)
                                                                   #9      PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:73:12)
                                                                   #10     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:11)
                                                                   #11     _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:143:19)
                                                                   #12     _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:121:22)
                                                                   #13     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:101:7)
                                                                   #14     _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:64:7)
                                                                   #15     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:48:7)
                                                                   #16     _invoke1 (dart:ui/hooks.dart:134:13)
                                                                   #17     _dispatchPointerDataPacket (dart:ui/hooks.dart:91:5)enter code here

这是我得到的日志。

【问题讨论】:

哪一行导致错误? var ourDb = await openDatabase(path, version: 1, onCreate: _onCreate); openDatabase() 来自哪里? 它来自数据库类 问题可能出在那儿。 【参考方案1】:

您的onCreate() 函数应该是void 类型(而不是Future)并且应该是async

【讨论】:

【参考方案2】:

您需要有一个 Future 为您的 initDB() 方法返回类型数据库:

Future<Database> initDB() async 
var databasesPath = await getDatabasesPath();
String path = join(databasesPath, "event.db");
var theDatabase = await openDatabase(path, version: 1, onCreate: _onCreate);
return theDatabase;

这里有一个完整的例子:

https://github.com/dazza5000/austin-feeds-me-flutter/blob/master/lib/data/event_database.dart

class EventsDatabase 
  static const EVENT_TABLE_NAME = "event";
  static final EventsDatabase _instance = EventsDatabase._internal();

  factory EventsDatabase() => _instance;

  static Database _db;

  Future<Database> get db async 
    if (_db != null) 
      return _db;
    
    _db = await initDB();
    return _db;
  

  EventsDatabase._internal();

  Future<Database> initDB() async 
    var databasesPath = await getDatabasesPath();
    String path = join(databasesPath, "event.db");
    var theDatabase = await openDatabase(path, version: 1, onCreate: _onCreate);
    return theDatabase;
  

  void _onCreate(Database db, int version) async 
    await db.execute("CREATE TABLE " + EVENT_TABLE_NAME + " ("
        "id STRING PRIMARY KEY, "
        "name TEXT, "
        "time INTEGER, "
        "description TEXT, "
        "url TEXT, "
        "photo TEXT, "
        "lat DOUBLE, "
        "lng DOUBLE)");
  

  Future closeDb() async 
    var dbClient = await db;
    return dbClient.close();
  

【讨论】:

如您所说更改了我的代码。但同样的错误再次出现。 请发布日志、确切的异常和行号。您的问题可能在插入语句中。 我添加了我在问题中得到的日志。你能检查一下吗? 如果您更改了代码,请将更新后的代码也放入。谢谢。 能否更新 onCreate 以返回 void "void _onCreate(Database db, int version) async"

以上是关于类型 'Future<dynamic>' 不是 Flutter 数据库创建中的“Future<int>”类型异常的子类型的主要内容,如果未能解决你的问题,请参考以下文章

类型 'Future<dynamic>' 不是 Flutter 数据库创建中的“Future<int>”类型异常的子类型

Flutter 类型“Future<List<dynamic>>”不是类型转换中“List<dynamic>”类型的子类型

错误:类型“Future<dynamic>”不是“List<dynamic>”类型的子类型。扑

颤振类型错误future <dynamic> 不是future <bool> 类型的子类型

参数类型“Iterable<Future<dynamic>>”不能分配给参数类型“Iterable<Future<news_item>>”

类型“Future<dynamic>”不是类型转换中“List<Profile>”类型的子类型