0

I want to print the index of array element as another array. I have array list name btns. It contains the value is true or false. I want the index of element having true. here my code is

List<bool> btn = [true, false, true, false, false, false, false];
Map<int, bool> map2 = btn.asMap();
var arr = new List<int>();
map2.forEach((key, value) {
  if (value) {
    print(key);
    arr.add(key);
  }
});

print(arr);

It is printin [0,2]. And showing correct results. But when I put in method. It showing error.

List<int> getbtnsInArray() {
Map<int, bool> map2 = btn.asMap();
var arr = new List<int>();
map2.forEach((key, value) {
  if (value) {
    print(key);
    arr.add(key);
  }
});
   return arr;
}

print (getbtnsInArray);

It showing the error is Closure: () => List from Function 'getbtnsInArray':.

I don't know the reason. Please help me to find the answer.

1 Answer 1

1

You need to add brackets to call the method.

print(getbtnsInArray());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.