Can I wrap the MaterialApp widget with the Provider?
Asked Answered
S

2

5

I'm new to flutter and I'm trying to create an app with provider. I wrapped MaterialApp widget with the ChangeNotifierProvider and the app works and I can use the provider as it intended to do. I need to know is it okay to do so and will i face any problems?

Widget build(BuildContext context) {
    return ChangeNotifierProvider<BaseModel>(
        builder: (context) =>
            BaseModel(loading: false, title: "Title", isLoggedIn: false),
        child: MaterialApp(
            routes: <String, WidgetBuilder>{
                "/home": (BuildContext context) => Home(),
                "/signIn": (BuildContext context) => SignIn()
            },
            initialRoute: "/signIn",
            title: 'Flutter Demo',
            theme: ThemeData(
                // is not restarted.
                primarySwatch: Colors.blue,
            ),
            home: SignIn()),
    );

In all the sample codes they use Provider under "home" in MaterialApp widget. I used MaterialApp inside the provider.

Sol answered 28/8, 2019 at 11:37 Comment(0)
A
16

It is totally fine. There's no problem whatsoever.

Authority answered 28/8, 2019 at 11:42 Comment(1)
Thanks! Do you have a better solution to use provider pattern with named routing?Sol
O
0

They just changed the name of 'builder' to 'create'.

return ChangeNotifierProvider<BaseModel>(
    create: (context) =>
        BaseModel(loading: false, title: "Title", isLoggedIn: false),
    child: MaterialApp(...
Occult answered 28/9 at 6:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.