1
votes

I have a solidity contract getter function for an array of string :

string[] public flightsRegistered;

function getFlightsRegistered
                          (
                          )
                          public
                          view
                          returns(string[])

{
  return flightsRegistered;
}

While compiling with truffle, this is what i get

Truffle compile error

TypeError: This type is only supported in the new experimental ABI encoder. Use "pragma experimental ABIEncoderV2;" to enable the feature. returns(string[])

Any other work around?

Web3 v1.0.0-beta.37

Truffle v5.0.9

Solidity v0.4.24 (solc-js)

1
The error message is pretty clear. Is there something specific you're having trouble understanding?user94559
Yeah, is this the right way to return an array of strings to a DAPP?Husni Abdul Nazer

1 Answers

2
votes

Currently solidity only support return of array if you use pragma experimental ABIEncoderV2. If you dont want to use that, you have to create one more function that will return the lenght of the array and in the Dapp creates a for loop and access the element of array through index. Below is the sample code

pragma solidity >=0.4.22 <0.6.0;
contract Array {

string[] public flightsRegistered;


function getFlightsRegistered(uint _index) public view returns(string memory){
  return flightsRegistered[_index];
}

function totalFlightsRegistered() public view returns (uint ){
    return flightsRegistered.length;
}
}