Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON value '<null>' of type NSNull cannot be converted to NSString

I am getting the following error screen sometimes when I reload my React Native app in iOS Simulator.

enter image description here

Sometimes it doesn't happen at all but sometimes a few times during the day.

I am using SignalR client in my app as well as Firebase Messaging.

I have tried to put a try catch blocks to all of my app including where I use AsyncStorage but this is still happening.

Very frustrating.

like image 943
Ali Avatar asked Nov 30 '18 02:11

Ali


2 Answers

my problem was with source of the images that i had in my component. i had tell the image components to pass null as source while response of the server for the image was empty or null. means:

<Image source={{uri: null}} style={styles.myImageStyle}/>

it was ok with android but in ios i ran into this error. so i changed my code to following and it's working fine now.

const imageUri = myImageUri!=null ? myImageUri : ""

<Image source={imageUri.length!=0?{uri: imageuri}: null} style={styles.myImageStyle}/>
like image 114
Mahdieh Shavandi Avatar answered Sep 17 '22 20:09

Mahdieh Shavandi


I ran into this when using AsyncStorage. It can only store strings, so storing anything else won't work, like objects etc. Below is how I set and get an array of blocked users.

When you set your item, stringify it:

AsyncStorage.setItem('blocked_users', JSON.stringify(array));

Then when you read it from async storage, use a promise and json parse the data:

AsyncStorage.getItem("blocked_users")
        .then((value) => {
             if(value){
               blocksArray = JSON.parse(value);
               this.setState({ blocked_users: blocksArray});
             };
        });
like image 37
mediaguru Avatar answered Sep 19 '22 20:09

mediaguru