3

I'm trying to run my flutter app on my iPhone 7 but I'm receiving an error message. Running the app on my Android phone as well as on the iPhone XR simulator works fine. I also tried to test the app on my iPad and I got the exact same error message as shown below. Replacing the ios folder with a fresh copy did not solve the issue.

I've tried to run the latest sqflite version and flutter doctor shows no issues.

Future<Database> initDatabase() async {
    //Get the dir
    Directory directory = await getApplicationDocumentsDirectory();
    String path = directory.path + 'posts.db';

    //Open or Create the database using given path
    var postsDataBase = openDatabase(path, version: 1, onCreate: _createDb);
    return postsDataBase;
  }

[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: DatabaseException(open_failed /var/mobile/Containers/Data/Application/04C0A23F-6C36-42C2-9CE5-C7C5370F59FA/Documentsposts.db)
#0      wrapDatabaseException 
package:sqflite/src/exception_impl.dart:11
<asynchronous suspension>
#1      SqfliteDatabaseFactoryImpl.wrapDatabaseException 
package:sqflite/src/factory_impl.dart:29
#2      SqfliteDatabaseMixin.safeInvokeMethod 
package:sqflite/src/database_mixin.dart:184
#3      SqfliteDatabaseMixin.openDatabase 
package:sqflite/src/database_mixin.dart:519
<asynchronous suspension>
#4      SqfliteDatabaseMixin.doOpen 
package:sqflite/src/database_mixin.dart:612
<asynchronous suspension>
#5      SqfliteDatabaseOpenHelper.openDatabase 
package:sqflite/src/database.dart:32
<asynchronous suspension>
#6      SqfliteDatabaseFactoryMixin.openDatabase.<anonymous closure> 
package:sqflite/src/factory_mixin.dart:100
<asynchronous suspension>
#7      ReentrantLock.synchronized.<anonymous closure>.<anonymous closure> 
package:synchronized/src/reentrant_lock.dart:33
#8      _rootRun  (dart:async/zone.dart:1124:13)

3 Answers3

0

Check whether the parent directory exists or not and try using the latest sqlite version.

Kothai
  • 115
  • 1
  • 6
0

This worked for me.

String path = directory.path + '/' + 'posts.db';
Jonathan Perez
  • 254
  • 1
  • 7
0

As you can see in the logs, the path you build ends with /Documentsposts.db where I think you likely want /Documents/posts.db

Don't use the plus sign to build a path but use join from the package path.

i.e. instead of:

String path = directory.path + 'posts.db';

do

var path = join(directory.path, 'posts.db');

On iOS you should also make sure the parent directory exists first.

alextk
  • 5,713
  • 21
  • 34