'How to fix java.lang.Double cannot be cast to java.lang.String in React Native
I have problem regarding on my AsyncStorage.getItem. I have module which is the login, where I need to set the item inside the storage, after I get the result there is an yellow message below that I have Error: java.lang.Double cannot be cast to java.lang.String I didn't know where the error came.
I will show you guys, my function to set the item in the storage.
if(response.status == '200')
{
AsyncStorage.setItem('authorization_code',access_token);
AsyncStorage.setItem('authorization_expiration',expires_in);
AsyncStorage.setItem('authorization_type',token_type);
//let token = AsyncStorage.getItem(JSON.stringify("authorization_code"));
AsyncStorage.getItem('authorization_code', (err, result) => {
alert(result);
});
}
Solution 1:[1]
Use toString()
to force type in js side. I suppose that is expires_in
the floating point number here, so:
AsyncStorage.setItem('authorization_code',access_token);
AsyncStorage.setItem('authorization_expiration',expires_in.toString());
AsyncStorage.setItem('authorization_type',token_type);
Solution 2:[2]
This issue comes for datatype error. once you uses a asyncstorage you must store only string data types only. if you stores a int it will pass this error. So you can use .toString()
at the end of the your source code
AsyncStorage.setItem('userId', response.data.dataset[0].id.toString());
Solution 3:[3]
use .toString()
.
AsyncStorage.setItem("addressId", id.toString())
.then((success) => {
console.log("succesfully saved in asyncestorage");
})
.catch((e) => console.log(e));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | isalgueiro |
Solution 2 | Rushan Thasindu |
Solution 3 | Junaid anwar |