Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass data from one provider model to another?

I want use provider (ChangeNotifierProvider) and ChangeNotifier for manage app state. But how I can access state from one model in another model?

Use case: In chat app, one model for store user information. Other model use user information (for example user id) to make call to database (Firestore) and get stream of chat data.

For example:

class Model1 extends ChangeNotifier {
  final List<Item> items = [];

class Model2 extends ChangeNotifier {
//Access items from Model1 here
items;

Is this possible? I not like have very big Model because is hard to maintain.



Thanks!

like image 546
FlutterFirebase Avatar asked Jun 27 '19 20:06

FlutterFirebase


People also ask

Can I obtain two different providers using the same type?

Can I obtain two different providers using the same type? No. While you can have multiple providers sharing the same type, a widget will be able to obtain only one of them: the closest ancestor.

How to pass the value from one model to another?

If you want to pass the value to another model, which value should be passed? From my understanding, you just need to assign the value from the property " greencar " of the class " carone " to the property " redcar " of the class " cartwo ".

Can We pass data between screens without a changenotifierprovider?

Because we can pass data between screens without the latest Provider package, ChangeNotifierProvider, and ChangeNotifier. Let’s consider a simple data model where we have a Book class and a list of dummy data based on that Book class constructors.

How to pass data from one class to another class?

Because we pass data between classes through class constructor. The following code snippet handles that passage of data. itemBuilder: (ctx, i) => BookItem ( books [i].id, books [i].title, books [i].imageUrl, ), Now we can have a Book Item controller, where we can request that data through class constructor.

How to pass data from one sibling to another?

For passing data among siblings, there are multiple methods we can choose from as shown below: 1 Combination of the above two methods (callback and use of props). 2 Using Redux. 3 ContextAPI More ...


Video Answer


3 Answers

Using provider, one model doesn't access another model.

Instead, you should use ProxyProvider, to combine a model from others.

Your models would look like:

class Foo with ChangeNotifier {
  int count = 0;

  void increment() {
    count++;
    notifyListeners();
  }
}

class Bar with ChangeNotifier {
  int _count;
  int get count => _count;
  set count(int value) {
    if (value != count) {
      _count = value;
      notifyListeners();
    }
  }
}

And then you can use ChangeNotifierProxyProvider this way (assuming there's a `ChangeNotifierProvider higher in your widget tree):

ChangeNotifierProxyProvider<Foo, Bar>(
  initialBuilder: (_) => Bar(),
  builder: (_, foo, bar) => bar
    ..count = foo.count, // Don't pass `Foo` directly but `foo.count` instead
)
like image 53
Rémi Rousselet Avatar answered Oct 17 '22 16:10

Rémi Rousselet


From v4 the ChangeNotifierProxyProvider the provider can be constructed like so:

ChangeNotifierProxyProvider<Foo, Bar>(
  create: (_) => Bar(),
  update: (_, foo, bar) => bar
    ..count = foo.count,
)

Notice that initialBuilder: has changed to create: and builder: to update:

like image 36
Iain Smith Avatar answered Oct 17 '22 15:10

Iain Smith


I have tried to implement this simply.

Provider version ^4.3.3

Here i given a simple example

main.dart

ChangeNotifierProxyProvider<AuthProvider, ProductsProvider>(
       create: (context) => ProductsProvider(),
       update: (context, auth, previousProducts) => previousProducts
            ..update(auth.token, previousProducts.items == null ? [] : previousProducts.items),
),

authProvider.dart

class AuthProvider with ChangeNotifier {
  String _token;
  // _token is a response token.
  String get token {
    return _token;
}

productsProvider.dart

Product is a class of single product

class ProductsProvider with ChangeNotifier {
  List<Product> _items = [];
  String _authToken;
  void update(authToken, items) {
    _items = items;
    _authToken = authToken;
    notifyListeners();
  }
}

Product.dart

class Product with ChangeNotifier {
  final String id;
  final String title;
  final String description;
  final double price;
  final String imageUrl;
  bool isFavorite;

  Product({
    @required this.id,
    @required this.title,
    @required this.description,
    @required this.price,
    @required this.imageUrl,
    this.isFavorite = false,
  });
}
like image 1
Mijanur Rahman Avatar answered Oct 17 '22 17:10

Mijanur Rahman