私はフラッターアプリケーション開発に不慣れです。 Flutterアプリケーションでは、パスプロバイダープラグインとSQFLiteを使用してデータベースを作成します。しかし、それは機能していません。例外メッセージタイプ「Future」がタイプ「Future」のサブタイプではないことを示しています。
ここにコードを追加します
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
これは私が取得したログです。
onCreate()
関数は(void
ではなく)Future
型であり、async
である必要があります。