2

I am doing an experiment, where I want to make two similar apps with single source code.

I am trying to make an "adaptive" State on top of the widget tree, and this state (ChangeNotifier) depends on the application (isApp1() determines which app is this ).

here's the code that I'm using:

ChangeNotifierProvider(
      create: (context) {
        return isApp1() ? AppState1() : AppState2();
      },
      child: MaterialApp(
        onGenerateRoute: isAdminApp()
            ? App1Router.generateRoute
            : App2Router.generateRoute,
        initialRoute: "/",
      ),
    );

when I try to read the state using Provider.of in the low levels of the tree I am facing the error:

Error: Could not find the correct Provider<AppState1> above this Widget

Note: when I put AppState1() or AppState2() directly instead of isApp1() ? AppState1() : AppState2() I don't face this error.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
tareq albeesh
  • 1,701
  • 2
  • 10
  • 13
  • Are you sure that isApp1() returns the correct value? Do you use the same method when you are trying to find which provider you should read?. Based on the error you posted, it looks to me you are trying to read AppState1 when you should be reading AppState2 – Sandeep Singh May 31 '22 at 10:57
  • That's not the case since when I put `AppState1()` or `AppState2()` directly instead of `isApp1() ? AppState1() : AppState2()` I don't face this error. – tareq albeesh May 31 '22 at 13:03
  • Exactly. You know what to read from the Widget. Try reading `AppState2` manually instead of `AppState1` when you should be reading the last one; you will probably face the same error. – Sandeep Singh Jun 01 '22 at 07:02

0 Answers0