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

Help - Implementing with Provider #27

Closed
eitanaviv opened this issue Dec 4, 2019 · 6 comments
Closed

Help - Implementing with Provider #27

eitanaviv opened this issue Dec 4, 2019 · 6 comments
Labels
help wanted Extra attention is needed

Comments

@eitanaviv
Copy link

Hi there,
I'm trying to implement Alice using Provider without success.
The only way I managed to implement Alice is by not declaring it as final field under StatelessWidget class (which gives me a warning, but still works).
My code:

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  Alice _alice;
  final _database = constructDb();

  @override
  Widget build(BuildContext context) {
    SystemChrome.setEnabledSystemUIOverlays([]);

    _alice ??= Alice(
        showNotification: false, showInspectorOnShake: true, darkTheme: true);

    return MultiProvider(
      providers: [
        Provider<MyDatabase>(
          create: (context) => _database,
          dispose: (context, value) => value.close(),
        ),
        Provider<ApiProvider>(
            create: (context) => ApiProvider(_database, _alice))
      ],
      child: MaterialApp(
        navigatorKey: _alice.getNavigatorKey(),
        title: 'MyApp',
        theme: ThemeData(
          fontFamily: "Rubik",
          brightness: Brightness.dark,
          primaryColor: Config.primaryColor,
          primaryColorDark: Config.darkColor,
          accentColor: Config.blueColor,
          scaffoldBackgroundColor: Config.darkColor,
          canvasColor: Config.primaryColor,
        ),
        home: HomeScreen(),
      ),
    );
  }
}

Note: ApiProvider is a class for implementing Dio latest version.

I would appreciate the help!

P.S.
I think it would be nicer if the calls list will be ordered as DESC and not ASC, and also wrap all the screens with SafeArea (it's not readable when using a device with a notch).

@MattisBrizard
Copy link
Contributor

MattisBrizard commented Dec 9, 2019

Hi @eitanaviv !

I don't think Provider matters here (unless you want to provide your Alice instance to the children)
I think that you can do this :

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  final Alice _alice = Alice(
        showNotification: false, showInspectorOnShake: true, darkTheme: true);
  final _database = constructDb();

  @override
  Widget build(BuildContext context) {
    SystemChrome.setEnabledSystemUIOverlays([]);

    return MultiProvider(
      providers: [
        Provider<MyDatabase>(
          create: (context) => _database,
          dispose: (context, value) => value.close(),
        ),
        Provider<ApiProvider>(
            create: (context) => ApiProvider(_database, _alice))
      ],
      child: MaterialApp(
        navigatorKey: _alice.getNavigatorKey(),
        title: 'MyApp',
        theme: ThemeData(
          fontFamily: "Rubik",
          brightness: Brightness.dark,
          primaryColor: Config.primaryColor,
          primaryColorDark: Config.darkColor,
          accentColor: Config.blueColor,
          scaffoldBackgroundColor: Config.darkColor,
          canvasColor: Config.primaryColor,
        ),
        home: HomeScreen(),
      ),
    );
  }
}

@eitanaviv
Copy link
Author

Hi @MattisBrizard,
I already tried your solution but it's not working..
About Provider - I have to use it in order to create one instance of ApiProvider class, and because of that I must declare Alice in MyApp in order to use the _alice.getNavigatorKey() function.
If you have another idea for me, please :)

@MattisBrizard
Copy link
Contributor

MattisBrizard commented Dec 10, 2019

@eitanaviv
What is your error doing like this ? (I think this is not due to Alice, but how you use Provider)

@jhomlala jhomlala added the help wanted Extra attention is needed label Dec 22, 2019
@alyyasser
Copy link
Contributor

alyyasser commented Jan 6, 2020

you should use ProxyProvider if you want to provide an alice instance to other provider.

here's some example:

class MainApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        Provider.value(value: Alice()),
        ProxyProvider0<Api>(
          update: (BuildContext context, Api value) {
            return value ?? Api(alice: Provider.of(context));
          },
        ),
      ],
      child: ...,
    );
  }
}

by doing so, you can get the provided alice instance in any descendant widget, using:

Alice alice = Provider.of(context);

or

var alice = Provider.of<Alice>(context);

@eitanaviv
Copy link
Author

@MattisBrizard I'm not getting any error, but it seems that the method I use creates multiple instances of Alice and because of that I'm getting notifications but when entering Alice's screen I see nothing.

@eitanaviv
Copy link
Author

@alyyasser Great solution!!
Works perfect now, thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants