0

I'm new to Flutter & Dart and trying to build an App.

My Goal for now is to build a List (Array), but with an "index" to access it later. Afterwards I want to output it within a widget. Unfortunately I haven't found any tutorials or helpful ressources on the web.

  1. Build a List which contains the date & days for an event. I tried the following, but it doesn't work.

    var events = [ {"day": "Monday", "date": "01.01.2019", "title": "Musical Event Austria"}, {"day": "Wednesday", "date", "01.01.2019", "title": "Musical Event 2"}];
    
  2. Loop throught the list and output it within a widget

    new Container(decoration: const BoxDecoration(
              color: Color(0xFF0F1633),
    ),
     child: SingleChildScrollView(
            scrollDirection: Axis.horizontal,
            child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: events.map((day, date, title) => new Text(title)).toList(),
              ),
            ),
          ),
    

So how may I build the list the right way and output it? What I want to output inside a Text Widget is actually the title, date and day...but really tried many ways to acchieve it, no way. :)))

Thanks in advance!

1 Answer 1

3

you may create a basic class for your need

class YourObject {
  final String title;
  final String day;
  final String date;

  YourObject({this.title, this.day, this.date});
}

create a List from that class/object

  List<YourObject> yourObjectList = [
    YourObject(day: "Monday", date: "01.01.2019", title: "Musical Event Austria"),
    YourObject(day: "Wednesday", date: "01.01.2019", title: "Musical Event 2"),
  ];

and you able to render title , date and day with like that or you can change the Container according to your needs/design

  return Column(
    children: yourObjectList.map((currentObject) {
      return Container(
        child: Row(
          children: <Widget>[
            Text(currentObject.title),
            Text(currentObject.day),
            Text(currentObject.date),
          ],
        ),
      );
    }).toList(),
  );

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

1 Comment

Thanks Sir! Worked like a charme. :)))

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.