openDb method

Future<void> openDb()

Implementation

Future<void> openDb() async {
  // Get the document path
  final directory = await getApplicationDocumentsDirectory();

  // Get path for flagship directory or create if not exist
  Directory fsFlagshipDir =
      await Directory.fromUri(Uri.file(directory.path + fsDirectory))
          .create(recursive: true)
          .catchError((error) {
    Flagship.logger(Level.DEBUG,
        "Enable to create the flagship directory to save hit and visitor data ");
    throw Exception('Flagship, Failed to create directory flagship');
  });

  // Format the path of the hit database
  String pathToDataBase = join(fsFlagshipDir.path, 'hits_database.db');

  // Format the path for the visitor database
  String pathToDataBaseVisitor =
      join(fsFlagshipDir.path, 'visitor_database.db');

  // Open database for hits
  _hitDatabase = await openDatabase(pathToDataBase, onCreate: (db, version) {
    Flagship.logger(
        Level.DEBUG, " Run the CREATE TABLE hits on the database.");
    return db.execute(
      'CREATE TABLE table_hits(id TEXT PRIMARY KEY, data_hit TEXT)',
    );
  }, version: 1);

  // Open database for visitor data
  _visitorDatabase =
      await openDatabase(pathToDataBaseVisitor, onCreate: (db, version) {
    Flagship.logger(
        Level.DEBUG, "Run the CREATE TABLE visitor on the database");
    return db.execute(
      'CREATE TABLE table_visitors(id TEXT PRIMARY KEY, visitor TEXT)',
    );
  }, version: 1);

  return;
}