`Finish` Flutter button with reset() and pushNamed() results in “Concurrent modification during iteration” error

Introduction

I am working on Flutter and Dart.
In the app, I have a screen that displays the result of the invoice scan,
It doesn’t matter whats the content there,
because the question not related to it.
Within this screen I added a button named Finish,
which suppose to take the user to the HomeScreen.

Actually, this button takes the user to the HomeScreen as expected,
but then, trying to scan another invoice face an error.
e.g, For the first scan it succeeds, but comes back to home
by the Finish button probably causes a Bug,
so the next try to scan is failed.
Actually, I tried two approaches, where the first works well,
but the second faces a Bug.

The relevant code snippet

Attention:
Share Button (and its implementation code,
consist of _showExportOptions),
dose not relevant for the question.

Attention:
You have to uncomment the approach you want to test.

 @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start, // Align all buttons to the start
      children: [
        // 'Share' Button
        InkWell(
          onTap: () => _showExportOptions(context, widget.vendor),
          child: _buildIconButton(Icons.share, 'Share', color: Colors.white),
        ),
        SizedBox(width: 16), // Space between buttons

        // 'Finish' Button
        InkWell(
          onTap: () async {
    /*
    // The first approch:  you see 3 `Navigator lines`
    // because for my app this takes you to `HomePage`.
    Navigator.of(context).pop();
    Navigator.of(context).pop();
    Navigator.of(context).pop();
    Provider.of<SupplierModel>(context, listen: false).reset();
    */

            /*
            // The second approach
            Navigator.of(context).pushNamed(HomeScreen.routename, arguments: 1);
            Provider.of<SupplierModel>(context, listen: false).reset();
            */
          },

          child: _buildIconButton(Icons.check, 'Finish', color: Colors.white),
        ),
      ],
    );
  }


  Widget _buildIconButton(IconData icon, String label, {Color? color}) {
    return Column(
      children: [
        Icon(icon, color: color),
        Text(label, style: TextStyle(color: color)),
      ],
    );
  }

The issue

Expected:
A button for going back to HomeScreen.
Actually:
The user came back to Home Screen, but after that, trying to scan a new invoice failed.

With this approcah, all works well:

Navigator.of(context).pop();
    Navigator.of(context).pop();
    Navigator.of(context).pop();
    Provider.of<SupplierModel>(context, listen: false).reset();

However, with this approach, an error occurs:

Provider.of<SupplierModel>(context, listen: false).reset();
Navigator.of(context).pushNamed(HomeScreen.routename, arguments: 1);
 

The error massage

@pragma("vm:external-name", "Error_throwWithStackTrace")
Concurrent modification during iteration: Instance(length:36) of '_GrowableList'.

Somtimes, insted of the error massage i see:
“Your invoice is not align”.

Attention:
I also tried to place the Provider after the Navigator,
but that does not solve it.

Moreever, I also tried to use Future delay of 100 miliseconds,
but its is also failed.

New contributor

Yogev Arye is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật