[英]How can i pass a HashMap to a react-native android callback?
When trying to give a callback in a custom native module a java.util.HashMap
, I get the following error : java.lang.RuntimeException: Cannot convert argument of type class java.util.HashMap
. 当尝试在自定义本机模块java.util.HashMap
进行回调时,我收到以下错误: java.lang.RuntimeException: Cannot convert argument of type class java.util.HashMap
。
I am not very clear on what kind of types can be passed to the callback. 我不太清楚可以将哪种类型的类型传递给回调。 Someone suggests on SO using a WritableNativeMap
. 有人建议在SO上使用WritableNativeMap
。 If so, is there a straightforward way to cast the HashMap
? 如果是这样,是否有一种直接的方式来转换HashMap
? Or must I iterate through all key/value pairs ? 或者我必须遍历所有键/值对?
If not, the docs mention a ReadableMap
. 如果没有,那么文档会提到一个ReadableMap
。 Would that be better ? 会更好吗?
Many thanks for your help. 非常感谢您的帮助。
Here is some code it case it helps. 这是一些有用的代码。 The method uses the Firebase Android SDK: 该方法使用Firebase Android SDK:
public void fetch(String path, final Callback callback) {
root.child(path).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
if (snapshot.exists()) {
// snapshot.getValue() is a java.util.HashMap
callback.invoke(null, snapshot.getValue()); // This triggers the error
} else {
callback.invoke(null);
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
callback.invoke(firebaseError.getMessage());
}
});
}
For key-value structures, the callback expects a WritableMap
, which means whatever data structure you're using needs to be converted to a WritableMap
. 对于键值结构,回调需要一个WritableMap
,这意味着您使用的任何数据结构都需要转换为WritableMap
。
Here is a basic implementation from Firebase's DataSnapshot
: 以下是Firebase的DataSnapshot
的基本实现:
private <Any> Any castSnapshot(DataSnapshot snapshot) {
if (snapshot.hasChildren()) {
WritableMap data = Arguments.createMap();
for (DataSnapshot child : snapshot.getChildren()) {
Any castedChild = castSnapshot(child);
switch (castedChild.getClass().getName()) {
case "java.lang.Boolean":
data.putBoolean(child.getKey(), (Boolean) castedChild);
break;
case "java.lang.Integer":
data.putInt(child.getKey(), (Integer) castedChild);
break;
case "java.lang.Double":
data.putDouble(child.getKey(), (Double) castedChild);
break;
case "java.lang.String":
data.putString(child.getKey(), (String) castedChild);
break;
case "com.facebook.react.bridge.WritableNativeMap":
data.putMap(child.getKey(), (WritableMap) castedChild);
break;
}
}
return (Any) data;
} else {
String type = snapshot.getValue().getClass().getName();
switch (type) {
case "java.lang.Boolean":
return (Any)((Boolean) snapshot.getValue());
case "java.lang.Long":
// TODO check range errors
return (Any)((Integer)(((Long) snapshot.getValue()).intValue()));
case "java.lang.Double":
return (Any)((Double) snapshot.getValue());
case "java.lang.String":
return (Any)((String) snapshot.getValue());
default:
return (Any) null;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.