With null safety enabled, I'm using the new dart version <2.18.1>.
this is my code
import 'dart:async';
import 'dart:io';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sqflite/sqflite.dart';
import 'package:untitled3/product.dart';
class ProductDBHelper{
static final _databaseName = 'mydb.db';
static final _databaseVersion = 1;
static final _table_products = 'products';
static String? path;
ProductDBHelper._privateContructor();
static final ProductDBHelper instance = ProductDBHelper._privateContructor();
static Database? _database;
///////////// Check whether the database created or not ...........
Future get database async{
if(_database != null) return _database;
_database = await _initDatabase();
return _database;
}
//////////////////////////Inittalize database with lile path, db name .............
_initDatabase() async{
Directory documentDirectory = await getApplicationDocumentsDirectory();
///////// Localstorage path/databasename.db
String path = join(documentDirectory.path , _databaseName);
return await openDatabase(
path,
version: _databaseVersion,
onCreate: _onCreate);
}
///////////////////// On Create for creating datadase ........................
FutureOr _onCreate(Database db, int version) async{
await db.execute('CREATE TABLE $_table_products(id INTEGER PRIMARY KEY autoincrement, name TExt , price TEXT, quantity INTEGER)');
}
static Future getfileData() async
{
return getDatabasesPath().then((value)
{
return path = value;
}
);
}
Future insertProduct(Product product) async {
Database db = await instance.database;
return await db.insert( _table_products, Product.toMap(product) , conflictAlgorithm: ConflictAlgorithm.ignore);
}
Future<List<Product>> getProductList() async{
Database db = await instance.database;
List<Map> maps = await db.query(_table_products);
print(maps);
return Product.fromMapList(maps);
}
Future<Product> updateProduct(Product product) async{
DatabaseException db = await instance.database;
await db.update(_table_products, Product.toMap(product) , where: 'id = ?'
, whereArgs: [product.id]);
return product;
}
Future deleteProduct(Product product) async {
DatabaseException db = await instance.database;
var deletedProduct = await db.delete(_table_products, where: 'id = ?'
, whereArgs: [product.id]);
return product;
}
}
I'm getting these errors:
The method 'update' isn't defined for the type 'DatabaseException'.
The method 'delete' isn't defined for the type 'DatabaseException'.
and this error in the Run log:
lib/product_db_helper.dart:76:14: Error: The method 'update' isn't defined for the class 'DatabaseException'.
- 'DatabaseException' is from 'package:sqflite_common/src/exception.dart' ('../../Documents/flutter/Flutter/flutter_windows_3.3.2-stable/flutter/.pub-cache/hosted/pub.dartlang.org/sqflite_common-2.3.0/lib/src/exception.dart'). Try correcting the name to the name of an existing method, or defining a method named 'update'. await db.update(_table_products, Product.toMap(product) , where: 'id = ?' ^^^^^^ lib/product_db_helper.dart:84:35: Error: The method 'delete' isn't defined for the class 'DatabaseException'.
- 'DatabaseException' is from 'package:sqflite_common/src/exception.dart' ('../../Documents/flutter/Flutter/flutter_windows_3.3.2-stable/flutter/.pub-cache/hosted/pub.dartlang.org/sqflite_common-2.3.0/lib/src/exception.dart'). Try correcting the name to the name of an existing method, or defining a method named 'delete'. var deletedProduct = await db.delete(_table_products, where: 'id = ?' ^^^^^^
Future<Product> updateProduct(Product product) async{
DatabaseException db = await instance.database;
await db.//update//(_table_products, Product.toMap(product) , where: 'id = ?'
, whereArgs: [product.id]);
return product;
}
Future deleteProduct(Product product) async {
DatabaseException db = await instance.database;
var deletedProduct = await db.//delete//(_table_products, where: 'id = ?'
, whereArgs: [product.id]);
return product;
}
This is where the errors appear. I put // where the red underlines are. Please help me.
CodePudding user response:
in your updateProduct function,
Change This
DatabaseException db = await instance.database;
to this
Database db = await instance.database;