Skip to content
Advertisement

Flutter SQLlite the argument type ‘Future’ can’t be assigned to the parameter type ‘String’ error

I have copied and modified a bit the code from this official tutorial: https://flutter.dev/docs/cookbook/persistence/sqlite
My version of the insertion looks like this:

final Future<Database> database = openDatabase(
    join(await getDatabasesPath(), 'user_database.db'),
    onCreate: (db, version) {
      return db.execute(
        "CREATE TABLE users(uid INTEGER PRIMARY KEY, display name TEXT, email TEXT)",
      );
    },
    version: 1,
  );
  Future<void> insertUser(User user) async {
  final Database db = await database;

  await db.insert(
    'users',
    user.toMap(),
    conflictAlgorithm: ConflictAlgorithm.replace,
  );

  final u = User("_displayName", "_email", "_ownerOfClub", "_password", "_id");

  await insertUser(u);

But it shows an error for the await getDatabasesPath() part: for the await it says: Unexpected text ‘await’, and for the getDatabasesPath(): The argument type ‘Future’ can’t be assigned to the parameter type ‘String’.

Advertisement

Answer

try this approach

Future<Database> database ;

void openDBFunction() async {
var databasesPath  = await getDatabasesPath();
String path = join(databasesPath, 'demo.db');

// open the database
    database = await openDatabase(path, version: 1,
    onCreate: (Database db, int version) async {
  // When creating the db, create the table
  await db.execute(
      'CREATE TABLE Test (id INTEGER PRIMARY KEY, name TEXT, value INTEGER, num REAL)');
});
}

or

void openDBFunction() async {

// open the database
    database = await openDatabase(join(await getDatabasesPath(), 'demo.db'), version: 1,
    onCreate: (Database db, int version) async {
  // When creating the db, create the table
  await db.execute(
      'CREATE TABLE Test (id INTEGER PRIMARY KEY, name TEXT, value INTEGER, num REAL)');
});
}


hope it helps

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement