I have a screen (ProductAddScreen.dart) that tries to load data from firestore (products unpublished) but if the list is empty, I want to redirect to a new screen (ProductFormScreen.dart).
@override
Widget build(BuildContext context) {
return StreamBuilder<List<Product>>(
stream: context.watch<ProductService>().unpublished(),
initialData: [],
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Loading(color: Colors.green);
}
// ════════ Exception caught by widgets library ════════
// setState() or markNeedsBuild() called during build.
if (!snapshot.hasData) {
Navigator.pushReplacement(context, MaterialPageRoute(builder: (_) => ProductFormScreen()));
}
return Scaffold(
appBar: AppBar(
title: Text(Strings.productAddAppBarTitle),
),
body: ListView.separated(
itemBuilder: (context, index) {
final product = snapshot.data[index];
return ProductItemRow(
product: snapshot.data[index],
onTap: () => print('hello'),
);
},
separatorBuilder: (context, index) => Divider(height: 0),
itemCount: snapshot.data.length,
),
);
},
);
}
I come from react js and I think I am confused. How can I do this with Flutter?