0

How do you get the depth of a List in flutter/ dart language, given a list like this one

List testRy = [1,2,[3,4,[5,6],7,[8,[9,91]],10],11,12];

Can you help me create something like an extension for doing that, that is callable from anywhere?

1
  • This is not depth. This is the maximum length of a nested list as one of the elements of the original list. But this is not the depth of the list. Commented Sep 27, 2023 at 10:35

1 Answer 1

0

I found a solution!

Created a list extension as below:

extension Lists<E> on List<E> { int get depth { int depth = 0; getElementDepth(List list) { depth++; for (var item in list) { if (item is List) { getElementDepth(item); } } return depth; } return getElementDepth(this); } }

and now I can call use it like this:

let test = [1,2,[3,4,[5,6],7,[8,[9,91]],10],11,12]; print(test.depth);

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.