Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StateError (Bad state: Cannot use "ref" after the widget was disposed.) #3459

Open
Rijankunwar3691 opened this issue Mar 29, 2024 · 1 comment
Assignees
Labels
bug Something isn't working question Further information is requested

Comments

@Rijankunwar3691
Copy link

Rijankunwar3691 commented Mar 29, 2024

Describe the bug
StateError (Bad state: Cannot use "ref" after the widget was disposed.)

To Reproduce
When i Click the Pay Now button i am getting this issue. if i remove

 await ref
                          .read(orderDetailNotifierProvider.notifier)
                          .getOrderDetails(
                            outletIdentifier: outletDetails
                                    .firstOrNull?.identifier
                                    ?.toInt() ??
                                0,
                            type: selectedTable != null ? 1 : 2,
                            typeId: selectedTable?.tableId ??
                                selectedTakeAway?.takeAwayUid ??
                                takeAwayUuid,
                          );

I am not getting any error.
<Please add a small sample to that can be executed to reproduce the problem. As a general rule, 100 lines is the maximum>

Expected behavior
Get order details and navigate to payement page if it orderdetail is not empty.

####### code------

onPressed: () async {

                  // Fetch order details
                  await ref
                      .read(orderDetailNotifierProvider.notifier)
                      .getOrderDetails(
                        outletIdentifier: outletDetails
                                .firstOrNull?.identifier
                                ?.toInt() ??
                            0,
                        type: selectedTable != null ? 1 : 2,
                        typeId: selectedTable?.tableId ??
                            selectedTakeAway?.takeAwayUid ??
                            takeAwayUuid,
                      );

                  // Order items if selectedMenuItems is not empty
                  if (selectedMenuItems.isNotEmpty) {
                    await ref
                        .read(orderItemNotifierProvider.notifier)
                        .orderItem(
                          cover: widget.cover,
                          kotNumber: 0,
                        );
                  }

                  // Retrieve order details after fetching
                  final List<Orders> orderDetails = ref
                          .read(orderDetailNotifierProvider)
                          .data
                          .data
                          ?.orders ??
                      [];

                  // Check if selectedMenuItems is empty and orderDetails is not empty
                  if (selectedMenuItems.isEmpty &&
                      orderDetails.isNotEmpty) {
                    // Fetch additional data
                    ref
                        .read(selectedTableTakeAwayProvider.notifier)
                        .getNewTableOrTakeAway(
                          outletDetails: outletDetails.firstOrNull,
                          selectedTable: selectedTable,
                          selectedTakeAway: selectedTakeAway,
                        );
                    final newSelectedTable = ref
                        .read(selectedTableTakeAwayProvider)
                        .selectedTable;
                    final newSelectedTakeAway = ref
                        .read(selectedTableTakeAwayProvider)
                        .selectedTakeAway;

                    // Prepare bill
                    ref
                        .read(prepareBillNotifierProvider.notifier)
                        .prepareBill(
                          ouletIdentifer:
                              outletDetails.firstOrNull?.identifier ?? 0,
                          tableId: newSelectedTable?.tableId ?? 0,
                          roomId: 0,
                          takeAwayUid: newSelectedTable == null
                              ? newSelectedTakeAway?.takeAwayUid ??
                                  takeAwayUuid
                              : null,
                        );

                    // Get settlement data
                    ref
                        .read(settlementDataNotifierProvider.notifier)
                        .getSettlementData(
                          outletIdentifier:
                              outletDetails.firstOrNull?.identifier ?? 0,
                        );

                    // Navigate to the payment page
                    if (context.mounted) {
                      Navigator.pushNamed(context, AppRoutes.payementPage);
                    }
                  }
                },
@rrousselGit
Copy link
Owner

That's way too much code. Please make a minimal reproducible example.

Generally speaking, this error is expected and a mistake on your end. Chances are you used ref after an await without checking context.mounted

@rrousselGit rrousselGit added question Further information is requested and removed needs triage labels Mar 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants