The value obtained from the database is not applied to the variable - flutter

I have a function to get a value from a sqlite database. How do I set this value to a variable?
Get value from DB
getPref(Setting setting) async {
var dbClient = await db;
try {
dbClient.query('settings',
where: 'name = ?',
whereArgs: [setting.name],
columns: ['value']).then((data) {
List<Map<String, dynamic>> s = data;
List<String> list = [];
for (var x in s) {
x.forEach((k, v) => list.add(v));
}
print("NAME: ${setting.name} VALUE: ${list[0]}");
return list[0];
});
} on Exception catch (ex) {
print(ex.toString());
}
}
Result:
NAME: mainPage VALUE: all
But when I put this value it doesn't apply, it throws null.
String page = "";
getPage() {
DBHelper database = DBHelper();
database.getPref(Setting(name: 'mainPage')).then((val) {
setState(() {
print("VAL " + val.toString());
page = val;
});
});
}
Unhandled Exception: type 'Null' is not a subtype of type 'String'

Try replacing:
getPref(Setting setting) async {
var dbClient = await db;
try {
dbClient.query('settings',
where: 'name = ?',
whereArgs: [setting.name],
columns: ['value']).then((data) {
List<Map<String, dynamic>> s = data;
List<String> list = [];
for (var x in s) {
x.forEach((k, v) => list.add(v));
}
print("NAME: ${setting.name} VALUE: ${list[0]}");
return list[0];
});
}
with:
getPref(Setting setting) async {
var dbClient = await db;
try {
var data = await dbClient.query('settings',
where: 'name = ?',
whereArgs: [setting.name],
columns: ['value']);
List<Map<String, dynamic>> s = data;
List<String> list = [];
for (var x in s) {
x.forEach((k, v) => list.add(v));
}
print("NAME: ${setting.name} VALUE: ${list[0]}");
return list[0];
}
What's wrong?
When querying your db you are not waiting to complete the query before returning the list[0]. In other words, you are returning your list[] before getting the data from the database as .then doesn't block the execution onwards and prematurely executes return list[0] which you don't want.

Related

Getting null value instead of input value in flutter

I am receiving a null value when I am saving a value as in my code I have three fields
String? _itemName ="";
String? _dateCreated ="";
int? _id;
and when after running my project, I insert an item by calling this method:
Future<int> saveItem(NoDoItem item) async
{
var dbClient = await database;
int res = await dbClient.insert(tableName, item.toMap());
debugPrint(res.toString());
return res;
}
Using saveItem() function:
void _hndleSubmitted(String text) async
{
_textEditingController.clear();
NoDoItem noDoItem = NoDoItem(text, DateTime.now().toIso8601String());
int savedItemId = await db.saveItem(noDoItem);
debugPrint("Item saved ID: $savedItemId");
}
and after this when I am retrieving all itemNames, I get null value instead of user entered value
Defining getItems() func:
Future<List> getItems() async
{
var dbClient = await database;
var result = await dbClient.rawQuery("SELECT * FROM $tableName ORDER BY $columnItemName ASC");
return result.toList();
}
Using getItems() function:
_readNotoDoItems() async
{
List items = await db.getItems();
items.forEach((item) {
NoDoItem noDoItem = NoDoItem.map(item);
print("Db items: ${noDoItem.itemName}");
});
}
Please tell me what is the the reason that I am getting a null value instead of entered value and how to fix this issue?
I think the problem is here
void _hndleSubmitted(String text) async {
_textEditingController.clear(); // <--------- HERE
NoDoItem noDoItem = NoDoItem(text, DateTime.now().toIso8601String());
int savedItemId = await db.saveItem(noDoItem);
debugPrint("Item saved ID: $savedItemId");
}
you clear the value with _textEditingController.clear(); right before using it. move it after adding the item to database
void _hndleSubmitted(String text) async {
NoDoItem noDoItem = NoDoItem(text, DateTime.now().toIso8601String());
int savedItemId = await db.saveItem(noDoItem);
textEditingController.clear();
debugPrint("Item saved ID: $savedItemId");
}

FLUTTER, SQFLITE ,How do I prevent deletion if related data still exists?

Hi guys I'm newbie in Flutter and currently having intern.
So I got 3 tables here, which are Item database is the main , group database, category database.
If the items databases is using the group, how should I prevent user delete group database because the group is in used in item database?
Will be great if someone can guide on me , because I'm totally new to this and because of intern I had to done it quickly..
import 'dart:async';
import 'dart:io' as io;
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path_provider/path_provider.dart';
import 'package:ezystock/model/item_model.dart';
import 'package:ezystock/model/group_model.dart';
import 'package:ezystock/model/category_model.dart';
class DBHelper {
static Database _db;
static const String DB_NAME = 'database.db';
Future<Database> get db async {
if (_db != null) {
return _db;
}
_db = await initDb();
return _db;
}
initDb() async {
io.Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, DB_NAME);
var db = await openDatabase(path, version: 1, onCreate: _onCreate);
return db;
}
_onCreate(Database db, int version) async {
await db.execute("CREATE TABLE 'item' ('id' INTEGER PRIMARY KEY, 'code' TEXT, 'description' TEXT,'cost' REAL,'price' REAL,'category' TEXT,'group' TEXT)");
await db.execute("CREATE TABLE 'group' ('id' INTEGER PRIMARY KEY, 'code' TEXT, 'description' TEXT)");
await db.execute("CREATE TABLE 'category' ('id' INTEGER PRIMARY KEY, 'code' TEXT, 'description' TEXT)");
}
//Category
Future<Category> saveCategory(Category category) async {
var dbClient = await db;
category.id = await dbClient.insert("category", category.toMap());
return category;
}
Future<List<Category>> getCategory() async {
var dbClient = await db;
List<Map> maps = await dbClient.query("category", columns: ["id", "code","description"]);
//List<Map> maps = await dbClient.rawQuery("SELECT * FROM $TABLE");
List<Category> category = [];
if (maps.length > 0) {
for (int i = 0; i < maps.length; i++) {
category.add(Category.fromMap(maps[i]));
}
}
return category;
}
Future<int> deleteCategory(int id) async {
var dbClient = await db;
return await dbClient.delete("category", where: 'id = ?', whereArgs: [id]);
}
Future<int> updateCategory(Category category) async {
var dbClient = await db;
return await dbClient.update("category", category.toMap(),
where: 'id = ?', whereArgs: [category.id]);
}
//Group
Future<Group> saveGroups(Group group) async {
var dbClient = await db;
group.id = await dbClient.insert("group", group.toMap());
return group;
}
Future<List<Group>> getGroups() async {
var dbClient = await db;
List<Map> maps = await dbClient.query("group", columns: ["id", "code","description"]);
//List<Map> maps = await dbClient.rawQuery("SELECT * FROM $TABLE");
List<Group> groups = [];
if (maps.length > 0) {
for (int i = 0; i < maps.length; i++) {
groups.add(Group.fromMap(maps[i]));
}
}
return groups;
}
Future<int> deleteGroups(int id) async {
var dbClient = await db;
List<Map> items = await dbClient.query("item", where: 'group = ?', whereArgs: [id]);
if (items.length > 0) {
return await dbClient.delete("group", where: 'id = ?', whereArgs: [id]);
} else {
}
}
Future<int> updateGroups(Group group) async {
var dbClient = await db;
return await dbClient.update("group", group.toMap(),
where: 'id = ?', whereArgs: [group.id]);
}
//items
Future<Items> saveItems(Items items) async {
var dbClient = await db;
items.id = await dbClient.insert("item", items.toMap());
return items;
}
Future<List<Items>> getItems() async {
var dbClient = await db;
List<Map> maps = await dbClient.query("item", columns: ["id", "code","description","cost","price","category","group"]);
//List<Map> maps = await dbClient.rawQuery("SELECT * FROM $TABLE");
List<Items> groups = [];
if (maps.length > 0) {
for (int i = 0; i < maps.length; i++) {
groups.add(Items.fromMap(maps[i]));
}
}
return groups;
}
Future<int> deleteItems(int id) async {
var dbClient = await db;
return await dbClient.delete("item", where: 'id = ?', whereArgs: [id]);
}
Future<int> updateItems(Items items) async {
var dbClient = await db;
return await dbClient.update("item", items.toMap(),
where: 'id = ?', whereArgs: [items.id]);
}
Future close() async {
var dbClient = await db;
dbClient.close();
}
}
The answer is: dont provide any method to delete group table.
So remove this code:
Future<int> deleteGroups(int id) async {
var dbClient = await db;
List<Map> items = await dbClient.query("item", where: 'group = ?', whereArgs: [id]);
if (items.length > 0) {
return await dbClient.delete("group", where: 'id = ?', whereArgs: [id]);
} else {
}

Flutter: value of type 'Future<List<UserVideo>>' can't be assigned to a variable of type 'List<UserVideo>'

I am trying to use one List (custom type) but getting error.
When i try to use the getData() function. Like below.
List<UserVideo> videoDataList = [];
videoDataList = UserVideo.getData();
This is initState method.
#override
void initState() {
videoDataList = await UserVideo.getData();
WidgetsBinding.instance.addObserver(this);
_videoListController.init(
_pageController,
videoDataList,
);
super.initState();
}
I am getting the error.
A value of type 'Future<List<UserVideo>>' can't be assigned to a variable of type 'List<UserVideo>'.
Try changing the type of the variable, or casting the right-hand type to 'List<UserVideo>'.
Here is the code for function.
class UserVideo {
final String url;
final String image;
final String desc;
UserVideo({
this.url: mockVideo,
this.image: mockImage,
this.desc,
});
Future <List<UserVideo>> getData() async {
List<UserVideo> list = [];
try {
var deviceid = '123';
var dtgUid = '100';
var nodata;
var bodyss = {
"uid": dtgUid,
"deviceid": deviceid,
};
var url = 'http://192.168.100.4:8080/videos/get-data.php';
// Starting Web API Call.
var response = await http
.post(url, body: json.encode(bodyss))
.timeout(Duration(seconds: 5), onTimeout: () {
return null;
});
if (response.statusCode == 200) {
final data = StreamingFromJson(response.body);
if (data.count == null) {
count = 0;
} else {
count = data.count;
}
if (data.content.length > 0 && data.content[0].name != 'Empty') {
for (var i in data.content) {
list.add(UserVideo(image: i.thumbnail, url: i.video, desc: i.title));
}
} else {
nodata = 'No Record Found';
}
print(list.length);
}
} catch (e) {
print("Exception Caught: $e");
}
return list;
}
Edit:
Just showing the hardcoded value which is working fine.
static List<UserVideo> fetchVideo() {
List<UserVideo> list = [];
list.add(UserVideo(image: '', url: mockVideo, desc: 'Test1'));
list.add(UserVideo(image: '', url: mV2, desc: 'MV_TEST_2'));
list.add(UserVideo(image: '', url: mV3, desc: 'MV_TEST_3'));
list.add(UserVideo(image: '', url: mV4, desc: 'MV_TEST_4'));
return list;
}
I can use it like this and no error.
videoDataList = UserVideo.fetchVideo();
Your method getData() returns a Future:
Future<List<UserVideo>> getData() async {
List<UserVideo> list = [];
try {
var deviceid = '123';
var dtgUid = '100';
var nodata;
var bodyss = {
"uid": dtgUid,
"deviceid": deviceid,
};
You have to use async/await to call the method getData():
List<UserVideo> videoDataList = [];
videoDataList = await UserVideo.getData();
or use then():
List<UserVideo> videoDataList = [];
UserVideo.getData().then((list){
videoDataList = list;
});
Note: To use await you need to declare a method async

Unable to display ListView from SqFLite

My data is able to upload to the database without any error, however i cant seem to display my listview with the data.
As you can see from the _submit() function, if theres an error, snackbar will be shown indicating theres an error and will not proceed to the mainpage, however, the result shows a snackbar with a success message,
So Im suspecting it has to do with my listview code, or my databasehelper as I may have missed out something in the code.
Any help is deeply appreciated!
Heres my listview code:
FutureBuilder<List<Note>>(
future: _databaseHelper.getNoteList(),
builder: (BuildContext context, AsyncSnapshot<List<Note>> snapshot){
if(snapshot.hasData){
return ListView.builder(
itemCount: _count,
itemBuilder: (BuildContext context, int position) {
Note note = snapshot.data[position];
return Card(
color: Colors.white,
elevation: 2.0,
child: new ListTile(
title: new Text(note.title),
subtitle: new Text(note.bodyText),
onTap: () =>
_navigateToEditAddPage(note, 'Edit a Note'),
onLongPress: () => _showDeleteDialog(note),
),
);
});
}else{
return Container(width: 0,height: 0,);
}
},),
Heres my insertData code:
void _submit() async {
if (_formKey.currentState.validate()) {
note.title = _titleController.text;
note.bodyText = _bodyTextController.text;
note.date = _dateController.text;
if (note.id == null) {
int result = await _databaseHelper.insertData(note);
if (result != 0) {
_moveToHomePage('Note successfully added');
} else {
_showSnackBar('Note unable to be inserted due to some error');
}
} else {
int result = await _databaseHelper.updateData(note);
if (result != 0) {
_moveToHomePage('Note successfully updated');
} else {
_showSnackBar('Note unable to be updated due to some error');
}
}
}
}
Heres my DatabaseHelper code:
class DatabaseHelper {
static Database _database;
String dataTable = 'NoteTable';
String colId = 'id';
String colTitle = 'title';
String colBody = 'bodyText';
String colDate = 'date';
DatabaseHelper._();
static final DatabaseHelper db = DatabaseHelper._();
Future<Database> get database async {
if (_database == null) {
_database = await initializeDatabase();
}
return _database;
}
Future<Database> initializeDatabase() async {
Directory directory = await getApplicationDocumentsDirectory();
String path = directory.path + 'notes.db';
var notesDatabase =
await openDatabase(path, version: 1, onCreate: _createDb);
return notesDatabase;
}
void _createDb(Database database, int newVersion) async {
await database.execute("CREATE TABLE $dataTable ("
"$colId INTEGER PRIMARY KEY AUTOINCREMENT,"
"$colTitle TEXT,"
"$colBody TEXT,"
"$colDate TEXT"
")");
}
Future<List<Map<String, dynamic>>> getNoteListMap() async {
Database db = await this.database;
var result = await db.query(dataTable);
return result;
}
Future<int> insertData(Note note) async {
Database db = await this.database;
var result = await db.insert(dataTable,note.toMap(),conflictAlgorithm:
ConflictAlgorithm.replace,);
return result;
}
Future<int> updateData(Note note) async {
Database db = await this.database;
var result = await db.update(dataTable,note.toMap(),
where: 'colId = ?', whereArgs: [note.id]);
return result;
}
Future<int> deleteData(Note note) async {
Database db = await this.database;
var result = await db
.delete(dataTable, where: 'colId = ?', whereArgs: [note.id]);
return result;
}
Future<int> getCount() async{
Database db = await this.database;
List<Map<String,dynamic>> x = await db.rawQuery('SELECT COUNT (*) from $dataTable');
int result = Sqflite.firstIntValue(x);
return result;
}
Future<List<Note>> getNoteList() async {
var noteMapList = await getNoteListMap();
int count = noteMapList.length;
//list of notes, each note consist of their own independent variables
List<Note> noteList;
for (int i = 0; i < count; i++) {
noteList.add(Note.fromMapObject(noteMapList[i]));
}
return noteList;
}
}
And lastly my Note model:
class Note {
int _id;
String _date;
String _title;
String _bodyText;
Note(this._date, this._title, this._bodyText);
Note.withId(this._id, this._date, this._title, this._bodyText);
set date(String date) {
this._date = date;
}
get date => _date;
set title(String title) {
this._title = title;
}
get title => _title;
set bodyText(String bodyText){
this._bodyText = bodyText;
}
get bodyText => _bodyText;
get id => _id;
Map<String, dynamic> toMap() {
var map = new Map<String, dynamic>();
if (_id != null) {
map['id'] = _id;
}
map['title'] = _title;
map['bodyText'] = _bodyText;
map['date'] = _date;
return map;
}
//Converting a map object to a note object
Note.fromMapObject(Map<String,dynamic> fromMap){
_id = fromMap['id'];
_title = fromMap['title'];
_bodyText = fromMap['bodyText'];
_date = fromMap['date'];
}
}
I found two errors in your code.
1: in getNoteList() of DatabaseHelper
List<Note> noteList;
to
List<Note> noteList = [];
2: in listview code
itemCount: _count,
to
itemCount: snapshot.data.length,
result:

throw ArgumentError("nullColumnHack required when inserting no data");Exception in Flutter

I am working on SqFlite programme but it shows ArgumentError Exception.
My code is not working it showing I can not insert data into database.
Please some one help me with this.SQFlite Operation like CRUD is not performing.
Exception like ArgumentError Exception like occurring some default dart file with Exception Showing.
Exception throw ArgumentError("nullColumnHack required when inserting no data");
import 'dart:async';
import 'dart:io';
import 'package:sqflite/sqflite.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sqlite_app/models/note.dart';
class DatabaseHelper {
static 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) {
_databaseHelper = DatabaseHelper._createInstance();
}
return _databaseHelper;
}
Future<Database> get databse async {
if (_database == null) {
_database = await initalizeDatabase();
}
return _database;
}
Future<Database> initalizeDatabase() async {
Directory directory = await getApplicationDocumentsDirectory();
String path = directory.path + 'note.db';
var noteDatabase = await openDatabase(
path, version: 1, onCreate: _createDb);
return noteDatabase;
}
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.databse;
//var result=await db.rawQuery('SELECT * FROM $noteTable order by $colPriority ASC');
var result = await db.query(noteTable, orderBy: '$colPriority ASC');
return result;
}
Future<int> insertNote(Note note) async {
Database db = await this.databse;
var result = await db.insert(noteTable, note.toMap());
return result;
}
Future<int> updateNote(Note note) async {
var db = await this.databse;
var result = await db.update(
noteTable, note.toMap(), where: '$colId=?', whereArgs: [note.id]);
return result;
}
Future<int> deleteNote(int id) async {
var db = await this.databse;
int result = await db.rawDelete('DELETE FROM $noteTable WHERE $colId=$id');
return result;
}
Future<int> getCount() async {
Database db = await this.databse;
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;
List<Note> noteList = List<Note>();
for (int i = 0; i < count; i++) {
noteList.add(Note.fromMapObject(noteMapList[i]));
}
return noteList;
}
}
Just pass one more argument in db.insert(i.e. nullColumnHack) with value equal to the Auto Incrementing ID Integer...it will work...Do this for all other operations...
Future<int> insertNote(Note note) async {
Database db = await this.databse;
var result = await db.insert(noteTable, note.toMap(),nullColumnHack: colId);
return result;
}