I am trying to use the react-native-contacts
library to fetch contacts in my React Native app. However, even after granting the required permission, I encounter the following error:
Permission: granted
(NOBRIDGE) LOG [TypeError: Cannot read property 'getAll' of null]
0.76.4
react-native-contacts
(^8.0.4
)Here is my code:
ContactsScreen.tsx:
import React, { useEffect } from 'react';
import Contacts from 'react-native-contacts';
import { View, Text, StyleSheet, PermissionsAndroid } from 'react-native';
const ContactsScreen: React.FC = () => {
const contacts = () => {
PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.READ_CONTACTS, {
title: 'Contacts',
message: 'This app would like to view your contacts.',
buttonPositive: 'Please accept bare mortal',
})
.then((res) => {
console.log('Permission: ', res);
Contacts.getAll()
.then((contacts) => {
console.log(contacts);
})
.catch((e) => {
console.log(e);
});
})
.catch((error) => {
console.error('Permission error: ', error);
});
}
useEffect(() => {
contacts();
}, []);
return (
<View style={styles.container}>
<Text>Contacts</Text>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 16,
backgroundColor: '#f9f9f9',
},
});
export default ContactsScreen;
AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
npx react-native run-android
.react-native-contacts
documentation to confirm API usage.AndroidManifest.xml
.The app should fetch and display contacts after permissions are granted.
Even though the app logs that the permission is granted, it throws the following error:
TypeError: Cannot read property 'getAll' of null
react-native-contacts
as per the documentation.Question:
Why is the Contacts
module returning null
for the getAll
method? Is there a step I am missing during the setup or configuration? I have to just get access to all contacts, then I will find way to render them in stylish format. Just need to get contacts first.
Any answer is appreciated, I am spending almost 2-3 days no result.
Thanks all.
After completely rebooting my Windows and starting fresh, I was finally able to resolve the issue I was facing. It turns out that the problem stemmed from the folder structure — specifically, my user name contained a space, which caused issues during the Android Studio installation process. After restoring my system, a new user profile was created without any spaces in the name, which allowed me to successfully follow the React Native CLI documentation for setting up Android Studio.
Everything is now working perfectly, and I can access my contacts with ease. If you're running into a similar issue, I highly recommend trying this solution! Simply restore your system or create a new user without spaces in the username, and you should be good to go. It worked wonders for me, and I hope it helps you too!