2

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.

1
  • do you want a list of text widgets? Commented Nov 19, 2020 at 8:35

4 Answers 4

3

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,
        ),
      ),
    );
  }

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

5 Comments

And how can i show it in scaffold widget ? Mean i am using for loop before returning scaffold how can i show this now in scaffold widget ?
you need to learn flutter basics first. You cannot randomly execute for loop anywhere. Everything in flutter is a Widget, So I suggest you read more about widgets because you'll need some kind of listing widget to contain all of your text widgets. When building the list widget you can map your array elements to text widgets and pass them as children.
@Akash Sarode is right. And I edited the answer with some more code.
@Akif its showing error now RangeError (index): Invalid value: Only valid value is 0: 1
Of course, that is my bad. Removed curly braces between the string array.
3

You can use toList which makes easier and also use indexOf if you need index

import 'package:flutter/material.dart';

class DateDataWidget extends StatelessWidget {
  final 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'
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
          child: Column(
        children: datesData.map((date) {
          int index = datesData.indexOf(date); // use index if you want.
          print(index);
          return Text(date);
        }).toList(),
      )),
    );
  }
}

You can use int index if you need.

Comments

3
make it more dynamic

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

Update You can show with listview.builder

class MyWidget extends StatelessWidget {
  final List data = [
    '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'
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView.builder(
          itemCount: data.length,
          itemBuilder: (BuildContext ctxt, int index) {
            return new Text(data[index]);
          }),
    );
  }
}

Comments

1

try this code on dartpad.dev

import 'package:flutter/material.dart';

void main() {
  runApp(App());
}

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: MyWidget(),
        ),
      ),
    );
  }
}

class MyWidget extends StatelessWidget {
  final List data = [
    '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'
  ];
  
  @override
  Widget build(BuildContext context) {
    return ListView(
      children: data.map((element) => ListTile(title: Text(element))).toList(),
    );
  }
}

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.