import 'dart:async';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
import '../models/product.dart';
class DbHelper {
late Database ?_db;
static final DbHelper instance=DbHelper();
Future<Database> get db async{
if(_db==null){
_db=await initializedb();
}
return _db!;
}
static Future<Database> initializedb()async {
String dbPath = join(await getDatabasesPath(),"etrade.db");
var etradeDB = await openDatabase(dbPath,version: 1,onCreate: createDb);
return etradeDB;
}
void createDb(Database db, int version)async {
await db.execute("Create table products(id integer primary key, name text, description
text, unitPrice integer)");
}
Future<List<Product>> getProducts() async{
Database db = await this.db;
var result = await db.query("products");
return List.generate(result.length, (i){
return Product.fromObject(result[i]);
});
}
Future<int> insert(Product product) async {
Database db = await this.db;
var result = await db.insert("products",product.toMap());
return result;
}
Future<int> delete(int id) async {
var db = await this.db;
var result = await db.rawDelete("delete from products where id=$id");
return result;
}
Future<int> update(Product product) async {
var db = await this.db;
var result = await db.update("product", product.toMap(), where: "id=?",whereArgs:
[product.id]);
return result;
}
}
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:sqflite_demo/data/dbHelper.dart';
class ProductAdd extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return ProductAddState();
}
}
class ProductAddState extends State with DbHelper{
initState() {
myAsyncInit();
}
myAsyncInit() async {
//do async stuff
}
var Dbhelper = DbHelper;
final txtName = TextEditingController();
final txtDescription = TextEditingController();
var txtUnitPrice = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
appBar: AppBar(
title: Text("Yeni Ürün ekle"),
),
body: Padding(
padding: EdgeInsets.all(30.0),
child: Column(
children: <Widget>[
buildNameField(),
buildDescriptionField(),
buildUnitPriceField(),
buildSaveButton()
],
),
),
);
}
buildUnitPriceField() {
return TextField(
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly
],
decoration: InputDecoration(labelText: "Birim fiyatı", hintText: "20"),
controller: txtName,
);
}
buildNameField() {
return TextField(
decoration: InputDecoration(labelText: "Ürün adı", hintText: "Tohum"),
controller: txtDescription,
);
}
buildDescriptionField() {
return TextField(
decoration: const InputDecoration(
labelText: "Ürün açıklaması", hintText: "Domates"),
controller: txtUnitPrice,
);
}
buildSaveButton() {
return FlatButton(
onPressed: () {
addProduct();
},
child: const Text("Ekle"),
);
}
void addProduct() async {
await Dbhelper.insert;
}
}
I want to call insert from the db helper below, the error it gives is as follows The getter 'insert' isn't defined for the type 'Type'. (Documentation) Try importing the library that defines 'insert', correcting the name to the name of an existing getter, or defining a getter or field named 'insert'.