Skip to content

Instantly share code, notes, and snippets.

@plateaukao
Created March 11, 2020 15:06
Show Gist options
  • Save plateaukao/9d5e9536282e89301854ae9b9fd313fe to your computer and use it in GitHub Desktop.
Save plateaukao/9d5e9536282e89301854ae9b9fd313fe to your computer and use it in GitHub Desktop.
class GrammarManager {
static const DB_NAME = "grammar.db";
Database _grammarDb;
var _path;
Future<bool> isDBExisting() async {
final _databasesPath = await getDatabasesPath();
if (!Directory(_databasesPath).existsSync()) {
Directory(_databasesPath).createSync();
}
_path = join(_databasesPath, DB_NAME);
return await File(_path).exists();
}
Future<Database> initDB() async {
if (_grammarDb != null) return _grammarDb;
var exists = await isDBExisting();
if(!exists) {
print("Creating new copy from asset");
ByteData data = await rootBundle.load(join("assets", DB_NAME));
List<int> bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
await File(_path).writeAsBytes(bytes);
}
_grammarDb = await openDatabase(_path, readOnly: true);
return _grammarDb;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment