i'm new to flutter, i have created a database and with one table, and every time i'm trying to insert new data into the the table, the id of the the new entry is always null, i've tried differents method but i did not find a solution, down are my classes
final int? customerId;
final String name;
final String address;
final String city;
final String phone1;
final String phone2;
final String email;
final String code;
final DateTime createDate;
final DateTime lastUpdate;
final int serverId1;
final int serverId2;
Customer({
this.customerId,
required this.name,
required this.address,
required this.city,
required this.phone1,
required this.phone2,
required this.email,
required this.code,
required this.createDate,
required this.lastUpdate,
required this.serverId1,
required this.serverId2,
});
// Factory method to create a Customer object from a map
factory Customer.fromMap(Map<String, dynamic> map) {
return Customer(
customerId: map['c_customer_id'],
name: map['c_name'],
address: map['c_address'],
city: map['c_city'],
phone1: map['c_phone1'],
phone2: map['c_phone2'],
email: map['c_email'],
code: map['c_code'],
createDate: DateTime.parse(map['c_create_date']),
lastUpdate: DateTime.parse(map['c_last_update']),
serverId1: map['c_server_id1'],
serverId2: map['c_server_id2'],
);
}
// Convert the Customer object to a map
Map<String, dynamic> toMap() {
return {
'c_customer_id': customerId, // Include customerId field
'c_name': name,
'c_address': address,
'c_city': city,
'c_phone1': phone1,
'c_phone2': phone2,
'c_email': email,
'c_code': code,
'c_create_date': createDate.toIso8601String(),
'c_last_update': lastUpdate.toIso8601String(),
'c_server_id1': serverId1,
'c_server_id2': serverId2,
};
}
// Create a new Customer object with updated values
Customer copyWith({
required int customerId,
String? name,
String? address,
String? city,
String? phone1,
String? phone2,
String? email,
String? code,
DateTime? createDate,
DateTime? lastUpdate,
int? serverId1,
int? serverId2,
}) {
return Customer(
customerId: customerId,
name: name ?? this.name,
address: address ?? this.address,
city: city ?? this.city,
phone1: phone1 ?? this.phone1,
phone2: phone2 ?? this.phone2,
email: email ?? this.email,
code: code ?? this.code,
createDate: createDate ?? this.createDate,
lastUpdate: lastUpdate ?? this.lastUpdate,
serverId1: serverId1 ?? this.serverId1,
serverId2: serverId2 ?? this.serverId2,
);
}
}
here is the code for my database and insert operation
import 'package:path/path.dart';
import 'package:mvc_customer_app/Models/customer.dart';
Future<Database> customerDataBase() async {
return openDatabase(join(await getDatabasesPath(), "invoiceDataB.db"),
onCreate: (db, version) {
return db.execute('''
CREATE TABLE customers(
c_customer_id INTEGER PRIMARY KEY AUTOINCREMENT,
c_code TEXT,
c_name TEXT,
c_address TEXT,
c_city TEXT,
c_phone1 TEXT,
c_phone2 TEXT,
c_email TEXT,
c_server_id1 INTEGER,
c_server_id2 INTEGER,
c_create_date TEXT,
c_last_update TEXT
)
''');
}, version: 1);
}
Future<Customer> addNewCustomer({required Customer customer}) async {
final db = await customerDataBase();
final customerId = await db.insert("customers", customer.toMap());
final newCustomer = customer.copyWith(customerId: customerId);
return newCustomer;
}
here is the code for the front end for inserting new data into the database
import 'package:flutter/material.dart';
import 'package:mvc_customer_app/Models/customer.dart';
import 'package:mvc_customer_app/Provider/db_helper.dart';
class addCustomer extends StatefulWidget {
@override
_addCustomerState createState() => _addCustomerState();
}
class _addCustomerState extends State<addCustomer> {
final TextEditingController _nameController = TextEditingController();
final TextEditingController _addressController = TextEditingController();
final TextEditingController _cityController = TextEditingController();
final TextEditingController _phone1Controller = TextEditingController();
final TextEditingController _phone2Controller = TextEditingController();
final TextEditingController _emailController = TextEditingController();
final TextEditingController _serverId1Controller = TextEditingController();
final TextEditingController _serverId2Controller = TextEditingController();
bool _isSubmitting = false;
String _statusMessage = '';
static String generateRandomCode() {
const length = 8; // Set the desired length of the random code
const characters =
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
final random = Random();
final code = List.generate(
length, (_) => characters[random.nextInt(characters.length)]);
return code.join();
}
void _submitForm() async {
if (!mounted) {
return;
}
setState(() {
_isSubmitting = true;
_statusMessage = '';
});
final String name = _nameController.text;
final String address = _addressController.text;
final String city = _cityController.text;
final String phone1 = _phone1Controller.text;
final String phone2 = _phone2Controller.text;
final String email = _emailController.text;
final String serverId1 = _serverId1Controller.text;
final String serverId2 = _serverId2Controller.text;
final String code = generateRandomCode();
final Customer newCustomer = Customer(
customerId: null,
serverId1: 0,
serverId2: 0,
createDate: DateTime.now(),
lastUpdate: DateTime.now(),
code: code,
name: name,
address: address,
city: city,
phone1: phone1,
phone2: phone2,
email: email,
);
try {
await addNewCustomer(customer: newCustomer); // Insert into local database
setState(() {
_statusMessage = 'Customer added successfully';
_isSubmitting = false;
});
} catch (error) {
setState(() {
_statusMessage = 'Failed to add customer: $error';
print(error);
_isSubmitting = false;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Add Customer"),
backgroundColor: Color.fromARGB(255, 28, 44, 58),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: ListView(
children: [
TextField(
controller: _nameController,
decoration: InputDecoration(labelText: 'Name'),
),
TextField(
controller: _addressController,
decoration: InputDecoration(labelText: 'Address'),
),
TextField(
controller: _cityController,
decoration: InputDecoration(labelText: 'City'),
),
TextField(
controller: _phone1Controller,
decoration: InputDecoration(labelText: 'Phone 1'),
),
TextField(
controller: _phone2Controller,
decoration: InputDecoration(labelText: 'Phone 2'),
),
TextField(
controller: _emailController,
decoration: InputDecoration(labelText: 'Email'),
),
TextField(
controller: _serverId1Controller,
decoration: InputDecoration(labelText: 'Server Id 1'),
),
TextField(
controller: _serverId2Controller,
decoration: InputDecoration(labelText: 'Server Id 2'),
),
SizedBox(height: 16.0),
ElevatedButton(
onPressed: _isSubmitting ? null : _submitForm,
child: const Text('Submit'),
),
const SizedBox(height: 8.0),
Text(
_statusMessage,
style: const TextStyle(color: Colors.red),
),
],
),
),
);
}
}
i've tryied to change the customerId by null, by 0. even change the value in the model by i'm getting the same result. the customerId is null, i've set it as PRIMARY KEY AUTOINCREMENT but it does not increment automatically.