bug fix: enhance contact fetching with permission handling
Some checks failed
/ mirror (push) Successful in 4s
/ build (push) Has been cancelled
/ build-stealth (push) Has been cancelled

This commit is contained in:
AlexisDanlos 2025-03-06 15:44:03 +01:00
parent 2ea2c679b2
commit 7ff9418e06
3 changed files with 46 additions and 19 deletions

View File

@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_contacts/flutter_contacts.dart';
import 'package:permission_handler/permission_handler.dart';
import '../../services/contact_service.dart';
class ContactState extends StatefulWidget {
@ -24,6 +25,7 @@ class _ContactStateState extends State<ContactState> {
bool _loading = true;
double _scrollOffset = 0.0;
Contact? _selfContact = Contact();
bool _permissionRequestInProgress = false;
// Getters for all contacts and favorites
List<Contact> get contacts => _allContacts;
@ -35,8 +37,26 @@ class _ContactStateState extends State<ContactState> {
@override
void initState() {
super.initState();
fetchContacts(); // Fetch all contacts by default
FlutterContacts.addListener(_onContactChange);
_initializeContacts();
}
Future<void> _initializeContacts() async {
try {
final status = await Permission.contacts.status;
if (status.isGranted) {
await fetchContacts();
} else {
final result = await Permission.contacts.request();
if (result.isGranted) {
await fetchContacts();
} else {
setState(() => _loading = false);
}
}
} catch (e) {
debugPrint('Error initializing contacts: $e');
setState(() => _loading = false);
}
}
void _onContactChange() => fetchContacts();

View File

@ -68,25 +68,24 @@ class _HistoryPageState extends State<HistoryPage>
void _toggleFavorite(Contact contact) async {
try {
if (await FlutterContacts.requestPermission()) {
Contact? fullContact = await FlutterContacts.getContact(contact.id,
withProperties: true,
withAccounts: true,
withPhoto: true,
withThumbnail: true);
Contact? fullContact = await FlutterContacts.getContact(contact.id,
withProperties: true,
withAccounts: true,
withPhoto: true,
withThumbnail: true);
if (fullContact != null) {
fullContact.isStarred = !fullContact.isStarred;
await FlutterContacts.updateContact(fullContact);
}
if (fullContact != null) {
fullContact.isStarred = !fullContact.isStarred;
await FlutterContacts.updateContact(fullContact);
await _refreshContacts();
} else {
print("Could not fetch contact details");
}
} catch (e) {
print("Error updating favorite status: $e");
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Failed to update favorite status')));
debugPrint("Error updating favorite status: $e");
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Failed to update contact favorite status')),
);
}
}
}

View File

@ -1,19 +1,27 @@
import 'package:flutter/material.dart';
import 'package:flutter_contacts/flutter_contacts.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:permission_handler/permission_handler.dart';
// Service to manage contact-related operations
class ContactService {
Future<List<Contact>> fetchContacts() async {
if (await FlutterContacts.requestPermission()) {
final hasPermission = await Permission.contacts.status;
if (!hasPermission.isGranted) {
return [];
}
try {
return await FlutterContacts.getContacts(
withProperties: true,
withThumbnail: true,
withAccounts: true,
withGroups: true,
withPhoto: true);
} catch (e) {
debugPrint('Error fetching contacts: $e');
return [];
}
return [];
}
Future<List<Contact>> fetchFavoriteContacts() async {