0

I am trying to use a code like below:

class MultiPageProvider extends StatefulWidget {
  const MultiPageProvider({Key? key}) : super(key: key);

  @override
  _MultiPageProviderState createState() => _MultiPageProviderState();
}

class _MultiPageProviderState extends State<MultiPageProvider> {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<UserModal>(
      create: (context) => UserModal(),
      child: Scaffold(
        appBar: AppBar(
          title: const Text(
            "Using Provider",
          ),
        ),
        body: Consumer<UserModal>(
          builder: (context, modal, child) {
            switch (modal.activeIndex) {
              case 0:
                return const BasicDetails();
              case 1:
                return const EducationDetails();

              default:
                return const BasicDetails();
            }
          },
        ),
      ),
    );
  }
}

But as long as I use all the providers inside the main.dart function like below, I am wondering to know how can I add this provider to main.dart like the others?

class MyApp extends StatelessWidget { const MyApp({super.key});

@override
Widget build(BuildContext context) {
  return MultiProvider(
    providers: [
      ChangeNotifierProvider.value(
        value: Auth(),
      ),
      ChangeNotifierProxyProvider<Auth, Books>(
        create: (ctx) => Books('', []),
        update: (ctx, auth, previousBooks) => Books(
          auth.userId,
          previousBooks == null ? [] : previousBooks.books,
        ),
      ),

    ],
};

}

best_of_man
  • 643
  • 2
  • 15

1 Answers1

0

I would give this another thought. Have multiple providers given your base class and have the "for logic" somewhere else in the class.

 @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        Provider<BasicDetails>(
          create: (_) => BasicDetails(),
        ),
        Provider<EducationDetails>(
          create: (_) => BasicDetails(),
        ),
AkuKaku
  • 48
  • 4