0

I have a ListView.builder that gets its data from:

EnsaioItemModel ensaioitem = snapshot.data![index];

no onTap: ()

I call the form to edit the data:

await Navigator.push(
  context,
  MaterialPageRoute(
  builder: (context) => EnsaioItemFormView(),
  ),
) 

In the form, I make the maintenance of data and close it with

Navigator.pop(_buildContext);

I need to close the form, get the next index of ListView, and call the form again.

How could I do this?

2
  • Could you describe exactly what you want to achieve? E.g do you want the form for index 2 to open immediately you close the form for index 1? Commented Mar 26 at 0:48
  • Also, please include the entire ListView.builder to make it easier to provide an answer for your usecase. Commented Mar 26 at 0:49

3 Answers 3

1
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => const SizedBox(),
              ),
            ).then((value) {
              /// push new form
            });

I understand what you mean but what do you think about my solution, instead of pushing the next form in the form, you use func "then" to make the first push finish and then perform the next step.

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

Comments

0
void editSequence(BuildContext context, List<EnsaioItemModel> items, int index) async {
  if (index >= items.length) return;
  await Navigator.push(
    context,
    MaterialPageRoute(builder: (context) => EnsaioItemFormView(item: items[index])),
  );
  editSequence(context, items, index + 1);
}

// In ListView.builder onTap:
onTap: () => editSequence(context, snapshot.data!, index),

This recursive function opens the form for each item sequentially, moving to the next index after each Navigator.pop().

Comments

0

If I understand this correctly, you have a ListView with multiple children. When the user pressed, or taps, on an item, you want to push an edit ui/page with the current item as data for the page, then when the user is done editing, you want to pop the edit ui, and then re-open it with the next item from your ListView.

So basically you have a ItemsUi and a EditItemUi. The clean way to handle this is from the ItemsUi, not the EditItemUi. Here is the code, and the explanation after it:

// ItemsUi
  final items = [//your items];

  .....

  @override
  Widget build(BuildContext context) {
    ListView.builder(itemBuilder: (context, index) => ListTile(title: items[index].name, onTap: () => onItemTap(index),)

   ....

  onItemTap(index) async {
    if(index >= items.length) return;
    final result = await Navigator.push(
      context,
      MaterialPageRoute(
      builder: (context) => EditItemUi(items[index]),
      ),
    );
    onItemTap(++index);
  }

  _processResult(result) { // process result}

In your onTap function, you should keep the index of the item, and then await the result. Once you have the result, just call the same method recursively on the next item, and so on.

PS: PLEASE don't pass the buildContext as a result in Navigator.pop(result). This will make you run down a rabit hole of errors.

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.