bug fix: enhance contact fetching with permission handling #44
@ -1,5 +1,6 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_contacts/flutter_contacts.dart';
|
import 'package:flutter_contacts/flutter_contacts.dart';
|
||||||
|
import 'package:permission_handler/permission_handler.dart';
|
||||||
import '../../services/contact_service.dart';
|
import '../../services/contact_service.dart';
|
||||||
|
|
||||||
class ContactState extends StatefulWidget {
|
class ContactState extends StatefulWidget {
|
||||||
@ -24,6 +25,7 @@ class _ContactStateState extends State<ContactState> {
|
|||||||
bool _loading = true;
|
bool _loading = true;
|
||||||
double _scrollOffset = 0.0;
|
double _scrollOffset = 0.0;
|
||||||
Contact? _selfContact = Contact();
|
Contact? _selfContact = Contact();
|
||||||
|
bool _permissionRequestInProgress = false;
|
||||||
|
|
||||||
// Getters for all contacts and favorites
|
// Getters for all contacts and favorites
|
||||||
List<Contact> get contacts => _allContacts;
|
List<Contact> get contacts => _allContacts;
|
||||||
@ -35,8 +37,26 @@ class _ContactStateState extends State<ContactState> {
|
|||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
fetchContacts(); // Fetch all contacts by default
|
_initializeContacts();
|
||||||
FlutterContacts.addListener(_onContactChange);
|
}
|
||||||
|
|
||||||
|
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();
|
void _onContactChange() => fetchContacts();
|
||||||
|
@ -68,25 +68,24 @@ class _HistoryPageState extends State<HistoryPage>
|
|||||||
|
|
||||||
void _toggleFavorite(Contact contact) async {
|
void _toggleFavorite(Contact contact) async {
|
||||||
try {
|
try {
|
||||||
if (await FlutterContacts.requestPermission()) {
|
Contact? fullContact = await FlutterContacts.getContact(contact.id,
|
||||||
Contact? fullContact = await FlutterContacts.getContact(contact.id,
|
withProperties: true,
|
||||||
withProperties: true,
|
withAccounts: true,
|
||||||
withAccounts: true,
|
withPhoto: true,
|
||||||
withPhoto: true,
|
withThumbnail: true);
|
||||||
withThumbnail: true);
|
|
||||||
|
|
||||||
if (fullContact != null) {
|
if (fullContact != null) {
|
||||||
fullContact.isStarred = !fullContact.isStarred;
|
fullContact.isStarred = !fullContact.isStarred;
|
||||||
await FlutterContacts.updateContact(fullContact);
|
await FlutterContacts.updateContact(fullContact);
|
||||||
}
|
|
||||||
await _refreshContacts();
|
await _refreshContacts();
|
||||||
} else {
|
|
||||||
print("Could not fetch contact details");
|
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print("Error updating favorite status: $e");
|
debugPrint("Error updating favorite status: $e");
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
if (mounted) {
|
||||||
SnackBar(content: Text('Failed to update favorite status')));
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
const SnackBar(content: Text('Failed to update contact favorite status')),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,19 +1,27 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_contacts/flutter_contacts.dart';
|
import 'package:flutter_contacts/flutter_contacts.dart';
|
||||||
import 'package:qr_flutter/qr_flutter.dart';
|
import 'package:qr_flutter/qr_flutter.dart';
|
||||||
|
import 'package:permission_handler/permission_handler.dart';
|
||||||
|
|
||||||
// Service to manage contact-related operations
|
// Service to manage contact-related operations
|
||||||
class ContactService {
|
class ContactService {
|
||||||
Future<List<Contact>> fetchContacts() async {
|
Future<List<Contact>> fetchContacts() async {
|
||||||
if (await FlutterContacts.requestPermission()) {
|
final hasPermission = await Permission.contacts.status;
|
||||||
|
if (!hasPermission.isGranted) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
return await FlutterContacts.getContacts(
|
return await FlutterContacts.getContacts(
|
||||||
withProperties: true,
|
withProperties: true,
|
||||||
withThumbnail: true,
|
withThumbnail: true,
|
||||||
withAccounts: true,
|
withAccounts: true,
|
||||||
withGroups: true,
|
withGroups: true,
|
||||||
withPhoto: true);
|
withPhoto: true);
|
||||||
|
} catch (e) {
|
||||||
|
debugPrint('Error fetching contacts: $e');
|
||||||
|
return [];
|
||||||
}
|
}
|
||||||
return [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<Contact>> fetchFavoriteContacts() async {
|
Future<List<Contact>> fetchFavoriteContacts() async {
|
||||||
|
Loading…
Reference in New Issue
Block a user