Created
March 11, 2020 15:06
-
-
Save plateaukao/9d5e9536282e89301854ae9b9fd313fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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