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()


回答:

你这个问题可能是多次消费同一个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()方法。你应该在添加数据之后再检查它的大小。

以上是 React Native中WritableArray添加WritableMap会报错? 的全部内容, 来源链接: utcz.com/p/945286.html

回到顶部