mobileapplicationPassvault/lib/api_provider/edit_credential_provider.dart

77 lines
2.3 KiB
Dart
Raw Permalink Normal View History

2024-04-12 05:23:32 +00:00
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_passvault/constant/constantfile.dart';
import 'package:flutter_passvault/modelclasses/edit_credential_model.dart';
import 'package:flutter_passvault/hive_storage/store_credential_model.dart';
import 'package:flutter_passvault/view_pages/shared_preferance.dart';
import 'package:hive/hive.dart';
class EditCredentialProvider extends ChangeNotifier {
final dio = Dio();
MyEditCredentialDta? _serverResponse;
MyEditCredentialDta? get editcredential => _serverResponse;
final Box<Storedcredential> hivebox =
Hive.box<Storedcredential>('credentialListBox');
Future<void> posteditcredential() async {
var allRecords = hivebox.values.toList();
var unsyncedRecords = allRecords
.where((record) =>
record.issync == false && record.isOfflineupdate == true)
.toList();
print("Stored_updateunsyncedRecords : $unsyncedRecords");
for (var record in unsyncedRecords) {
print("unsyncedRecords_uid: ${record.uid}");
final Map<String, dynamic> editdataa = {
'username': record.username,
'password': record.password,
'name': record.name,
'detail': record.detail
};
String? value = await CustomSharedPreferences.getCustomValue();
print("Edit hMy_credential: $value");
dio.options.headers['Authorization'] = 'Bearer $value';
print("uid_iss11: ${record.uid}");
try {
final response = await dio.put(
"${ApiConstants.newKonectarurl}/creds/${record.uid}",
data: editdataa);
if (response.statusCode == 200) {
print('Updated successful');
print(response.data);
print("updatedd ${response.data['data']}");
record.issync = true;
// record.uid = true;
record.isOfflineupdate = false;
await hivebox.put(record.id, record);
print("server_resposeeeeeeeeeeeeeeEdittt: $_serverResponse");
_serverResponse = MyEditCredentialDta.fromJson(response.data);
} else {
print('Failed : ${response.statusCode}');
record.issync = false;
record.isOfflineupdate = true;
await hivebox.put(record.id, record);
}
} catch (error) {
print('Failed : $error');
}
}
}
}