Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why AsyncStorage getItem is returning null?

export const USER_KEY = "isLoggedIn";

export const phoneVerified = () => AsyncStorage.setItem(USER_KEY, 1);
export const userInfoVerified = () => AsyncStorage.setItem(USER_KEY, 2);

I have used the above functions to store the value and the below one to get the value.

export const isSignedIn = () => {
  return new Promise((resolve, reject) => {
    AsyncStorage.getItem(USER_KEY)
      .then(res => {
          console.log("from isSignedIn : "+res); //res is showing null.
        if (res !== null) {
          resolve(res);
        } else {
          resolve(0);
        }
      })
      .catch(err => reject(err));
  });
};

Why this always returns null? I was trying async/await but still getting null. I think somehow the data is not storing.

like image 864
A. K. M. Nazmul Hassan Avatar asked Feb 16 '18 06:02

A. K. M. Nazmul Hassan


People also ask

What does AsyncStorage getItem return?

getItem ​ Gets a string value for given key . This function can either return a string value for existing key or return null otherwise. In order to store object value, you need to deserialize it, e.g. using JSON. parse() .

Is AsyncStorage deprecated?

Deprecated. Use one of the community packages instead. AsyncStorage is an unencrypted, asynchronous, persistent, key-value storage system that is global to the app. It should be used instead of LocalStorage.

How do you save AsyncStorage data?

The setItem method saves data to the AsyncStorage and allows a key and a value. Here, the key is a string that the data or value is assigned to: // React Native component const value = { name: "Chimezie", job: "Software Developer" }; const storeUser = async () => { try { await AsyncStorage. setItem("user", JSON.


1 Answers

I'm afraid you can only store strings. Please refer to this React Native AsyncStorage storing values other than strings and this https://facebook.github.io/react-native/docs/asyncstorage.html#setitem

Thanks.

like image 106
Vishu Bhardwaj Avatar answered Oct 13 '22 05:10

Vishu Bhardwaj