i have RefreshIndicator
and inside it ListView
so that RefreshIndicator
work swipe down gesture on ListView
, also i have set GlobalKey<RefreshIndicatorState>()
to RefreshIndicator
so that i can show indicator on button tap.
all is wokring but in _onRefresh
i am calling showSnackBar
but snakbar showing twice i dont know why
final _refreshIndicatorKey = GlobalKey<RefreshIndicatorState>();
@override
Widget build(BuildContext context) {
return RefreshIndicator(
key: _refreshIndicatorKey,
onRefresh: _onRefresh,
child: buidlListView(snapshot),
);
}
Widget buidlListView(AsyncSnapshot<List<Model>> snapshot) {
if (snapshot.hasData && snapshot.data!.isNotEmpty) {
return buildModelList(snapshot);
}
return buildNoModelList();
}
Widget buildNoModelList() {
return ListView(
children: [
ElevatedButton(
onPressed: _onRefresh,
child: const SizedBox(
width: double.infinity,
child: Text('Refresh', textAlign: TextAlign.center),
),
],
);
}
Widget buildModelList(
AsyncSnapshot<List<Model>> snapshot,
) {
final modelList = snapshot.data!;
return ListView.separated(
padding: const EdgeInsets.all(16),
itemCount: modelList.length,
separatorBuilder: (context, index) => const SizedBox(height: 16),
itemBuilder: (context, index) {
final model = modelList[index];
return ModelCard(
key: ValueKey(model),
model: model,
);
},
);
}
Future<void> _onRefresh() async {
// ignore: unawaited_futures
_refreshIndicatorKey.currentState?.show();
final isModelExist = await loadModels();
// not returning this because i want to show snack bar after indicator hides
Future<void>.delayed(const Duration(seconds: 3));
if (!isModelExist) {
showSnackBar();
}
return Future.value();
}
void showSnackBar() {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
behavior: SnackBarBehavior.floating,
content: Text('No Model Found'),
),
);
}
Future<bool> loadModels() async {
final snapshot = await modelCollectionRef().get();
if (snapshot.size > 0) {
//...
return true;
}
return false;
}
> snapshot**.
– kapil singh Jun 24 '22 at 06:33