3

我是颤振应用程序开发的新手。在我的颤振应用程序中,我使用路径提供程序插件和 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

这是我得到的日志。

4

2 回答 2

1

您需要有一个 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();
  }
}
于 2018-07-24T11:17:31.700 回答
1

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

于 2018-10-04T21:48:55.940 回答