rrousselGit / provider

InheritedWidgets, but simple
https://pub.dev/packages/provider
MIT License
5.1k stars 509 forks source link

Example for providers #65

Closed ManojMM026 closed 4 years ago

ManojMM026 commented 5 years ago

It will be great if we can have an example of every available provider to start with. Its easier for a guy like me who is just starting with flutter and provider to understand it better.

Moanrisy commented 5 years ago

Anyone have Listenable provider example?

rrousselGit commented 5 years ago

This is clearly needed. But what would be reasonable examples?

Is a counter app using each provider acceptable?

rubensdemelo commented 5 years ago

Would like to suggest more real case situations, e.g. http requests, combined with service/repository, etc

ahmetkocadogan commented 5 years ago

As a newbie, it is always hard to figure out how to make authentication with api token and manage auth state within application. This kind of example can be really helpful.

HaithamSheshtawy commented 5 years ago

For me, I'm creating my design system. I'm trying to make my atoms listen to the app theme. I need to have a central place. My current implementation is as following:

    @override
      Widget build(BuildContext context) {
        return Consumer<ThemeDataManager>(
            builder: (context, ThemeDataManager _themeDataManager, _) {
          return Text(
            TextAtomUtils.getTextData(
                data, variationFiltered.textCase ?? DEFAULT_TEXT_CASE),
            locale: locale,
            maxLines: maxLines,
            overflow: overflow,
            semanticsLabel: semanticsLabel,
            softWrap: softWrap,
            strutStyle: strutStyle,
            style: style ?? TextAtomUtils.getStyle(variationFiltered),
            textAlign: textAlign ?? TextAtomUtils.getTextAlign(variationFiltered),
            textDirection: textDirection,
            textScaleFactor: textScaleFactor,
          );
        });
      }

This approach is working fine. instead of the above approach, I need to refactor it to be as follows:

      @override
        Widget build(BuildContext context) {
            return  WithTheme(child: constructChild(),);
        }

      Widget constructChild(){
          return Text(
              TextAtomUtils.getTextData(
                  data, variationFiltered.textCase ?? DEFAULT_TEXT_CASE),
              locale: locale,
              maxLines: maxLines,
              overflow: overflow,
              semanticsLabel: semanticsLabel,
              softWrap: softWrap,
              strutStyle: strutStyle,
              style: style ?? TextAtomUtils.getStyle(variationFiltered),
              textAlign: textAlign ?? TextAtomUtils.getTextAlign(variationFiltered),
              textDirection: textDirection,
              textScaleFactor: textScaleFactor,
            );
        }

WithTheme class as follows:

    class WithTheme extends StatelessWidget {
      WithTheme({@required this.child});

      final Widget child;

      @override
      Widget build(BuildContext context) {
        return Consumer<ThemeDataManager>(
            builder: (context, ThemeDataManager _themeDataManager, _) {
          return child;
        });
      }
    }

but unfortunately, it is not working.

HaithamSheshtawy commented 5 years ago

Finally, I succeeded to fix it as follows:

class WithTheme<T extends ThemeDataManager> extends StatelessWidget {
   WithTheme({@required this.builder});

  Widget Function(BuildContext context, T model, Widget child) builder;

  @override
  Widget build(BuildContext context) {
    return Consumer<T>(builder: builder,);
  }
HaithamSheshtawy commented 5 years ago

I recommended this Structure for Provider. https://medium.com/flutter-community/flutter-architecture-provider-implementation-guide-d33133a9a4e8

bradyt commented 5 years ago

@rrousselGit I think adding super simple cases of each approach is a great idea. I don't see any reason to omit the simple cases. In addition to any less trivial cases people feel should be covered.

bradyt commented 5 years ago

There are now two examples linked at https://flutter.dev/docs/development/data-and-backend/state-mgmt/simple#putting-it-all-together.

chimon2000 commented 5 years ago

What stuck out to me after watching provider episode on The Boring Show, was that just simple documentation on how to use each provider, their tradeoffs, and example use cases would go a long way.

In this case, the showrunners fell back to ChangeNotifierProvider because it was not immediately obvious how to use ValueListenableProvider in the current documentation.

rrousselGit commented 5 years ago

Hum, to be honest, guessing others' problem is not part of my skills.

It'd help me tremendously if peoples could:

rrousselGit commented 5 years ago

Also, bear in mind that there's not one finite architecture using provider.

Some do scoped_model, others do Mobx, or others go for setState.

A complex example would implicitly mean that one of them is better than the others when that's not necessarily true,

zrfrank commented 5 years ago

I really wish we could have some DOs and DONTs in the documentation.

bradyt commented 5 years ago

Perhaps if we there were a set of examples at the most introductory level, to give a notion of each provider. Similar to the examples at https://medium.com/flutter/managing-flutter-application-state-with-inheritedwidgets-1140452befe1.

@HansMuller, can we convince you to write a followup along the lines of this provider library? Thank you for that article by the way.

bastienJS commented 4 years ago

This is clearly needed. But what would be reasonable examples?

Is a counter app using each provider acceptable?

A counter app is counterproductive...

The api says me nothing. Its cryptic. How to create parent : child relations with n-levels ?

where each level has behavior like canAdd,canDelete, is whole parent/children form valid.

I rather use no statemanagement library and hack the code native instead of asking 1000 questions for every easy sample I can not find.

How to nest models correctly that I do not get Stack overflow exceptions? I got many of them!

If there is no easy access devs will dump that library!

rrousselGit commented 4 years ago

I'll close this as I do not have the time to make complete app samples. Also, there are many examples available on medium, youtube, udemy...

I know this is not what you would like to hear, but that's just not my thing and I'm working on my free time.

But I would encourage people to make pull requests to add their articles to the README or add examples.

jonataswalker commented 4 years ago

Not a finished (nor will be) app but hopefully can help as a start.

https://github.com/jonataswalker/flutter-example