// THis is model>>
class CompanyProfile {
// to create name tag for database
static const String columnId = '_id';
static const String comapnyNametag = 'CompanyName';
static const String phoneNumbertag = 'PhoneNumber';
static const String addressLine1tag = 'AddressLine1';
static const String addressLine2tag = 'AddressLine2';
static const String emailaddresstag = 'EmailAddress';
static const String invPrefixtag = 'InvoicePrefix';
static const String footerTexttag = 'FooterText';
// to create new Profile or Update profile
int? id;
String? companyName;
String? addressLine1;
String? addressLine2;
String? emailAddress;
String? phoneNumber;
String? invPrefix;
String? footerText;
CompanyProfile({
this.id,
this.companyName,
this.addressLine1,
this.addressLine2,
this.emailAddress,
this.phoneNumber,
this.invPrefix,
this.footerText,
});
Map<String, dynamic> toMap() {
var map = <String, dynamic>{
comapnyNametag: companyName,
phoneNumbertag: phoneNumber,
addressLine1tag: addressLine1,
addressLine2tag: addressLine2,
emailaddresstag: emailAddress,
invPrefixtag: invPrefix,
footerTexttag: footerText,
};
if (id != null) {
map[columnId] = id;
}
return map;
}
CompanyProfile.fromMap(Map<String, dynamic> map) {
id = map[columnId];
companyName = map[comapnyNametag];
phoneNumber = map[phoneNumbertag];
addressLine1 = map[addressLine1tag];
addressLine2 = map[addressLine2tag];
emailAddress = map[emailaddresstag];
invPrefix = map[invPrefixtag];
footerText = map[footerTexttag];
}
}
//this is databasae helper to create database
import 'dart:io';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sqflite/sqflite.dart';
import '../models/company_profile.dart';
class ProfileProvider {
static const _dbName = 'profile.db';
static const _dbVersion = 1;
static Database? _db;
String tableName = 'profile';
String columnId = '_id';
String comapnyNametag = 'CompanyName';
String phoneNumbertag = 'PhoneNumber';
String addressLine1tag = 'AddressLine1';
String addressLine2tag = 'AddressLine2';
String emailaddresstag = 'EmailAddress';
String invPrefixtag = 'InvoicePrefix';
String footerTexttag = 'FooterText';
ProfileProvider._privateConstructor();
static final ProfileProvider instance = ProfileProvider._privateConstructor();
Future<Database?> get database async {
if (_db != null) return _db;
_db = await _createDB();
return _db;
}
_createDB() async {
Directory dir = await getApplicationDocumentsDirectory();
String path = join(dir.path, _dbName);
return await openDatabase(
path,
version: _dbVersion,
onCreate: _onCreate,
);
}
Future _onCreate(Database db, int version) async {
await db.execute('''
create table $tableName (
$columnId integer primary key autoincrement,
$comapnyNametag text not null,
$phoneNumbertag text not null,
$addressLine1tag text not null,
$addressLine2tag text not null,
$emailaddresstag text not null,
$invPrefixtag text not null,
$footerTexttag text not null
)
''');
}
Future<CompanyProfile> insert(CompanyProfile profile) async {
final db = await database;
profile.id = await db!.insert(tableName, profile.toMap());
return profile;
}
// Future<CompanyProfile> getProfile(int id) async {
// final db = await database;
// final maps = await db.query(
// tableName,
// columns:
// )
// }
// Future<int> insert(Map<String, dynamic> row) async {
// final db = await database;
// return await db!.insert(tableName, row);
// }
Future<List<Map<String, dynamic>>> queryAllRows() async {
final db = await database;
return await db!.query(tableName);
}
// Future<List<CompanyProfile>> profiledetails() async {
// final db = await database;
// final List<Map<String, dynamic>> maps =
// db!.query(tableName) as List<Map<String, dynamic>>;
// return List.generate(maps.length, (i) {
// return CompanyProfile(
// id: maps[i][columnId],
// companyName: maps[i][comapnyNametag],
// phoneNumber: maps[i][phoneNumbertag],
// addressLine1: maps[i][addressLine1tag],
// addressLine2: maps[i][addressLine2tag],
// emailAddress: maps[i][emailaddresstag],
// invPrefix: maps[i][invPrefixtag],
// footerText: maps[i][footerTexttag],
// );
// });
// }
Future<Iterable<CompanyProfile>> getProfile() async {
final db = await database;
final List<Map<String, Object?>> queryResult = await db!.query(tableName);
return queryResult.map((e) => CompanyProfile.fromMap(e));
}
Future<int> update(Map<String, dynamic> row) async {
final db = await database;
int id = row[columnId];
return await db!
.update(tableName, row, where: '$columnId = ?', whereArgs: [id]);
}
Future<int> delete(int id) async {
final db = await database;
return await db!.delete(tableName, where: '$columnId = ?', whereArgs: [id]);
}
}
// this is database helper
Using this model I was able to save data in the database.
//From debug Console.
{_id: 1, CompanyName: a, PhoneNumber: a, AddressLine1: a, AddressLine2: a, EmailAddress: a, InvoicePrefix: a, FooterText: a}
Now I Want to get the values and show in a profile screen like this:
Company Name: a Address: addressline1 and address line 2 Phone: a Email: a Invoice Prefix: a Footer Text: a
Later I will make a pdf file using this data as well. Therefore, I need to get the values individually. Anybody can help me about this? I am searching in google and youtube but nothing works. some methods doesn't work becasue very old, and most of the methods shows how to create a list graving values from database. I don't want to create any list. I will have only one data in the whole database and i want use this everywhere in the app.
Lots of talking :p