如何调用dbhelper insert或update或delete


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;
}
}

我想从下面的数据库助手调用insert,它给出的错误如下没有为类型"type"定义getter"insert"。(文档(尝试导入定义"insert"的库,将名称更正为现有getter的名称,或者定义名为"insert"的getter或字段。

首先,如果您想创建一个单例类,请这样做,

class DbHelper {
static final DbHelper instance= DbHelper._();//change to this
...
}

第二,当使用singleton时,不需要创建它的实例,在main或某个根小部件中初始化该类。像这样,

void main()async{
await DbHelper.init();
...
}

第三,现在你想在哪里使用这些功能,

class ProductAdd extends StatefulWidget {
const ProductAdd({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _ ProductAddState();
}
class _ ProductAddState extends State< ProductAdd> {
// you actually don't need "mixin" just directly use it.

...
void addProduct() async {
await Dbhelper.insert();
}
}
}

关于singleton的更多信息,你如何在达特建造一个辛格尔顿?

最新更新