I am making a flutter app using SQLite for storage.
I have used SQflite package for using SQLite.
Here's the file in which I've written all the CRUD functions (I have included comments to indicate where is the error in the file):
import 'dart:async';
import 'package:path_provider/path_provider.dart';
import 'package:sqflite/sqflite.dart';
import 'Employee.dart';
class DBmanager {
late Database _database;
Future openDb() async {
if (_database == null) {
_database = await openDatabase(
join(await getDatabasesPath(), 'medicine.db'),//getting an error in this line for the join keyword
version: 1, onCreate: (Database db, int version) async {
await db.execute(
'CREATE TABLE Employee(id INTEGER PRIMARY KEY autoincrement, medicineName TEXT, Frequency TEXT, dosageAmount TEXT, medicineType TEXT, instructions TEXT)',
);
});
}
}
//function to insert medicine in the database
Future<int> insertMedicine(Medicine medicine) async {
await openDb();
return await _database.insert('Medicine', medicine.toMap());
}
//function to get information of medicine from database
Future<List<Medicine>> getMedicineList() async {
await openDb();
final List<Map<String, dynamic>> maps = await _database.query('Medicine');
return List.generate(maps.length, (i) {
return Medicine(
id: maps[i]['id'],//getting error in this line for 'id'
medicineName: maps[i]['Medicine name'],
frequency: maps[i]['Frequency'],
dosageAmount: maps[i]['Dosage Amount'],
medicineType: maps[i]['Medicine Type'],
instructions: maps[i]['Instructions'],
);
});
}
//function to update the information of medicine in the database
Future<int> updateMedicine(Medicine medicine) async {
await openDb();
return await _database.update('Medicine', medicine.toMap(),
where: "id = ?", whereArgs: [medicine.id]);
}
//function to delete medicine from database
Future<void> deleteMedicine(int id) async {
await openDb();
await _database.delete('Medicine', where: "id = ?", whereArgs: [id]);
}
}
In the above code I am getting the follwoing errors :- Line 12 : The method 'join' isn't defined for the type 'DBmanager'. Try correcting the name to the name of an existing method or defining a method named 'join'.
Line 37 : The named parameter 'id' isn't defined. Try correcting the nameto an existing named parameter's name, or defining a named parameter with the name 'id'.
Here's the file in which I've created the medicine class:-
import 'package:flutter/material.dart';
class Medicine {
int id;
String medicineName;
String frequency;
String dosageAmount;
String medicineType;
String instructions;
Medicine({
this.id,//getting an error in this line for id
required this.medicineName,
required this.frequency,
required this.dosageAmount,
required this.medicineType,
required this.instructions,
});
Map<String, dynamic> toMap() {
return {
'Medicine name': medicineName,
'Frequency': frequency,
'dosage Amount': dosageAmount,
'Medicine Type': medicineType,
'Instructions': instructions
};
}
}
In the above code I am getting the follwoing errors :- Line 24 : The parameter 'id' can't have a value of 'null' because of its type, but the implicit default value is 'null'. Try adding either an explicit non-'null' default value or the 'required' modifier.
I can't use 'required' modifier because the value of id will not be given by the user, it'll be auto-incremented.
I also tried initializing the 'id' but that didn't resolve the error.
Please help!
You need to import path
to get join
method.
import 'package:path/path.dart';