I am using the ProviderObserver
class as per riverpod docs (provider_observer).
A provider fetchProductsProvider
is used as below in my code.
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:provider/provider.dart' as prov;
class Schedule extends ConsumerWidget {
final double parentEdgePadding;
const ScheduledDelivery({
super.key,
required this.parentEdgePadding,
});
@override
Widget build(BuildContext context, WidgetRef ref) {
final productList = ref.watch(fetchProductsProvider(clientUid: null, specificProductId: null));
print('NUM PRODUCTS = ${productList.value?.length}n');
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Schedule'),
const SizedBox(
height: 15.0,
),
prov.Consumer<List<Jobs>?>(
builder: (BuildContext context, List<Jobs>? jobs, _) {
return switch (productList) {
AsyncError(:final error) => Center(child: Text('Error: $error')),
AsyncData(:final value) => Builder(
builder: (context) {
//...
final output = manipulate(jobs, value);
//...
return Text('$output');
},
),
_ => CircularProgressIndicator()
};
},
),
],
);
}
double manipulate(List<Jobs> jobs, List<Product> products) {
// do calculations and return a double
}
}
This provider is being initialized twice and updated twice when widget is drawn.
Console output:
Provider fetchProductsProvider:FetchProductsProvider#1d349(null) was initialized with
AsyncLoading<List<Product>>()
NUM PRODUCTS = null
Provider fetchProductsProvider:FetchProductsProvider#1d349(null) was initialized with
AsyncLoading<List<Product>>()
NUM PRODUCTS = null
Provider fetchProductsProvider:FetchProductsProvider#1d349(null) updated from
AsyncLoading<List<Product>>() to AsyncData<List<Product>>(value: [Instance of 'Product',
Instance of 'Product', Instance of 'Product', Instance of 'Product', Instance of 'Product'])
Provider fetchProductsProvider:FetchProductsProvider#1d349(null) updated from
AsyncLoading<List<Product>>() to AsyncData<List<Product>>(value: [Instance of 'Product',
Instance of 'Product', Instance of 'Product', Instance of 'Product', Instance of 'Product'])
NUM PRODUCTS = 5
Why is it happening so? and is it causing my firebase reads twice?
The parent widget is a ConsumerStatefulWidget having below snippet of code.
Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(
height: 8.0,
),
Schedule(
parentEdgePadding: 80.0,
),
// .. other children
]
)