Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter how to show array data in text widget by for loop

Tags:

flutter

I have simple list in which some string are available. I just to need to show all strings in Text widget by for loop

I try like this

List datesData = [{
  '09:00 am - 10:00 am',
  '10:00 am - 11:00 am',
  '11:00 am - 12:00 am',
  '12:00 am - 01:00 am',
  '01:00 am - 02:00 am',
  '02:00 am - 03:00 am',
  '03:00 am - 04:00 am',
  '04:00 am - 05:00 am'
}];

for(int i = 0; i < 8; i++){
     Container(
       child: Text(datesData(i)),
     );
}

ITs show error in datesData The expression doesn't evaluate to a function, so it can't be invoked.

Also in don't want to define length of for lopp like for(int i = 0; i < 8; i++) i need to check it automatically that it has 8 strings.

like image 371
rameez khan Avatar asked Jul 31 '26 05:07

rameez khan


1 Answers

You need to get the indexed data of the date array. There is an incorrect usage of parentheses. You need to use square brackets to get an index of array. It should look like this:

 for(int i = 0; i < 8; i++){
    Container(
     child: Text(datesDate[i]),
      );
    }

EDIT:

Ok, you can use this widget list inside a column. Now, it will look like this:


  List datesData = [
    
      '09:00 am - 10:00 am',
      '10:00 am - 11:00 am',
      '11:00 am - 12:00 am',
      '12:00 am - 01:00 am',
      '01:00 am - 02:00 am',
      '02:00 am - 03:00 am',
      '03:00 am - 04:00 am',
      '04:00 am - 05:00 am'
    
  ];

  List<Widget> textWidgetList = List<Widget>(); // Here we defined a list of widget!

  @override
  Widget build(BuildContext context) {
    for (int i = 0; i < 8; i++) {
      textWidgetList.add(
        Container(
          child: Text(datesData[i]),
        ),
      );
    }

    return Scaffold(
      body: Container(
        child: Column(
          children: textWidgetList,
        ),
      ),
    );
  }

like image 127
Akif Avatar answered Aug 01 '26 18:08

Akif



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!