Flutter: How to best instantiate SQFlite db object when class is created Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Flutter - How to update the table structure in Sqflite?How to do a database insert with SQFlite in FlutterHow to do a database update with SQFlite in FlutterHow to do a database query with SQFlite in FlutterHow can I send data sqflite (offline) to server mysql using Flutter?How to save api fetched data into local database using sqflite in flutter?How to get the total of column values in sqflite in flutter?Drop table and recreate if exist in sqflite flutterSqflite in flutter, working with two databases (attach)(Flutter) Creating SQFLite database automatically when a new project is created
Significance of Cersei's obsession with elephants?
また usage in a dictionary
Why didn't Eitri join the fight?
Dating a Former Employee
What causes the direction of lightning flashes?
Is there such thing as an Availability Group failover trigger?
Trademark violation for app?
Wu formula for manifolds with boundary
old style "caution" boxes
Can an alien society believe that their star system is the universe?
Delete nth line from bottom
How could we fake a moon landing now?
How do pianists reach extremely loud dynamics?
Fantasy story; one type of magic grows in power with use, but the more powerful they are, they more they are drawn to travel to their source
Why aren't air breathing engines used as small first stages
Can you use the Shield Master feat to shove someone before you make an attack by using a Readied action?
Is CEO the profession with the most psychopaths?
Using audio cues to encourage good posture
Is there a kind of relay only consumes power when switching?
Around usage results
Does classifying an integer as a discrete log require it be part of a multiplicative group?
What is the meaning of the simile “quick as silk”?
Do square wave exist?
How do I find out the mythology and history of my Fortress?
Flutter: How to best instantiate SQFlite db object when class is created
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Flutter - How to update the table structure in Sqflite?How to do a database insert with SQFlite in FlutterHow to do a database update with SQFlite in FlutterHow to do a database query with SQFlite in FlutterHow can I send data sqflite (offline) to server mysql using Flutter?How to save api fetched data into local database using sqflite in flutter?How to get the total of column values in sqflite in flutter?Drop table and recreate if exist in sqflite flutterSqflite in flutter, working with two databases (attach)(Flutter) Creating SQFLite database automatically when a new project is created
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'm learning Flutter and am on to my second program. This program uses SQFlite, and I copied most of the db handling from another program which appears to use a common pattern. I have made some modifications to it.
What I don't like, is having to evaluate the db every time to determine if it needs to be created as in "Database db = await this.db;". I would prefer it if it was simply created when the class is created, and then just used as instantiated. However, I'm unfamiliar with "._internal", and unfamiliar with "factory", so I'm unsure of the best way to achieve that aim.
Would appreciate if someone could show me how to best achieve that. IE. Remove the need for "Database db = await this.db;", and just reference the db.
Extract of relevant code for DbHelper is as follows:
import 'package:sqflite/sqflite.dart';
import 'dart:async';
import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'noteRec.dart';
class DbHelper {
static final DbHelper _dbHelper = DbHelper._internal();
static const String sTblNotes = "Notes";
static const String sColId = "Id";
static const String sColTitle = "Title";
static const String sColDetail = "Detail";
DbHelper._internal();
factory DbHelper()
return _dbHelper;
static Database _db;
Future<Database> get db async
return _db != null ? _db : await initializeDb();
Future<Database> initializeDb() async
Directory dir = await getApplicationDocumentsDirectory();
String path = dir.path + "/Notes.db";
_db = await openDatabase(path, version: 1, onCreate: _createDb);
return _db;
void _createDb(Database db, int newVersion) async
await db.execute(
"CREATE TABLE $sTblNotes($sColId INTEGER PRIMARY KEY, $sColTitle TEXT, " +
"$sColDetail TEXT)");
Future<List> getNoteRecs() async
Database db = await this.db;
var result =
await db.rawQuery("SELECT * FROM $sTblNotes ORDER BY $sColTitle ASC");
return result;
flutter sqflite
add a comment |
I'm learning Flutter and am on to my second program. This program uses SQFlite, and I copied most of the db handling from another program which appears to use a common pattern. I have made some modifications to it.
What I don't like, is having to evaluate the db every time to determine if it needs to be created as in "Database db = await this.db;". I would prefer it if it was simply created when the class is created, and then just used as instantiated. However, I'm unfamiliar with "._internal", and unfamiliar with "factory", so I'm unsure of the best way to achieve that aim.
Would appreciate if someone could show me how to best achieve that. IE. Remove the need for "Database db = await this.db;", and just reference the db.
Extract of relevant code for DbHelper is as follows:
import 'package:sqflite/sqflite.dart';
import 'dart:async';
import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'noteRec.dart';
class DbHelper {
static final DbHelper _dbHelper = DbHelper._internal();
static const String sTblNotes = "Notes";
static const String sColId = "Id";
static const String sColTitle = "Title";
static const String sColDetail = "Detail";
DbHelper._internal();
factory DbHelper()
return _dbHelper;
static Database _db;
Future<Database> get db async
return _db != null ? _db : await initializeDb();
Future<Database> initializeDb() async
Directory dir = await getApplicationDocumentsDirectory();
String path = dir.path + "/Notes.db";
_db = await openDatabase(path, version: 1, onCreate: _createDb);
return _db;
void _createDb(Database db, int newVersion) async
await db.execute(
"CREATE TABLE $sTblNotes($sColId INTEGER PRIMARY KEY, $sColTitle TEXT, " +
"$sColDetail TEXT)");
Future<List> getNoteRecs() async
Database db = await this.db;
var result =
await db.rawQuery("SELECT * FROM $sTblNotes ORDER BY $sColTitle ASC");
return result;
flutter sqflite
add a comment |
I'm learning Flutter and am on to my second program. This program uses SQFlite, and I copied most of the db handling from another program which appears to use a common pattern. I have made some modifications to it.
What I don't like, is having to evaluate the db every time to determine if it needs to be created as in "Database db = await this.db;". I would prefer it if it was simply created when the class is created, and then just used as instantiated. However, I'm unfamiliar with "._internal", and unfamiliar with "factory", so I'm unsure of the best way to achieve that aim.
Would appreciate if someone could show me how to best achieve that. IE. Remove the need for "Database db = await this.db;", and just reference the db.
Extract of relevant code for DbHelper is as follows:
import 'package:sqflite/sqflite.dart';
import 'dart:async';
import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'noteRec.dart';
class DbHelper {
static final DbHelper _dbHelper = DbHelper._internal();
static const String sTblNotes = "Notes";
static const String sColId = "Id";
static const String sColTitle = "Title";
static const String sColDetail = "Detail";
DbHelper._internal();
factory DbHelper()
return _dbHelper;
static Database _db;
Future<Database> get db async
return _db != null ? _db : await initializeDb();
Future<Database> initializeDb() async
Directory dir = await getApplicationDocumentsDirectory();
String path = dir.path + "/Notes.db";
_db = await openDatabase(path, version: 1, onCreate: _createDb);
return _db;
void _createDb(Database db, int newVersion) async
await db.execute(
"CREATE TABLE $sTblNotes($sColId INTEGER PRIMARY KEY, $sColTitle TEXT, " +
"$sColDetail TEXT)");
Future<List> getNoteRecs() async
Database db = await this.db;
var result =
await db.rawQuery("SELECT * FROM $sTblNotes ORDER BY $sColTitle ASC");
return result;
flutter sqflite
I'm learning Flutter and am on to my second program. This program uses SQFlite, and I copied most of the db handling from another program which appears to use a common pattern. I have made some modifications to it.
What I don't like, is having to evaluate the db every time to determine if it needs to be created as in "Database db = await this.db;". I would prefer it if it was simply created when the class is created, and then just used as instantiated. However, I'm unfamiliar with "._internal", and unfamiliar with "factory", so I'm unsure of the best way to achieve that aim.
Would appreciate if someone could show me how to best achieve that. IE. Remove the need for "Database db = await this.db;", and just reference the db.
Extract of relevant code for DbHelper is as follows:
import 'package:sqflite/sqflite.dart';
import 'dart:async';
import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'noteRec.dart';
class DbHelper {
static final DbHelper _dbHelper = DbHelper._internal();
static const String sTblNotes = "Notes";
static const String sColId = "Id";
static const String sColTitle = "Title";
static const String sColDetail = "Detail";
DbHelper._internal();
factory DbHelper()
return _dbHelper;
static Database _db;
Future<Database> get db async
return _db != null ? _db : await initializeDb();
Future<Database> initializeDb() async
Directory dir = await getApplicationDocumentsDirectory();
String path = dir.path + "/Notes.db";
_db = await openDatabase(path, version: 1, onCreate: _createDb);
return _db;
void _createDb(Database db, int newVersion) async
await db.execute(
"CREATE TABLE $sTblNotes($sColId INTEGER PRIMARY KEY, $sColTitle TEXT, " +
"$sColDetail TEXT)");
Future<List> getNoteRecs() async
Database db = await this.db;
var result =
await db.rawQuery("SELECT * FROM $sTblNotes ORDER BY $sColTitle ASC");
return result;
flutter sqflite
flutter sqflite
edited Mar 21 at 5:07
Brian Oh
asked Mar 21 at 4:21
Brian OhBrian Oh
2,09372540
2,09372540
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The following appears to do what I want to achieve.
dbHelper.dart
import 'dart:async';
import 'dart:io';
import 'package:sqflite/sqflite.dart';
import 'package:path_provider/path_provider.dart';
import 'noteRec.dart';
class DbHelper {
static final DbHelper _dbHelper = DbHelper._internal();
static const String sTblNotes = "Notes";
static const String sColId = "id";
static const String sColTitle = "title";
static const String sColDetail = "detail";
static Database _db;
DbHelper._internal();
factory DbHelper()
return _dbHelper;
Future<bool> openDb() async
if (_db == null)
Directory dir = await getApplicationDocumentsDirectory();
_db = await openDatabase("$dir.path/Notes.db",
version: 1, onCreate: _createDb);
return (_db != null);
void _createDb(Database db, int newVersion) async
await db.execute(
"CREATE TABLE $sTblNotes($sColId INTEGER PRIMARY KEY, $sColTitle TEXT, " +
"$sColDetail TEXT)");
Future<List> getNoteRecs() async
return await _db
.rawQuery("SELECT * FROM $sTblNotes ORDER BY $sColTitle ASC");
main.dart
import 'package:flutter/material.dart';
import 'dbHelper.dart';
import 'NotesList.dart';
DbHelper _dbHelper = DbHelper();
void main() async
await _dbHelper.openDb();
runApp(MaterialApp(home: NotesList()));
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55273749%2fflutter-how-to-best-instantiate-sqflite-db-object-when-class-is-created%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The following appears to do what I want to achieve.
dbHelper.dart
import 'dart:async';
import 'dart:io';
import 'package:sqflite/sqflite.dart';
import 'package:path_provider/path_provider.dart';
import 'noteRec.dart';
class DbHelper {
static final DbHelper _dbHelper = DbHelper._internal();
static const String sTblNotes = "Notes";
static const String sColId = "id";
static const String sColTitle = "title";
static const String sColDetail = "detail";
static Database _db;
DbHelper._internal();
factory DbHelper()
return _dbHelper;
Future<bool> openDb() async
if (_db == null)
Directory dir = await getApplicationDocumentsDirectory();
_db = await openDatabase("$dir.path/Notes.db",
version: 1, onCreate: _createDb);
return (_db != null);
void _createDb(Database db, int newVersion) async
await db.execute(
"CREATE TABLE $sTblNotes($sColId INTEGER PRIMARY KEY, $sColTitle TEXT, " +
"$sColDetail TEXT)");
Future<List> getNoteRecs() async
return await _db
.rawQuery("SELECT * FROM $sTblNotes ORDER BY $sColTitle ASC");
main.dart
import 'package:flutter/material.dart';
import 'dbHelper.dart';
import 'NotesList.dart';
DbHelper _dbHelper = DbHelper();
void main() async
await _dbHelper.openDb();
runApp(MaterialApp(home: NotesList()));
add a comment |
The following appears to do what I want to achieve.
dbHelper.dart
import 'dart:async';
import 'dart:io';
import 'package:sqflite/sqflite.dart';
import 'package:path_provider/path_provider.dart';
import 'noteRec.dart';
class DbHelper {
static final DbHelper _dbHelper = DbHelper._internal();
static const String sTblNotes = "Notes";
static const String sColId = "id";
static const String sColTitle = "title";
static const String sColDetail = "detail";
static Database _db;
DbHelper._internal();
factory DbHelper()
return _dbHelper;
Future<bool> openDb() async
if (_db == null)
Directory dir = await getApplicationDocumentsDirectory();
_db = await openDatabase("$dir.path/Notes.db",
version: 1, onCreate: _createDb);
return (_db != null);
void _createDb(Database db, int newVersion) async
await db.execute(
"CREATE TABLE $sTblNotes($sColId INTEGER PRIMARY KEY, $sColTitle TEXT, " +
"$sColDetail TEXT)");
Future<List> getNoteRecs() async
return await _db
.rawQuery("SELECT * FROM $sTblNotes ORDER BY $sColTitle ASC");
main.dart
import 'package:flutter/material.dart';
import 'dbHelper.dart';
import 'NotesList.dart';
DbHelper _dbHelper = DbHelper();
void main() async
await _dbHelper.openDb();
runApp(MaterialApp(home: NotesList()));
add a comment |
The following appears to do what I want to achieve.
dbHelper.dart
import 'dart:async';
import 'dart:io';
import 'package:sqflite/sqflite.dart';
import 'package:path_provider/path_provider.dart';
import 'noteRec.dart';
class DbHelper {
static final DbHelper _dbHelper = DbHelper._internal();
static const String sTblNotes = "Notes";
static const String sColId = "id";
static const String sColTitle = "title";
static const String sColDetail = "detail";
static Database _db;
DbHelper._internal();
factory DbHelper()
return _dbHelper;
Future<bool> openDb() async
if (_db == null)
Directory dir = await getApplicationDocumentsDirectory();
_db = await openDatabase("$dir.path/Notes.db",
version: 1, onCreate: _createDb);
return (_db != null);
void _createDb(Database db, int newVersion) async
await db.execute(
"CREATE TABLE $sTblNotes($sColId INTEGER PRIMARY KEY, $sColTitle TEXT, " +
"$sColDetail TEXT)");
Future<List> getNoteRecs() async
return await _db
.rawQuery("SELECT * FROM $sTblNotes ORDER BY $sColTitle ASC");
main.dart
import 'package:flutter/material.dart';
import 'dbHelper.dart';
import 'NotesList.dart';
DbHelper _dbHelper = DbHelper();
void main() async
await _dbHelper.openDb();
runApp(MaterialApp(home: NotesList()));
The following appears to do what I want to achieve.
dbHelper.dart
import 'dart:async';
import 'dart:io';
import 'package:sqflite/sqflite.dart';
import 'package:path_provider/path_provider.dart';
import 'noteRec.dart';
class DbHelper {
static final DbHelper _dbHelper = DbHelper._internal();
static const String sTblNotes = "Notes";
static const String sColId = "id";
static const String sColTitle = "title";
static const String sColDetail = "detail";
static Database _db;
DbHelper._internal();
factory DbHelper()
return _dbHelper;
Future<bool> openDb() async
if (_db == null)
Directory dir = await getApplicationDocumentsDirectory();
_db = await openDatabase("$dir.path/Notes.db",
version: 1, onCreate: _createDb);
return (_db != null);
void _createDb(Database db, int newVersion) async
await db.execute(
"CREATE TABLE $sTblNotes($sColId INTEGER PRIMARY KEY, $sColTitle TEXT, " +
"$sColDetail TEXT)");
Future<List> getNoteRecs() async
return await _db
.rawQuery("SELECT * FROM $sTblNotes ORDER BY $sColTitle ASC");
main.dart
import 'package:flutter/material.dart';
import 'dbHelper.dart';
import 'NotesList.dart';
DbHelper _dbHelper = DbHelper();
void main() async
await _dbHelper.openDb();
runApp(MaterialApp(home: NotesList()));
answered Mar 22 at 9:34
Brian OhBrian Oh
2,09372540
2,09372540
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55273749%2fflutter-how-to-best-instantiate-sqflite-db-object-when-class-is-created%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown