kanwar manraj
kanwar manraj

Reputation: 552

Which is better method to expose an object , by using Provider package or , creating an object

We can expose an object of a class by two methods like:

ClassName obj=Classname(); or obj=Provider.of<ClassName>(context);

is there any difference between them , or is there anyone of them better method.

Upvotes: 2

Views: 668

Answers (3)

madhumohan adiki
madhumohan adiki

Reputation: 35

listening is not possible with normal object creation where as with provider it is possible.

obj=Provider.of(context, listen:true);

Upvotes: 0

nvoigt
nvoigt

Reputation: 77285

ClassName obj = Classname(); 

This is creating a new instance of Classname. In Dart, you can omit the new keyword (since v2.0), older versions and most other languages actually force you to spell it out:

ClassName obj = new Classname(); 

It will call the constructor of the class and create a new instance. Alternatives would be named constructors that could look like this:

ClassName obj = Classname.fromInt(42); 

That said, what exactly is this and what is the difference:

obj = Provider.of(context);

A provider is a form of state management. State management is a complex way of saying "where do I actually call my constructors so that the instances are known to the program at the place and time I need them? Sometimes I want a new instance, sometimes I want the instance I used before."

A provider may create a new instance for you. It may also decide it already has the instance you are looking for. You decide that by configuring it.

The only way to create a new instance of a class is through one of it's constructors. Very likely (but configurable), a provider is using a class constructor to create the instance of a class that it is then providing to multiple layers of your program so you don't have to keep track of that variable yourself.

Keeping track of all your variables and their lifetimes by yourself gets complicated really fast the bigger your program gets.

My personal recommendation to everyone learning programming is: try it the way you already know (in this case: constructors). Then you will experience for yourself what the problem is and you will know why packages like provider or bloc were created. This is a much better learning experience than just believing a random person on the internet (me or someone else) who says they know it's "better". Because then you will understand the problem instead of being railroaded into some cargo cult of "use this, it's good for you".

Upvotes: 1

hisam
hisam

Reputation: 1609

welcome to the StackOverflow.

You can do both of them, but if you are using the Provider package, you have some benefits:

  1. It is much easier to transfer state to another level (or even really far level) inside your app's tree.
  2. It is really suitable for a large scale app to manage their state (but it's also suitable for the small app).

If you are passing a state or an object directly, you'll be completely in a mess when your app complexity grows (based on my experience).

I hope it will be helpful.

Upvotes: 0

Related Questions