custom_fake_store_api_co 0.0.1
custom_fake_store_api_co: ^0.0.1 copied to clipboard
This is a custom fake store api project.
example/lib/main.dart
import 'package:custom_fake_store_api_co/custom_fake_store_api_co.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: ProductListScreen(),
);
}
}
class ProductListScreen extends StatefulWidget {
@override
_ProductListScreenState createState() => _ProductListScreenState();
}
class _ProductListScreenState extends State<ProductListScreen> {
late Future<List<ProductModel>> _futureProducts;
final CustomFakeStoreApiCo _api = CustomFakeStoreApiCo();
@override
void initState() {
super.initState();
_futureProducts = _api.getProducts();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Fake Store API Example'),
),
body: FutureBuilder<List<ProductModel>>(
future: _futureProducts,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
} else if (snapshot.hasError) {
return Center(child: Text('Error: ${snapshot.error}'));
} else if (!snapshot.hasData || snapshot.data!.isEmpty) {
return Center(child: Text('No products found'));
} else {
final products = snapshot.data!;
return ListView.builder(
itemCount: products.length,
itemBuilder: (context, index) {
final product = products[index];
return ListTile(
title: Text(product.title),
subtitle: Text('\$${product.price}'),
leading: Image.network(product.image),
);
},
);
}
},
),
);
}
}