When we call ref.read(someProviderWithArgs.notifier).performSideEffect()
, can we use any value of arguments to do that?
Example: Given a provider below with arguments, does it make a difference
- If a provider is alive or not. Eg.
ref.read(FetchProductsProvider(productId: null, clientId: null).notifier).updateProductStatus(pid, true)
whereFetchProductsProvider(productId: null, clientId: null)
might have been disposed-off. - Will it make a network request if not alive and perform a side effect?
- Provider is alive with other instances also alive?
ref.invalidate(providerWithSpecificArgs)
line inside side effect method will take care of rebuilding that specific widget because it is being watched by widget where the request is performed?
In my case, I have many instances of this provider initiated, some get disposed off depending on what I’m doing on app. Whats the correct way to call a method in such case?
<code>@riverpod
class FetchProducts extends _$FetchProducts {
@override
Future<List<Product>> build({
required String? productId,
required String? clientId,
}) async {
if (productId != null) {
// Network request-1
} else if (clientId != null && clientId != '') {
// Network request-2
}
else {
// Network request-3
}
}
Future<void> updateProductStatus(String pid, bool inProd) async {
final db = FirebaseFirestore.instance;
String pdtRefId;
await db
.collection('product')
.where('pid', isEqualTo: pid)
.get()
.then((QuerySnapshot<Map<String, dynamic>> value) {
pdtRefId = value.docs.first.id;
final bool? selectPdtInProd = value.docs.first.data()["inProd"];
if (pdtRefId.isNotEmpty && selectPdtInProd != inProd) {
db.collection('product').doc(pdtRefId).update({"inProd": inProd});
}
});
ref.invalidate(FetchProductsProvider(specificProductId: null, clientUid: null));
await future;
}
}
</code>
<code>@riverpod
class FetchProducts extends _$FetchProducts {
@override
Future<List<Product>> build({
required String? productId,
required String? clientId,
}) async {
if (productId != null) {
// Network request-1
} else if (clientId != null && clientId != '') {
// Network request-2
}
else {
// Network request-3
}
}
Future<void> updateProductStatus(String pid, bool inProd) async {
final db = FirebaseFirestore.instance;
String pdtRefId;
await db
.collection('product')
.where('pid', isEqualTo: pid)
.get()
.then((QuerySnapshot<Map<String, dynamic>> value) {
pdtRefId = value.docs.first.id;
final bool? selectPdtInProd = value.docs.first.data()["inProd"];
if (pdtRefId.isNotEmpty && selectPdtInProd != inProd) {
db.collection('product').doc(pdtRefId).update({"inProd": inProd});
}
});
ref.invalidate(FetchProductsProvider(specificProductId: null, clientUid: null));
await future;
}
}
</code>
@riverpod
class FetchProducts extends _$FetchProducts {
@override
Future<List<Product>> build({
required String? productId,
required String? clientId,
}) async {
if (productId != null) {
// Network request-1
} else if (clientId != null && clientId != '') {
// Network request-2
}
else {
// Network request-3
}
}
Future<void> updateProductStatus(String pid, bool inProd) async {
final db = FirebaseFirestore.instance;
String pdtRefId;
await db
.collection('product')
.where('pid', isEqualTo: pid)
.get()
.then((QuerySnapshot<Map<String, dynamic>> value) {
pdtRefId = value.docs.first.id;
final bool? selectPdtInProd = value.docs.first.data()["inProd"];
if (pdtRefId.isNotEmpty && selectPdtInProd != inProd) {
db.collection('product').doc(pdtRefId).update({"inProd": inProd});
}
});
ref.invalidate(FetchProductsProvider(specificProductId: null, clientUid: null));
await future;
}
}