flutter_faker_plus
A comprehensive fake data generator for Flutter/Dart applications.
๐ Features
- User Profiles: Generate random user profiles with:
- Name
- Phone number
- Address
- Date of birth
- Gender
- Profile picture URL
- Bio
- E-commerce Products: Create random product data including:
- Product name
- Price
- Category
- Geolocation: Generate random latitude and longitude coordinates.
- Lorem Ipsum: Added Lorem Ipsum placeholder text.
- Future Plans:
- Add image URLs for e-commerce products.
- Support additional fake data types.
- Implement a fake QR code generator.
Flutter Faker Data Generator Example
This example demonstrates how to generate fake user, product, and location data using the flutter_faker_plus library in Flutter.
void main() {
WidgetsFlutterBinding.ensureInitialized();
// Register all mock routes
setupMockServer();
runApp(const FakeDataApp());
}
class FakeDataApp extends StatelessWidget {
const FakeDataApp({super.key});
@override
Widget build(BuildContext context) {
// Generate fake user, product, and location data using flutter_faker_plus.
final UserProfile user =
UserProfile.generate(); // Generate a fake user profile.
final EcommerceProduct product =
EcommerceProduct.generate(); // Generate a fake e-commerce product.
final GeoLocation location =
GeoLocation.generate(); // Generate a fake geo-location.
final LoremIpsum loremIpsum = LoremIpsum();
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Fake Data Generator Example'),
),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('User Profile:', style: TextStyle(fontSize: 20)),
// Displays a heading for the user profile section.
Text('Name: ${user.name}'),
// Displays the generated user's name.
Text('Email: ${user.email}'),
// Displays the generated user's email.
Text('Phone: ${user.phoneNumber}'),
// Displays the generated user's phone number.
Text('Address: ${user.address}'),
// Displays the generated user's address.
Text('Date of Birth: ${user.dateOfBirth}'),
// Displays the generated user's date of birth.
Text('Gender: ${user.gender}'),
// Displays the generated user's gender.
Image.network(user.profilePictureUrl),
// Displays the user's profile picture from a network URL.
Text('Bio: ${user.bio}'),
// Displays the generated user's bio.
SizedBox(height: 20),
// Adds vertical spacing of 20 pixels.
Text('E-Commerce Product:', style: TextStyle(fontSize: 20)),
// Displays a heading for the product section.
Text('Product: ${product.name}'),
// Displays the product name.
Text('Category: ${product.category}'),
// Displays the product category.
Text('Price: \$${product.price}'),
// Displays the product price prefixed with a dollar sign.
SizedBox(height: 20),
// Adds vertical spacing of 20 pixels.
Text('Geo-Location:', style: TextStyle(fontSize: 20)),
// Displays a heading for the geo-location section.
Text('Latitude: ${location.latitude}'),
// Displays the generated latitude.
Text('Longitude: ${location.longitude}'),
// Displays the generated longitude.
SizedBox(height: 20),
// Adds vertical spacing of 20 pixels.
Text('Lorem Ipsum:', style: TextStyle(fontSize: 20)),
Text(
loremIpsum.generate(words: 20),
),
SizedBox(height: 5),
Text(
loremIpsum.generate(
words: 60, paragraphs: 3, showLoremIpsum: true),
),
SizedBox(
height: 600,
child: UserPage())
],
),
),
),
),
);
}
}
๐งช Mock API Requests
flutter_faker_plus comes with a mock backend server for testing without a real API.
- Setup Mock Server
import 'package:flutter/material.dart';
import 'mock_server.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
setupMockServer(); // Register all mock routes
runApp(const FakeDataApp());
}
- ApiService Example
import 'mock_server.dart';
class ApiService {
final _client = MockHttpClient(mockApi);
Future<List<Map<String, dynamic>>> getUsers() async {
final res = await _client.request(method: HttpMethod.get, path: '/users');
return (res.data as List).map((e) => Map<String, dynamic>.from(e)).toList();
}
Future<Map<String, dynamic>> createUser(Map<String, dynamic> payload) async {
final res = await _client.request(
method: HttpMethod.post,
path: '/users',
body: payload,
);
return Map<String, dynamic>.from(res.data);
}
}
- Using Mock Requests in Widgets
final api = ApiService();
FutureBuilder<List<Map<String, dynamic>>>(
future: api.getUsers(),
builder: (context, snapshot) {
if (!snapshot.hasData) return CircularProgressIndicator();
final users = snapshot.data!;
return ListView.builder(
itemCount: users.length,
itemBuilder: (_, i) {
final u = users[i];
return ListTile(
title: Text(u['name'] ?? 'No Name'),
subtitle: Text(u['email'] ?? 'noemail@domain.com'),
);
},
);
},
);
- Create New User
await api.createUser({
'name': 'John Doe',
'email': 'john.doe@example.com',
});
๐ Installation
Add flutter_faker_plus to your pubspec.yaml file:
dependencies:
flutter_faker_plus: ^2.0.0