How to initialize SQLite connection - flutter

I new in Flutter.
I have already exist db and need add it to app.
Put file in assets/db/sms.db
in file pubspec.yaml
assets:
- assets/db/sms.db
Copy db file in applicationDirectory.
In file categoriesList.dart I try connect to it.
SmsDataBaseHelper.instance.getAllCategories()
I get error message.
database_helper.dart
import 'package:flutter/services.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart';
import 'dart:io' as io;
import '/models/categories.dart';
class SmsDataBaseHelper {
static final SmsDataBaseHelper instance = SmsDataBaseHelper._();
static Database? _db;
SmsDataBaseHelper._();
Future<Database> get db async {
_db ??= await _init();
return _db!;
}
Future<Database> _init() async {
io.Directory applicationDirectory = await getApplicationDocumentsDirectory();
String dbPathSms = path.join(applicationDirectory.path, "sms.db");
bool dbExistsSms = await io.File(dbPathSms).exists();
if (!dbExistsSms) {
// Copy from asset
ByteData data = await rootBundle.load(path.join("assets", "db", "sms.db"));
List<int> bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
// Write and flush the bytes written
await io.File(dbPathSms).writeAsBytes(bytes, flush: true);
}
return await openDatabase(dbPathSms);
}
/// get all the words from categories
Future<List<Categories>> getAllCategories() async {
if (_db == null) {
throw "_db is not initiated, initiate using [init(db)] function";
}
List<Map>? categories;
await _db!.transaction((txn) async {
categories = await txn.query(
"categories",
columns: [
"name",
"ru",
],
);
});
return categories!.map((e) => Categories.fromJson(e as Map<String, dynamic>)).toList();
}
}
categoriesList.dart
import 'package:flutter/material.dart';
import 'package:sqflite/sqflite.dart';
import '../utils/database_helper.dart';
import '../models/categories.dart';
class CategoriesList extends StatefulWidget {
const CategoriesList({super.key});
#override
State<CategoriesList> createState() => _CategoriesListState();
}
class _CategoriesListState extends State<CategoriesList> {
//late List<Categories> categoriesList;
#override
void initState(){
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('SMS Home Page'),
),
body: FutureBuilder<List<Categories>>(
future: SmsDataBaseHelper.instance.getAllCategories(),
builder: (BuildContext context, AsyncSnapshot<List<Categories>> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
return const Text('start widget');
case ConnectionState.active:
case ConnectionState.waiting:
return const Text('Awaiting result from api...');
case ConnectionState.done:
if (snapshot.hasError)
return Text('Error: ${snapshot.error}');
return Text('Result: ${snapshot.data}');
}
},
),
);
}
}

use this class to build database ..
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
class SqlDb {
static Database? _db;
Future<Database?> get db async {
if (_db == null) {
_db = await initialDb();
return _db;
} else {
return _db;
}
}
initialDb() async {
String databasepath = await getDatabasesPath();
String path = join(databasepath, 'shop.db');
Database mydb = await openDatabase(path,
onCreate: _onCreate, version: 1, onUpgrade: _onUpgrade);
return mydb;
}
_onUpgrade(Database db, int oldversion, int newversion) async {}
deletemydatabase() async {
String databasepath = await getDatabasesPath();
String path = join(databasepath, 'shop.db');
var x = await deleteDatabase(path);
return x;
}
_onCreate(Database db, int version) async {
await db.execute(
"CREATE TABLE cart (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT , title TEXT, size TEXT , code TEXT , img TEXT , price TEXT )");
}
//Select
readData(String sql) async {
Database? mydb = await db;
List<Map> response = await mydb!.rawQuery(sql);
return response;
}
//insert data
insertData(String sql) async {
Database? mydb = await db;
int response = await mydb!.rawInsert(sql);
return response;
}
deleteData(String sql) async {
Database? mydb = await db;
int response = await mydb!.rawDelete(sql);
return response;
}
updateData(String sql) async {
Database? mydb = await db;
int response = await mydb!.rawUpdate(sql);
return response;
}
}

Related

Issues sqflite in flutter

It's been a while I started flutter. And now I am working on the database part. I started with sqflite since it was the ideal one for offline apps but now I can't understand a thing please can somebody help me with this.
import 'package:flutter/widgets.dart';
import 'note.dart';
import 'package:sqflite/sqflite.dart';
import 'dart:async';
import 'dart:io';
import 'package:path_provider/path_provider.dart';
class DatabaseHelper {
static DatabaseHelper _databaseHelper = DatabaseHelper();
static Database? _database;
String noteTable = 'note_table';
String colID = 'id';
String colTitle = 'title';
String colDescription = 'description';
String colPriority = 'priority';
String colDate = 'date';
DatabaseHelper._createInstance();
factory DatabaseHelper() {
if (_databaseHelper == null) {
var databaseHelper = DatabaseHelper._createInstance();
_databaseHelper = databaseHelper;
}
return _databaseHelper;
}
//custom getter for the database
Future<Database> get database async {
// ignore: prefer_conditional_assignment
if (_database == null) {
_database = await initializeDatabase();
}
return _database!;
}
Future<Database> initializeDatabase() async {
Directory directory = await getApplicationDocumentsDirectory();
String path = directory.path + 'note.db';
var notesDatabase =
await openDatabase(path, version: 1, onCreate: _createDb);
return notesDatabase;
}
void _createDb(Database db, int newVersion) async {
await db.execute(
'CREATE TABLE $noteTable($colID INTEGER PRIMARY KEY AUTOINCREMENT, $colTitle TEXT, $colDescription TEXT, $colPriority INTEGER, $colDate TEXT)');
}
Future<List<Map<String, dynamic>>> getNoteMapList() async {
Database db = await this.database;
var result = await db.query(noteTable, orderBy: '$colPriority ASC');
return result;
}
Future<int> insertData(Note note) async {
Database db = await this.database;
var result = await db.insert(noteTable, note.toMap());
return result;
}
Future<int> updateNote(Note note) async {
Database db = await this.database;
var result = await db
.update(noteTable, note.toMap(), where: '$colID', whereArgs: [note.id]);
return result;
}
Future<int> deleteNote(int id) async {
Database db = await this.database;
var result =
await db.rawDelete('DELETE FROM $noteTable where $colID = $id');
return result;
}
Future<int> getCount() async {
Database db = await this.database;
List<Map<String, dynamic>> x =
await db.rawQuery('SELECT COUNT (*) FROM $noteTable');
int? result = Sqflite.firstIntValue(x);
return result!;
}
Future<List<Note>> getNoteList() async {
var noteMapList = await getNoteMapList();
int count = noteMapList.length;
}
}
This is the whole code. I have watched many videos but can't understand a thing. The problem starts when I the database part starts. And while answering the questions please try to be a little simple.

Flutter's sqflite queries

I am working with an already existing database dictionary.db that has a table words and has 4 columns id, englishWord, germanWord, isFavorite.
Using Sqflite I am trying to return a list of German words based on an input in English pretty much the userflow works like this: user input an English word (example: "Ability") and I return a list of synonyms to that word in German.
My problem is I don't know how to do it the method I am trying returns the full list instead of only the intended search results
This is my Entity:
class Word {
final String id;
final String eng;
final String ger;
final String isFav;
Word({this.id, this.eng, this.ger, this.isFav});
Map<String, dynamic> toMap() {
return {
'wordId': id,
'englishWord': eng,
'germanWord': ger,
'isFavorite': isFav,
};
}
factory Word.fromMap(Map<String, dynamic> json) => new Word(
id: json['wordId'],
eng: json['englishWord'],
ger: json['germanWord'],
isFav: json['isFavorite']
);
}
And this is my database helper class:
class DatabaseHelper{
DatabaseHelper._();
static final DatabaseHelper databaseHelper = DatabaseHelper._();
Database _database;
static const String DB_NAME = "dict.db";
static const String TABLE = "words";
static const String ID = "wordId";
static const String ENGLISH_WORD = "englishWord";
static const String GERMAN_WORD = "germanWord";
static const String IS_FAV = "isFavorite";
Future<Database> get database async {
if (_database != null) return _database;
_database = await getDatabaseInstance();
return _database;
}
Future<Database> getDatabaseInstance() async {
io.Directory directory = await getApplicationDocumentsDirectory();
String path = join(directory.path, DB_NAME);
return await openDatabase(path, version: 1,
onCreate: (Database db, int version) async {
await db.execute("CREATE TABLE IF NOT EXISTS $TABLE ("
"$ID TEXT,"
"$ENGLISH_WORD TEXT,"
"$GERMAN_WORD TEXT,"
"$IS_FAV TEXT"
")");
});
}
//This is where I get the search result list
//I am stuck here I don't know where to go from here, I only get the full list
Future<List<Word>> searchEnglishResults(String userSearch) async{
final db = await database;
var response = await db.query("Word");
List<Word> list = response.map((c) => Word.fromMap(c)).toList();
return list;
}
I assumed the database is like below:
|id | englidhWord | germanWord | isFavorite |
|-------|-------------|-------------------|------------|
| 'id0' | 'bye' | 'Tschüss' | 'false' |
| 'id1' | 'bye' | 'Auf Wiedersehen' | 'true' |
so if the user searches for "bye", she/he should receive [ 'Tschüss', 'Auf Wiedersehen'](the Word model not only the German words).
For that, query has some options like where and whereArgs that you can use to search for a specific row on the database.
Here we want to search the database for rows that their englidhWord field has a value of bye.
Here is the edited version of your DB:
import 'dart:io';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sudoku/src/word.dart';
class DatabaseHelper {
DatabaseHelper._();
static final DatabaseHelper databaseHelper = DatabaseHelper._();
Database _database;
String DB_NAME = "dict.db";
static const String TABLE = "words";
static const String ID = "wordId";
static const String ENGLISH_WORD = "englishWord";
static const String GERMAN_WORD = "germanWord";
static const String IS_FAV = "isFavorite";
Future<Database> get database async {
if (_database != null) return _database;
_database = await getDatabaseInstance();
return _database;
}
Future<Database> getDatabaseInstance() async {
Directory directory = await getApplicationDocumentsDirectory();
String path = join(directory.path, DB_NAME);
return await openDatabase(path, version: 1,
onCreate: (Database db, int version) async {
await db.execute("CREATE TABLE IF NOT EXISTS $TABLE ("
"$ID TEXT,"
"$ENGLISH_WORD TEXT,"
"$GERMAN_WORD TEXT,"
"$IS_FAV TEXT"
")");
});
}
//add new words
Future<int> add(Word word) async {
final db = await database;
var response = await db.insert(TABLE, word.toMap());
return response;
}
//This is where I get the search result list
//I am stuck here I don't know where to go from here, I only get the full list
Future<List<Word>> searchEnglishResults(String userSearch) async {
final db = await database;
var response = await db
.query(TABLE, where: '$ENGLISH_WORD = ?', whereArgs: [userSearch]);
List<Word> list = response.map((c) => Word.fromMap(c)).toList();
return list;
}
}
And I show the result with listview on a page named MyApp
import 'package:flutter/material.dart';
import 'package:sudoku/src/db.dart';
import 'package:sudoku/src/word.dart';
class MyApp extends StatefulWidget {
MyApp({Key key}) : super(key: key);
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
DatabaseHelper db;
#override
void initState() {
db = DatabaseHelper.databaseHelper;
add();
super.initState();
}
add() async {
// await db.add(Word(eng: 'hi', ger: 'Hallo', id: 'id2', isFav: 'true'));
// await db.add(Word(eng: 'bye', ger: 'Tschüss', id: 'id0', isFav: 'fasle'));
// await db.add(
// Word(eng: 'bye', ger: 'Auf Wiedersehen', id: 'id0', isFav: 'true'));
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: FutureBuilder<List<Word>>(
future: db.searchEnglishResults('bye'),
builder: (BuildContext context, AsyncSnapshot<List<Word>> snapshot) {
if (snapshot.hasData)
return ListView.builder(
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(snapshot.data[index].ger),
subtitle: Text(snapshot.data[index].eng),
trailing: Text(snapshot.data[index].isFav),
);
},
itemCount: snapshot.data.length,
);
return Center(
child: CircularProgressIndicator(),
);
},
),
);
}
}
I included a function named "add" in database and MyApp's initState to add new words to database.
If you refresh app for multiple time, it will add repetitive rows, the reason is that you should make one of the DB field unique, here can be id to do so you should create the table like below:
return await openDatabase(path, version: 1,
onCreate: (Database db, int version) async {
await db.execute("CREATE TABLE IF NOT EXISTS $TABLE ("
"$ID TEXT NOT NULL PRIMARY KEY,"
"$ENGLISH_WORD TEXT,"
"$GERMAN_WORD TEXT,"
"$IS_FAV TEXT"
")");
});
Also in add function in DB, you should choose what should be done when it faces to a repetitive row(Word here), there are some options, one can be to replace it, it should change like this:
//add new words
Future<int> add(Word word) async {
final db = await database;
var response = await db.insert(TABLE, word.toMap(), conflictAlgorithm: ConflictAlgorithm.replace);
return response;
}
I hope it was what You were looking for.

Flutter : SQFLITE Insert Image from API

I'm working with Flutter SQFlite.
I have App News and in this App have feature read News Offline, So i go to with SQFlite.
I have been to Insert and Fetch data from SQFlite , But the problem is Image what I insert from API , so if i don't have connection i can't fetch the Image.
I'm Thinking about firstly download Image from API , then save into directory device and calling Image from directory based on ID News.
Can you help me with this case ?
Thank's
My Full Source Code
import 'package:flutter_news/model/Bookmark_model.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:io';
class DBProvider {
DBProvider._();
static final DBProvider db = DBProvider._();
static Database _database;
Future<Database> get database async {
if (_database != null) return _database;
_database = await initDB();
return _database;
}
initDB() async {
Directory documentDirectory = await getApplicationDocumentsDirectory();
String path = join(documentDirectory.path, "flutter_news.db");
return await openDatabase(path,
version: 1, onCreate: _onCreate, onUpgrade: _onUpgrade);
}
void _onUpgrade(Database db, int oldVersion, int newVersion) {
//Run Migration according database versions
}
void _onCreate(Database db, int version) async {
var sqlCreateTable = '''CREATE TABLE bookmark(
id_berita TEXT,
judul_berita TEXT,
isi_berita TEXT,
gambar_berita TEXT,
nama_kategori TEXT,
nama_wartawan TEXT,
tanggal_berita TEXT
)''';
await db.execute(sqlCreateTable);
print("Table Bookmark Has Been Created");
}
// Future newBookmark(Bookmark newBookmark) async {
// final db = await database;
// var res = await db.rawInsert(
// "INSERT INTO tb_bookmark (id_berita,judul_berita,isi_berita,nama_kategori,nama_wartawan,tanggal_berita)VALUES(\'${newBookmark.idBerita}\',\'${newBookmark.judulBerita}\',\'${newBookmark.isiBerita}\',\'${newBookmark.namaKategori}\',\'${newBookmark.namaWartawan}\',\'${newBookmark.tanggalBerita}\')");
// print("Print : $res");
// return res;
// }
Future addBookmark(Bookmark newBookmark) async {
print(newBookmark);
var db = await database;
int res = await db.insert("bookmark", newBookmark.toJson());
print('Bookmark has been Added $res');
return res;
}
Future<List<Bookmark>> getAllBookmark() async {
final db = await database;
var res = await db.query('bookmark');
List<Bookmark> list = res.isNotEmpty
? res.map((json) => Bookmark.fromJson(json)).toList()
: [];
return list;
}
}
My Code Fetch Data
return Container(
child: FutureBuilder(
future: DBProvider.db.getAllBookmark(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting:
case ConnectionState.active:
return LoadingIndicator();
break;
default:
if (!snapshot.hasData) {
return DataNotFound(
iconData: Icons.sentiment_dissatisfied,
sizeIcon: 100,
);
} else {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
return Text('${snapshot.data[index].gambarBerita}');
},
);
}
}
},
),
);

Signing out of flutter app with sqflite database

I am trying to write the code for signing out of a flutter app logging in (The database of the map is made with sqflite).
However I am getting the following error message:
flutter: NoSuchMethodError: The method 'notify' was called on null.
Receiver: null
Tried calling: notify(Instance of 'AuthState')
Have provided code of required files below.
I am trying to incorporate a sign out function in the home_screen.dart file but I feel I am missing a link between the auth.dart, auth_provider.dart, login_screen.dart and home_screen.dart files. The codes of the required files for the issue are as follows:
File: database_helper.dart
import 'dart:async';
import 'dart:io' as io;
import 'package:path/path.dart';
import 'package:better_login/user.dart';
import 'package:sqflite/sqflite.dart' ;
import 'package:path_provider/path_provider.dart';
class DatabaseHelper {
static final DatabaseHelper _instance = new
DatabaseHelper.internal();
factory DatabaseHelper() => _instance;
static Database _db;
Future<Database> get db async {
if(_db != null)
return _db;
_db = await initDb();
return _db;
}
DatabaseHelper.internal();
initDb() async {
io.Directory documentsDirectory = await
getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, "main.db");
var theDb = await openDatabase(path, version: 1, onCreate:
_onCreate);
return theDb;
}
void _onCreate(Database db, int version) async {
// When creating the db, create the table
await db.execute(
"CREATE TABLE User(username TEXT,password TEXT)");
print("Created tables");
}
Future<int> saveUser(User user) async {
var dbClient = await db;
int res = await dbClient.insert("User", user.toMap());
return res;
}
Future<int> deleteUsers() async {
var dbClient = await db;
int res = await dbClient.delete("User");
return res;
}
Future<bool> isLoggedIn() async {
var dbClient = await db;
var res = await dbClient.query("User");
return res.length > 0? true: false;
}
}
File:auth.dart
import 'package:better_login/database_helper.dart';
enum AuthState{ LOGGED_IN, LOGGED_OUT }
abstract class AuthStateListener {
void onAuthStateChanged(AuthState state);
}
class AuthStateProvider {
static final AuthStateProvider _instance = new
AuthStateProvider.internal();
List<AuthStateListener> _subscribers;
factory AuthStateProvider() => _instance;
AuthStateProvider.internal() {
_subscribers = new List<AuthStateListener>();
initState();
}
void initState() async {
var db = new DatabaseHelper();
var isLoggedIn = await db.isLoggedIn();
if(isLoggedIn)
notify(AuthState.LOGGED_IN);
else
notify(AuthState.LOGGED_OUT);
}
void subscribe(AuthStateListener listener) {
_subscribers.add(listener);
}
void dispose(AuthStateListener listener) {
for(var l in _subscribers) {
if(l == listener)
_subscribers.remove(l);
}
}
void notify(AuthState state) {
_subscribers.forEach((AuthStateListener s) =>
s.onAuthStateChanged(state));
}
}
File: auth_provider.dart
import 'package:flutter/material.dart';
import 'package:better_login/auth.dart';
class AuthProvider extends InheritedWidget {
const AuthProvider({Key key, Widget child, this.auth}) : super(key: key, child: child);
final AuthStateListener auth;
#override
bool updateShouldNotify(InheritedWidget oldWidget) => true;
static AuthProvider of(BuildContext context) {
return context.inheritFromWidgetOfExactType(AuthProvider);
}
}
File: home_screen.dart
import 'package:flutter/material.dart';
import 'package:better_login/auth.dart';
import 'package:better_login/login_screen.dart';
import 'package:better_login/login_screen_presenter.dart';
import 'package:better_login/auth_provider.dart';
class HomeScreen extends StatelessWidget {
HomeScreen({this.authStateListener, this.authStateProvider});
final AuthStateListener authStateListener;
final AuthStateProvider authStateProvider;
void _signOut() async {
try{
authStateProvider.notify(AuthState.LOGGED_OUT);
authStateListener.onAuthStateChanged(AuthState.LOGGED_OUT);
}catch(e){
print(e);
}
}
#override
Widget build(BuildContext context) {
// TODO: implement build
return new Scaffold(
appBar: new AppBar(
title: new Text("Home"),
actions: <Widget>[
new IconButton(icon: new Icon(Icons.exit_to_app), onPressed: (){_signOut();}),
],
),
body: new Center(
child: new Text("Welcome home!"),
),
);
}
}

Execute if database exists flutter

So basically what i want to do is:
When the user turns on the app for the first time,
The SQLite database is created and the data is fetched from the internet.
Until this is done, SetupPage() widget is displayed in scaffolds body or else Home() is displayed.
Now the code i wrote works perfectly for the first time, but when i open it the second time,
The SetupPage() only shows, it never goes back to the Home(). What am i doing wrong here ?
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:http/http.dart';
import 'package:path/path.dart';
import 'pages/home.dart';
import 'pages/SetUpPage.dart';
import 'package:sqflite/sqflite.dart';
class App extends StatefulWidget {
createState() {
return AppState();
}
}
class AppState extends State<App> {
final bgColor = const Color(0xFF1abc9c);
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
bool status = false;
Database database;
#override
void initState() {
initializeData();
super.initState();
}
void initializeData() async
{
var databasesPath = await getDatabasesPath();
String path = join(databasesPath, 'demo.db');
status = false;
Database database = await openDatabase(path, version: 1,
onCreate: (Database db, int version) async {
await db.execute(
'CREATE TABLE news (id INTEGER PRIMARY KEY, topic TEXT, img TEXT, newstitle TEXT, news TEXT, newslink TEXT)');
fetchData();
}
);
database.close();
}
#override
Widget build(context) {
return Scaffold(
key: _scaffoldKey,
appBar: AppBar(
title: Text("UnFound News"),
backgroundColor: bgColor,
),
body: status ? Home() : SetUpPage(),
);
}
/*Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondRoute()),
);*/
void fetchData() async {
var result = await get("https://api.myjson.com/bins/a0bvu");
var arr = json.decode(result.body)['post'];
for(int i = 0; i < arr.length; i++)
{
//TODO: Implement addition to database.
}
setState(() {
status = true;
});
}
}
You probably want to put the fetchData in a onOpen parameter instead of the onCreate parameter like this:
Database database = await openDatabase(path, version: 1,
onCreate: (Database db, int version) async {
await db.execute(
'CREATE TABLE news (id INTEGER PRIMARY KEY, topic TEXT, img TEXT, newstitle TEXT, news TEXT, newslink TEXT)');
await fetchData();
}, onOpen: (Database db) async {
setState(() {
status = true;
});
});
static Database _db;
Future<Database> get db async {
if(_db != null)
return _db;
_db = await initDb();
return _db;
}
//Creating a database with name test.dn in your directory
initDb() async {
io.Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, "test.db");
var theDb = await openDatabase(path, version: 1, onCreate: _onCreate);
return theDb;
}
checkDb(){
//do operation
var dbClient = await db;
}