i'm using switch case inside future builder (below) with connection state to view certain widgets in different connection states.
My question is, how can I refactor the switch case so i can use it in other places in the app?
Thanks..
code:
FutureBuilder(
future: _fetchProductsList,
builder: (context, AsyncSnapshot<List<ProductsList>> snapshot) {
switch(snapshot.connectionState){
case ConnectionState.none:
return const Text("There is no connection!!");
case ConnectionState.active:
case ConnectionState.waiting:
return const Center(child: CircularProgressIndicator());
case ConnectionState.done:
if(snapshot.hasData){
if (snapshot.data!.isNotEmpty){
List<ProductsList>? product = snapshot.data;
return ListView.builder(
primary: false,
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemCount: product!.length,
itemBuilder: (BuildContext context,int index){
return ProductCard(
img: product[index].image,
title: product[index].title,
price: product[index].price,
rating: product[index].rating.rate,
discount: 50,
onTap: () {}
);
}
);
}else {
// here your snapshot data is null, has no data...
return const Center(
child: Text(
"No Data Available Right Now!!"
)
);
}
}
}
// By default, show a loading spinner
return const Center(child: CircularProgressIndicator());
}
)