skip to Main Content

I have the following list

steps: [
‘Tenderize the veal to about 2–4mm, and salt on both sides.’,
‘On a flat plate, stir the eggs briefly with a fork.’,
‘Lightly coat the cutlets in flour then dip into the egg, and finally, coat in breadcrumbs.’,
‘Heat the butter and oil in a large pan (allow the fat to get very hot) and fry the schnitzels until golden brown on both sides.’,
‘Make sure to toss the pan regularly so that the schnitzels are surrounded by oil and the crumbing becomes ‘fluffy’.’,
‘Remove, and drain on kitchen paper. Fry the parsley in the remaining oil and drain.’,
‘Place the schnitzels on awarmed plate and serve garnishedwith parsley and slices of lemon.’
],

I’m trying to output it with an incremental counter as shown: writing it as clear steps

So, I wrote this code where
1- I’ve initialize a var to 1,
2- and Increment it with the loop

and here is the problem, I have to remove const from the constructor because I’m using a var instead of final (since I’m changing the value) but I got a warning not an error actually,

This class (or a class that this class inherits from) is marked as '@immutable', but one or more of its instance fields aren't final: MealDetailsScreen.count

but it made me think if there are any other efficient ways to output the list numbered, or should I ignore the warning?

2

Answers


  1. You can’t declare a var in a stateless widget. You’ll need to figure out another way to achieve this counter. Luckily, this is pretty easy since you are using Riverpod.

    It looks like you are using a ConsumerWidget from Riverpod, which can provide you with the illusion of a stateful widget, but in reality, you are still using a stateless widget. I would create a new state provider and go from there with your counter.

    Alternatively, you could use the index of your for-loop to act as the counter.

    Login or Signup to reply.
  2. The collection package has a mapIndexed method that could help you create a numbered list of steps in your constructor, that you assign to a late final variable.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search