import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:flutter_passvault/constant/constantfile.dart'; import 'package:flutter_passvault/modelclasses/share_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 ShareCredentialProvider extends ChangeNotifier { final dio = Dio(); ShareCredentialData? _serverResponse; ShareCredentialData? get message => _serverResponse; Future postsharecredential() async { final Box hivebox = Hive.box('credentialListBox'); var allRecords = hivebox.values.toList(); var unsyncedRecords = allRecords.where((record) => record.isOfflineshare == true).toList(); print("Stored_shared_unsyncedRecords : $unsyncedRecords"); for (var record in unsyncedRecords) { print("shareddd_data_value: ${record.name}"); print("shareddd_data_value: ${record.sharedUserIds}"); print("shareddd_data_value: ${record.uid}"); print("shareddd_data_value_: ${record.isOfflineshare}"); print("user_id: ${record.sharedUserIds.length}"); String key1 = 'users[]'; List> keyValuePairs = []; for (int i = 0; i < record.sharedUserIds.length; i++) { int value = record.sharedUserIds[i]; print("keyShared_value_is: $value"); keyValuePairs.add(MapEntry(key1, value)); } print("My_map_listdata_is $keyValuePairs"); String? token = await CustomSharedPreferences.getCustomValue(); final queryParameters = {}; for (var entry in keyValuePairs) { queryParameters[entry.key] = entry.value; print("entryyy_valueee: ${entry.value}"); } dio.options.headers['Authorization'] = 'Bearer $token'; print("key_values: $keyValuePairs"); print("record.sharedUserIds: ${record.sharedUserIds}"); List userlistid = []; userlistid.addAll(record.sharedUserIds); userlistid.add(record.createdBy); print("record.userlist: $userlistid"); print( "Shareeee_is: ${keyValuePairs.map((users) => 'users[]=$users').join('&')}"); final url = '${ApiConstants.newKonectarurl}/creds/share/${record.uid}?${userlistid.map((users) => 'users[]=$users').join('&')}'; print("urllll_is: $url"); print("url_is: $url"); try { final response = await dio.post(url); if (response.statusCode == 200) { record.issync = true; record.isOfflineshare = false; await hivebox.put(record.id, record); print('share data successful'); print(response.data); print(response.data['message']); print(response.data['data']); _serverResponse = ShareCredentialData.fromJson(response.data); notifyListeners(); } else { record.issync = false; record.isOfflineshare = true; await hivebox.put(record.id, record); print('Failed : ${response.statusCode}'); } } catch (error) { print('Failed : $error'); } } } //} String listToString(List inputList) { return inputList.join(', '); } }