I require the functionality to insert a customer into SQFlite, ensuring synchronization with the server in both offline and online scenarios. The process entails initially inserting the data into SQFlite; upon synchronization with the server, the inserted data is then synchronized with the server. Following synchronization, the respective IDs in the table are updated with the server-assigned IDs
i will share my code
// customer bloc
FutureOr<void> _createCustomer(
AddCustomerEvent event,
Emitter<CustomerState> emit,
) async {
try {
emit(state.copyWith(addCustomerStatus: StatusLoading()));
final res = await _customerRepository.addCustomer(
param: event.customer, imageFile: event.imageFile);
if (res.id != null) {
return emit(state.copyWith(addCustomerStatus: StatusSuccess()));
} else {
return emit(state.copyWith(
addCustomerStatus:
const StatusFailure('Customer Insertion Failed')));
}
} catch (e) {
emit(state.copyWith(addCustomerStatus: StatusFailure(e.toString())));
}
}
//model class
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';
part 'pm_add_customer_model.g.dart';
@JsonSerializable()
class PmAddCustomerModel extends Equatable {
final String? name;
final int? phone;
final String? street;
final String? street2;
final String? city;
final int? zip;
@JsonKey(name: 'country_id')
final int? countryId;
@JsonKey(name: 'state_id')
final int? stateId;
final String? email;
const PmAddCustomerModel({
this.name,
this.phone,
this.street,
this.street2,
this.city,
this.zip,
this.countryId,
this.stateId,
this.email,
});
factory PmAddCustomerModel.fromJson(Map<String, dynamic> json) {
return _$PmAddCustomerModelFromJson(json);
}
Map<String, dynamic> toJson() => _$PmAddCustomerModelToJson(this);
@override
List<Object?> get props {
return [
name,
phone,
street,
street2,
city,
zip,
countryId,
stateId,
email,
];
}
}