0

In my app, I have a list of 'goals'.

On the main page, there is a list.map than makes a button for each goal.

These buttons contain the name and description of the goal.

When the user clicks on them, they're taken to another page (goalPage)

When the goalPage loades, it needs to display the goal the user clicked on. I was thinking that if i could get acces to the list-idex-number of the goal, in the goalPage, it would work, because all the info I need is in the list. But I don't now how to get that index-number, spesially because it can change, it the user deletes a goal or adds a new one.



List (on MainPage):

List<Goal> goals = [
   Goal(title: 'Example goal', description: 'this is an example goal'),
];

Display of goals (on MainPage):

Column(
   children: goals.map((goal) => GoalTemplate(goal: goal)).toList(),
),

goal button template (on goalTemplate file):

class GoalTemplate extends StatefulWidget {

  final Goal goal;
  GoalTemplate({this.goal});

  @override
  _GoalTemplateState createState() => _GoalTemplateState();
}

class _GoalTemplateState extends State<GoalTemplate> {

  int listIndex;

  void navToGoalPage () async {
    Navigator.of(context).push(MaterialPageRoute(
      builder: (context) => GoalPage(),));
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        setState(() {
          listIndex = goals.indexOf();
        });
        navToGoalPage();
      },
      child: Column(
              children: <Widget>[
                Text(widget.goal.title),
                Text(widget.goal.description),
              ],
            ),
    );
  }
}

(The goal page doesn't work at all, well that's kinda why im asking here, I need to have a page, just with a Varioble of the current-index-value (not the total number of items in the list))

1 Answer 1

1

Try changing from using map to using for function:

Column(
  children: [
    for(int index = 0; index < goals.length; index++) // goals[index] is what you want to get
      GoalTemplate(goal: goal)
  ]
)
Sign up to request clarification or add additional context in comments.

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.