Husni Abdul Nazer
Husni Abdul Nazer

Reputation: 101

Could not return array of Strings from getter

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)

Upvotes: 0

Views: 985

Answers (1)

Uahmed
Uahmed

Reputation: 1845

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;
}
}

Upvotes: 2

Related Questions