Reputation: 359
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.
Upvotes: 2
Views: 6061
Reputation: 2786
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]);
}),
);
}
}
Upvotes: 3
Reputation: 1337
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.
Upvotes: 3
Reputation: 447
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(),
);
}
}
Upvotes: 1
Reputation: 7640
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,
),
),
);
}
Upvotes: 3