Last active
January 16, 2019 21:11
-
-
Save ericgrandt/aa31c1aed85f6d3acec86ad836b51627 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
import 'dart:io'; | |
import 'package:sqflite/sqflite.dart'; | |
import 'package:path_provider/path_provider.dart'; | |
import 'package:path/path.dart'; | |
class DBProvider { | |
DBProvider._(); | |
static final DBProvider db = DBProvider._(); | |
Database _database; | |
Future<Database> get database async { | |
if (_database != null) { | |
return _database; | |
} | |
_database = await initDB(); | |
return _database; | |
} | |
initDB() async { | |
Directory documentsDir = await getApplicationDocumentsDirectory(); | |
String path = join(documentsDir.path, 'my_app.db'); | |
return await openDatabase(path, version: 1, onOpen: (db) { | |
}, onCreate: (Database db, int version) async { | |
await db.execute(''' | |
CREATE TABLE user( | |
id INTEGER PRIMARY KEY, | |
name TEXT NOT NULL | |
) | |
'''); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment