An e-commerce flutter application.
- Shopping Cart & Watchlist tracking
- User Login and registration support
- Reviewing the products
- Different views
- Dynamic search mechanism
- Build list views on demand - less memory
Using
ListView.builder()
instead ofListView()
default constructor Example: More-memory consuming code
Widget _listOfProducts() {
return ListView(
padding: EdgeInsets.symmetric(vertical: 8.0),
children: //Check if the list is empty
Provider.of<ShoppingCart>(context).shoppingItemQuantites.isNotEmpty
? Provider.of<ShoppingCart>(context)
.shoppingItemQuantites
.keys
.map((e) {
return ShoppingCartItem(productId: e);
}).toList()
: []);
}
Less-memory consuming code
Widget _listOfProducts() {
return ListView.builder(
padding: EdgeInsets.symmetric(vertical: 8.0),
// Get items count using Provider
itemCount: Provider.of<ShoppingCart>(context)
.shoppingItemQuantites
.keys
.length,
itemBuilder: (context, index) {
return ShoppingCartItem(
productId: Provider.of<ShoppingCart>(context)
.shoppingItemQuantites
.keys
.elementAt(index));
});
}
- Future code organization
Using
FutureBuilder
instead of setting the state manually Example: Manual state management
String _vendor = 'Vendor not provided';
Widget _vendorCard() {
_getVendorName();
return Container(
padding: const EdgeInsets.all(4),
margin: const EdgeInsets.only(bottom: 2),
color: Colors.white,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [Text('Vendor:')],
),
Row(
children: [Text(_vendor)],
)
],
),
);
}
Future<void> _getVendorName() async {
var _result = await Provider.of<VendorsList>(context).getVendorNameByUid(
Provider.of<Catalog>(context).productsCatalog[widget.productId].vendor);
_vendor = _result;
}
Future state managemet
Widget _vendorText() {
return FutureBuilder(
future: Provider.of<VendorsList>(context).getVendorNameByUid(
Provider.of<Catalog>(context)
.productsCatalog[widget.productId]
.vendor),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(snapshot.data,
style: TextStyle(fontWeight: FontWeight.bold));
} else {
return Text("Vendor isn't provieded");
}
});
}