react-native-system-setting
react-native-system-setting copied to clipboard
`new NativeEventEmitter()` was called with a non-null argument without the required `addListener` method.
Hi
I am using this package to get the WIFI state.
When I am running the App I am getting this error -
new NativeEventEmitter()
was called with a non-null argument without the required addListener
method.
new NativeEventEmitter()
was called with a non-null argument without the required addListener
method.
I have all the installation process and the coing standard as per the document.
Please help me to get rid of that.
Same issue here
I have this message before but I don't see it again after I change my code to
` // utils.js import { locationAction } from '../slices/locationSlice';
export const listenLocationChange = async (SystemSetting, dispatch) => { return await SystemSetting.addLocationListener((enable) => { dispatch(locationAction.setIsLocationEnabled(enable)); }); };
// Components.js useEffect(() => { const locationListener = listenLocationChange(SystemSetting, dispatch); return () => { if (typeof locationListener?.remove === 'function') SystemSetting.removeListener(locationListener); }; }, []); `
I hope you already solve your problem
I worked on this recently and had similar issue. Here's the same comment as above with proper indentation and syntax highlighting.
// utils.js
import { locationAction } from '../slices/locationSlice';
export const listenLocationChange = async (SystemSetting, dispatch) => {
return await SystemSetting.addLocationListener((enable) => {
dispatch(locationAction.setIsLocationEnabled(enable));
});
};
// Components.js
useEffect(() => {
const locationListener = listenLocationChange(SystemSetting, dispatch);
return () => {
if (typeof locationListener?.remove === 'function')
SystemSetting.removeListener(locationListener);
};
}, []);
In other word, the fix is to actually add and remove the listeners using SystemSetting.addLocationListener
and SystemSetting.removeListener
.
Depending on the implementation, these changes could actually be directly in the same component.
Edit 2023-09-19: The library's code could actually be updated according to the following stack overflow post: https://stackoverflow.com/questions/69538962/new-nativeeventemitter-was-called-with-a-non-null-argument-without-the-requir
I added the functions to SystemSetting.java
and exposed them in SystemSetting.d.ts
and it looks like it worked so this could probably be a better solution.
diff --git a/SystemSetting.d.ts b/SystemSetting.d.ts
index 403808c..ab96e4d 100644
--- a/SystemSetting.d.ts
+++ b/SystemSetting.d.ts
@@ -31,6 +31,8 @@ interface VolumeData {
}
interface SystemSetting {
+ addListener: (eventName: string) => void;
+ removeListeners: (count: number) => void;
getBrightness: () => Promise<number>;
setBrightness: (val: number) => Promise<boolean>;
setBrightnessForce: (val: number) => Promise<boolean>;
diff --git a/android/src/main/java/com/ninty/system/setting/SystemSetting.java b/android/src/main/java/com/ninty/system/setting/SystemSetting.java
index 8ade5e6..c5d3adc 100644
--- a/android/src/main/java/com/ninty/system/setting/SystemSetting.java
+++ b/android/src/main/java/com/ninty/system/setting/SystemSetting.java
@@ -199,6 +199,25 @@ public class SystemSetting extends ReactContextBaseJavaModule implements Activit
return SystemSetting.class.getSimpleName();
}
+ /**
+ *
+ * @see https://stackoverflow.com/questions/69538962/new-nativeeventemitter-was-called-with-a-non-null-argument-without-the-requir
+ * @param eventName the name of the event
+ */
+ @ReactMethod
+ public void addListener(String eventName) {
+
+ }
+
+ /**
+ * @see https://stackoverflow.com/questions/69538962/new-nativeeventemitter-was-called-with-a-non-null-argument-without-the-requir
+ * @param count the number of listeners to remove
+ */
+ @ReactMethod
+ public void removeListeners(Integer count) {
+
+ }
+
@ReactMethod
public void setScreenMode(int mode, Promise promise) {
mode = mode == Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL ? mode : Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;