mutantkeyboard
mutantkeyboard

Reputation: 1724

React Native can't return array

Ok guys.

I'm building a small POC with BrotherSDK, and all I want to do is to return the list of label printers, and their paper sizes. I've read that in order to do this I have to use Promise.resolve function from ReactBridge.

I tried, but I get an error like this on my simulator.

simulator

My Java code looks like this:

 @ReactMethod
    public void getPrinterList(final Promise promise) {
        try {
            ArrayList<String> printerList = new ArrayList<>();
            Collections.addAll(printerList, "QL710W", "QL720NW",
                    "PTE550W",
                    "PTE500",
                    "PT750W",
                    "PTD800W",
                    "PTE800W",
                    "PTE850TKW",
                    "PTP900W",
                    "PTP960NW",
                    "QL810W",
                    "QL800",
                    "QL820NWB",
                    "PTP300BT",
                    "QL1100",
                    "QL1110NWB",
                    "QL1115NWB",
                    "PTP710BT");
            String [] retArray = new String[printerList.size()];
            retArray = printerList.toArray(retArray);
            WritableArray promiseArray = Arguments.createArray();
            for(int i=0; i < retArray.length; i++){
                promiseArray.pushString(retArray[i]);
            }
            promise.resolve(promiseArray);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @ReactMethod
    public void getPaperSizes(final Promise promise) {
        try {
            List<String> paperSizeList = new ArrayList<>();
            Collections.addAll(paperSizeList, "W17H54",
                    "W17H87",
                    "W23H23",
                    "W29H90",
                    "W29H42","W38H90",
                    "W39H48",
                    "W52H29",
                    "W54H29",
                    "W62H29",
                    "W62H100",
                    "W60H86",
                    "W12","W29", "W38",
                    "W50", "W54",
                    "W62", "W62RB");
            String [] retArray = new String[paperSizeList.size()];
            retArray = paperSizeList.toArray(retArray);
            WritableArray promiseArray = Arguments.createArray();
            for(int i = 0; i < retArray.length; i++) {
                promiseArray.pushString(retArray[i]);
            }
            promise.resolve(promiseArray);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

while my JS code looks like this:

// Two arrays
printerList = [];
paperSizes = [];

// Functions

  getPrinterList = async () => {
    this.printerList = await MyRNModule.getPrinterList();
  }

  getPaperSizes = async () =>  {
    this.paperSizes = await MyRNModule.getPaperSizes();
  }

Upvotes: 1

Views: 1811

Answers (1)

Raphael Rafatpanah
Raphael Rafatpanah

Reputation: 19977

The error message says it all.

com.facebook.react.bridge.readablenativemap cannot be cast to java.lang.string

You are returning an array just fine, except a string was expected.

To solve this, you can convert the array to a string via join.

Upvotes: 1

Related Questions