0

The following code is to open a new customer adding widget.

final newOrderIdScopedProvider = Provider.autoDispose<int>((ref) {
  ref.onDispose(() { 
   debugPrint('newOrderIdScopedProvider of value ${ref.state} has been disposed');
  });
  throw UnimplementedError('newOrderIdScopedProvider has not been initialized');
});

Navigator.of(context).push(
                            HeroDialogRoute(
                              builder: (_) => ProviderScope(
                                overrides: [
                                  newOrderIdScopedProvider.overrideWithValue(orderId),
                                ],
                                child: const AddNewCustomerWidget(),
                              ),
                            ),
                          );

The newOrderIdScopedProvider has been overriden for this page as it may be opened from different places with different orderIds

Now look at the AddNewCustomerWidget

SizedBox(
      width: 1100,
      height: 600,
      child: ClipRRect(
        borderRadius: BorderRadius.circular(10),
        child: Scaffold(
          key: addNewCustomerNotifier.scaffoldKey,
          body: SingleChildScrollView(
            scrollDirection: Axis.vertical,
            child: SingleChildScrollView(
              scrollDirection: Axis.horizontal,
              child: SizedBox(
                width: 1100,
                height: 600,
                child: Column(
                  children: const [
                    AddNewCustomerHeader(),
                    AddNewCustomerTextField(),
                    VerticalSpacer(height: 10),
                    Divider(),
                    AddNewCustomerTabsWidget(),
                    AddNewCustomerTabsView(),
                  ],
                ),
              ),
            ),
          ),
        ),
      ),
    );

The problem is that from inside the build method of the AddNewCustomerWidget, the newOrderIdScopedProvider can be read and everything is ok.

But,

when I try to read the newOrderIdScopedProvider from inside the build methods of the children like AddNewCustomerHeader() or AddNewCustomerTextField() or anyone,

I get the error message that the newOrderIdScopedProvider has not been Initialized.

Why is this happenning?

Karrar
  • 1,273
  • 3
  • 14
  • 30

1 Answers1

0

I Figured out the solution

the problem is that the AddNewCustomerWidget is a StatefulWidget (ConsumerStatefulWidget) and as it seems, the ScopedProvider can not be read by the children of the statefulWidget

the conclusion is that the provider scope can be read by the children of a widget only if that widget is stateless widget of kind of (ConsumerWidget)

and I just wrapped the children into a separated ConsumerWidget which is also wrapped by a ProviderScope to scope the newOrderIdScopedProvider

and everything went Ok .

Karrar
  • 1,273
  • 3
  • 14
  • 30