0

I am looping through an array i.e 'companies' using the for loop. I want to display the first element in this array using 'for loop' without having to do it this way 'companies[0].name'. Here is what I have done which works but I need to use them for loop.

    child: Column(
      children: < Widget > [
        // for (var item in companies)
        companies.length > 0 ?
        Text(
          '${companies[0].name}'.toUpperCase(), // how can i replace this using the for loop
          style: TextStyle(
            color: AppColor.white,
          ),
        ) :
        Text('No Company Assigned '),
      ],
    ),

10
  • companies.length > 0 ? ...companies.map((c) => Text('${c.name}')) : Text('No Company Assigned ') Commented Nov 24, 2021 at 13:54
  • why don't you want to use companies[0].name and why do you need to use for loop, can you clarify your question? Commented Nov 24, 2021 at 13:55
  • @YeasinSheikh because i will still need the for loop later and one because i would like to know if there's a work-around this using the for loop. Commented Nov 24, 2021 at 13:59
  • @pskink your suggestion displays all the elements in the array. How can i modify it to only display the first element? Commented Nov 24, 2021 at 14:04
  • 1
    wait, if you want first element what do you need a loop for? what do you need a Column for if it has just one child? what actually do you want to achieve? Commented Nov 24, 2021 at 14:05

3 Answers 3

2

You can use an inline function to create a loop. Though, I will suggest keep the logic simple here. And most prefer using map in your case as @pskink describe on comment section.

 Column(
        children: [
          () {
            // do the thing you want and return
            return companies.isEmpty
                ? Text('No Company Assigned ')
                : Text(companies.first.toUpperCase());
          }(),
        ],
      ),
Sign up to request clarification or add additional context in comments.

Comments

0

You can use ListBuilder like below example, if its first element return your widget, else return empty container.

final List<String> entries = <String>['A', 'B', 'C'];
final List<int> colorCodes = <int>[600, 500, 100];

ListView.builder(
  padding: const EdgeInsets.all(8),
  itemCount: entries.length,
  itemBuilder: (BuildContext context, int index) {
    return index == 0  ? Container(
      height: 50,
      color: Colors.amber[colorCodes[index]],
      child: Center(child: Text('Entry ${entries[index]}')),
    ): Container();
  }
);

Comments

0

You can use the mapIndexed or forEachIndexed extension methods from the collection package.

import 'package:collection/collection.dart';

item.forEachIndexed((index, element) {
  print('index: $index, element: $element');
});

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.