0

Would like to loop through a multidimensional object but appear to be missing something.

String label, action;
    var symbol;
    Object toggles = [
      {
        label = 'Reckless',
      },
      {
        label = 'Feckless',
      },
      {
        label = 'Legless',
      },
    ];

I attempted to use this in a Row:

   Row(
       children: for (var toggle in toggles) {
Text('label ${toggle.label}')
                  },
                );

Error message concerns for stating that it expected an identified. Clearly I'm not fully understanding how to do this.

7
  • If my question is so awful then perhaps you could explain why and then I wouldn't do it again. Commented Oct 24, 2021 at 13:27
  • can't you use a Map or List rather than object? Commented Oct 24, 2021 at 13:27
  • @rosh-dev I have tried with Map but it states that I cannot used named values. I believe I'm missing something in the object construction. Commented Oct 24, 2021 at 13:28
  • you can create object array by changing syntax like 'label' : 'Reckless' but you can't iterate through toggles array because it doesn't implement Iterable<dynamic> class. I think you need to find a another solution for your requirement Commented Oct 24, 2021 at 13:42
  • @rosh-dev Thanks. Ultimately I want to control the entire object for this via JSON as I'll also be dealing with language variants. If we were to imagine that "toggles" was in fact a JSON array containing the values of label, symbol and action per record, would that be a better approach? Commented Oct 24, 2021 at 13:45

1 Answer 1

2

I tried to run your code. The error :

Error: The type 'Object' used in the 'for' loop must implement 'Iterable<dynamic>'.

You cannot iterate on an Object.

If you change Object toggles = to List toggles = for example (which is iteratable), then, it is working.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. That sounds very plausible. Ultimately I want to use JSON for these objects so I'm looking at the JSON functions in Dart but this is extremely helpful for my understanding.

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.