React Native中WritableArray添加WritableMap会报错?

作者站长头像
站长
· 阅读数 16

React Native中WritableArray添加WritableMap会报错?React Native中WritableArray添加WritableMap会报错?WritableArrayprintData定义如下

WritableNativeArray printData = new WritableNativeArray();

为什么获取WritableArray的printData.size()为0呢?而且printData.pushMap(perPrintMap)会报错如下


Method threw 'com.facebook.react.bridge.ObjectAlreadyConsumedException' exception. Cannot evaluate com.facebook.react.bridge.WritableNativeMap.toString()
回复
1个回答
avatar
test
2024-06-29

你这个问题可能是多次消费同一个WritableMap或者WritableArray的时候出现的:应该每次新建新的WritableMap或者WritableArray:

WritableMap map1 = new WritableNativeMap();
map1.putString("key", "value");

WritableMap map2 = new WritableNativeMap();
map2.putString("key", "value");

WritableArray array = new WritableNativeArray();
array.pushMap(map1); // 这里map1被"消费"
array.pushMap(map2); // 这里map2被"消费"

printData.size()为0的问题,我猜可能是因为你在给printData添加数据之前就调用了size()方法。你应该在添加数据之后再检查它的大小。

回复
likes
适合作为回答的
  • 经过验证的有效解决办法
  • 自己的经验指引,对解决问题有帮助
  • 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
  • 询问内容细节或回复楼层
  • 与题目无关的内容
  • “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容