react-native-network-info 'null is not an object'

1.2k Views Asked by At

It does not matter which function of the react-native-network-info I use, I always get a warning ([Unhandled promise rejection: TypeError: null is not an object (evaluating 'RNNetworkInfo.getGatewayIPAddress')]) and the function does not return anything. See code sample. I also already tried to do it exactly as in the documentation (https://www.npmjs.com/package/react-native-network-info):

// Get Default Gateway IP
NetworkInfo.getGatewayIPAddress().then(defaultGateway => {
  console.log(defaultGateway);
});
import { NetworkInfo } from "react-native-network-info";

 _updateStates = () => {
    ...
    ...

    NetworkInfo.getGatewayIPAddress((gateway) => {
      console.log(gateway);
    });
  };
2

There are 2 best solutions below

2
On BEST ANSWER

It seems , autolinking does not work properly for this library, i had to follow the following steps to make it work , enter image description here

It's noted in the documentation of the library for manual setup. But don't follow the 3rd step , otherwise your ios project won't build. I tried the following method from the library ,

NetworkInfo.getIPAddress().then((ipAddress) => {
      console.log(ipAddress);
    });

and it worked.

1
On

You do not have added "then" in your code .Try this

_updateStates = () => {
    ...
    ...

    NetworkInfo.getGatewayIPAddress().then(gateway => {
     console.log(gateway);
    });
  };