Reputation: 491
Problem: I have a future builder inside a Column which is inside a SingleChildScrollView
It calls its future multiple times(Infinite).
FutureBuilder(
future: ProductLoaderUtil.getSimilarProducts(
context, widget.productItem.productCategoryId),
builder: (context, val) {
if (val.hasData && (!val.hasError))
return ListProductHorizontal(
productItemsHorizontal: val.data,
flag: false,
);
else
return Container(
height: _height * 0.06,
color: FakeWhite,
);
},
),
The function getting called is this,
static Future<List<ProductItem>> getSimilarProducts(
BuildContext context, String category) async {
List<ProductItem> products = [];
String categoryName = categories[0];
debugPrint(categoryName + " --- ");
await Firestore.instance
.collection("GlobalDataBase")
.document(categoryName)
.collection("ITEMS")
.limit(4)
.getDocuments()
.then((value) {
value.documents.forEach((element) {
// debugPrint(element.data.toString());
products.add(ProductItem.fromJson(element.data));
});
});
return products;
}
Upvotes: 0
Views: 991
Reputation: 963
that's probably because somewhere you use setState()
and when you that your widget tree get re-build and the FutureBuilder
call the future
method again, to prevent this gain access to the future method in initState()
by having a stateful widget like this
// inside state class...
Future getPopluarProductsFuture;
@override
void initState() {
super.initState();
getPopularProductsFuture = getPopularProducts(
context, widget.productItem.productCategoryId);
}
// now for the FutureBuilder make it like this
FutureBuilder(
future: getPopularProductsFuture,
builder: ......
)
Upvotes: 1