Skip to content

Instantly share code, notes, and snippets.

@amlsampath
Created June 13, 2025 10:38
Show Gist options
  • Select an option

  • Save amlsampath/84b91b52a04758ea8b67b213f33c4f5c to your computer and use it in GitHub Desktop.

Select an option

Save amlsampath/84b91b52a04758ea8b67b213f33c4f5c to your computer and use it in GitHub Desktop.
Assets Service
import 'package:cloud_firestore/cloud_firestore.dart';
class AssetService {
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
final String collectionName = 'assets';
// Sample data
final List<Map<String, dynamic>> initialData = [
{
"id": "asset001",
"title": "Mountain Lake Reflection",
"description": "Serene mountain lake with perfect reflection.",
"category": "Wallpapers",
"price": 250,
"currency": "LKR",
"previewUrl":
"https://images.pexels.com/photos/2662116/pexels-photo-2662116.jpeg",
"downloadUrl":
"https://images.pexels.com/photos/2662116/pexels-photo-2662116.jpeg",
"createdAt": "2025-06-01T12:00:00Z"
},
{
"id": "asset002",
"title": "Waterfall Paradise",
"description": "Majestic waterfall in tropical forest.",
"category": "Wallpapers",
"price": 300,
"currency": "LKR",
"previewUrl":
"https://images.pexels.com/photos/2440021/pexels-phocto-2440021.jpeg",
"downloadUrl":
"https://images.pexels.com/photos/2440021/pexels-phocto-2440021.jpeg",
"createdAt": "2025-06-02T09:00:00Z"
},
{
"id": "asset003",
"title": "Autumn Forest Path",
"description": "Golden autumn leaves on forest trail.",
"category": "Wallpapers",
"price": 220,
"currency": "LKR",
"previewUrl":
"https://images.pexels.com/photos/33109/fall-autumn-red-season.jpg",
"downloadUrl":
"https://images.pexels.com/photos/33109/fall-autumn-red-season.jpg",
"createdAt": "2025-06-02T10:00:00Z"
},
{
"id": "asset004",
"title": "Coastal Sunset",
"description": "Breathtaking sunset over ocean waves.",
"category": "Wallpapers",
"price": 350,
"currency": "LKR",
"previewUrl":
"https://images.pexels.com/photos/1005417/pexels-photo-1005417.jpeg",
"downloadUrl":
"https://images.pexels.com/photos/1005417/pexels-photo-1005417.jpeg",
"createdAt": "2025-06-03T08:00:00Z"
},
{
"id": "asset005",
"title": "Wildlife Icon Set",
"description": "Nature-themed icons featuring animals and plants.",
"category": "Icons",
"price": 350,
"currency": "LKR",
"previewUrl":
"https://images.pexels.com/photos/145939/pexels-photo-145939.jpeg",
"downloadUrl":
"https://images.pexels.com/photos/145939/pexels-photo-145939.jpeg",
"createdAt": "2025-06-03T09:30:00Z"
},
{
"id": "asset006",
"title": "Forest Pattern Pack",
"description": "Seamless forest and leaf patterns.",
"category": "Patterns",
"price": 400,
"currency": "LKR",
"previewUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"downloadUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"createdAt": "2025-06-04T10:00:00Z"
},
{
"id": "asset007",
"title": "Nature Presentation Template",
"description": "Eco-friendly PowerPoint template.",
"category": "Templates",
"price": 600,
"currency": "LKR",
"previewUrl":
"https://images.pexels.com/photos/417074/pexels-photo-417074.jpeg",
"downloadUrl":
"https://images.pexels.com/photos/417074/pexels-photo-417074.jpeg",
"createdAt": "2025-06-04T11:00:00Z"
},
{
"id": "asset008",
"title": "Mountain Range Panorama",
"description": "Stunning mountain range at dawn.",
"category": "Wallpapers",
"price": 380,
"currency": "LKR",
"previewUrl":
"https://images.pexels.com/photos/417173/pexels-photo-417173.jpeg",
"downloadUrl":
"https://images.pexels.com/photos/417173/pexels-photo-417173.jpeg",
"createdAt": "2025-06-05T08:00:00Z"
},
{
"id": "asset009",
"title": "Nature Photography Templates",
"description": "Instagram templates for nature photos.",
"category": "Templates",
"price": 500,
"currency": "LKR",
"previewUrl":
"https://images.pexels.com/photos/346885/pexels-photo-346885.jpeg",
"downloadUrl":
"https://images.pexels.com/photos/346885/pexels-photo-346885.jpeg",
"createdAt": "2025-06-05T09:00:00Z"
},
{
"id": "asset010",
"title": "Forest Animal Stickers",
"description": "Cute forest animal stickers.",
"category": "Stickers",
"price": 150,
"currency": "LKR",
"previewUrl":
"https://images.pexels.com/photos/247376/pexels-photo-247376.jpeg",
"downloadUrl":
"https://images.pexels.com/photos/247376/pexels-photo-247376.jpeg",
"createdAt": "2025-06-06T10:00:00Z"
},
{
"id": "asset011",
"title": "Nature UI Icon Set",
"description": "Eco-friendly icons for nature apps.",
"category": "Icons",
"price": 320,
"currency": "LKR",
"previewUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"downloadUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"createdAt": "2025-06-06T11:00:00Z"
},
{
"id": "asset012",
"title": "Wildflower Meadow",
"description": "Colorful wildflower field wallpaper.",
"category": "Wallpapers",
"price": 210,
"currency": "LKR",
"previewUrl":
"https://images.pexels.com/photos/1366630/pexels-photo-1366630.jpeg",
"downloadUrl":
"https://images.pexels.com/photos/1366630/pexels-photo-1366630.jpeg",
"createdAt": "2025-06-07T08:00:00Z"
},
{
"id": "asset013",
"title": "Eco Business Card",
"description": "Nature-inspired business card template.",
"category": "Templates",
"price": 250,
"currency": "LKR",
"previewUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"downloadUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"createdAt": "2025-06-07T09:00:00Z"
},
{
"id": "asset014",
"title": "Leaf Pattern Pack",
"description": "Organic leaf and nature patterns.",
"category": "Patterns",
"price": 400,
"currency": "LKR",
"previewUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"downloadUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"createdAt": "2025-06-08T10:00:00Z"
},
{
"id": "asset015",
"title": "Forest Ambience Sound Pack",
"description": "Peaceful forest sounds and birdsong.",
"category": "Audio",
"price": 500,
"currency": "LKR",
"previewUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"downloadUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"createdAt": "2025-06-08T11:00:00Z"
},
{
"id": "asset016",
"title": "Nature App UI Kit",
"description": "Organic nature-themed app UI kit.",
"category": "UI Kits",
"price": 800,
"currency": "LKR",
"previewUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"downloadUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"createdAt": "2025-06-09T08:00:00Z"
},
{
"id": "asset017",
"title": "Watercolor Nature Clipart",
"description": "Hand-painted nature watercolor clipart.",
"category": "Clipart",
"price": 360,
"currency": "LKR",
"previewUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"downloadUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"createdAt": "2025-06-09T09:00:00Z"
},
{
"id": "asset018",
"title": "Mountain Stream",
"description": "Crystal clear mountain stream wallpaper.",
"category": "Wallpapers",
"price": 270,
"currency": "LKR",
"previewUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"downloadUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"createdAt": "2025-06-10T10:00:00Z"
},
{
"id": "asset019",
"title": "Botanical Pattern",
"description": "Elegant botanical seamless pattern.",
"category": "Patterns",
"price": 300,
"currency": "LKR",
"previewUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"downloadUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"createdAt": "2025-06-10T11:00:00Z"
},
{
"id": "asset020",
"title": "Nature Highlights Icons",
"description": "Nature-themed Instagram story icons.",
"category": "Icons",
"price": 210,
"currency": "LKR",
"previewUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"downloadUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"createdAt": "2025-06-11T08:00:00Z"
},
{
"id": "asset021",
"title": "Northern Lights Forest",
"description": "Aurora borealis over snowy forest.",
"category": "Wallpapers",
"price": 270,
"currency": "LKR",
"previewUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"downloadUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"createdAt": "2025-06-11T09:00:00Z"
},
{
"id": "asset022",
"title": "Eco Resume Template",
"description": "Nature-inspired resume template.",
"category": "Templates",
"price": 400,
"currency": "LKR",
"previewUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"downloadUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"createdAt": "2025-06-12T08:00:00Z"
},
{
"id": "asset023",
"title": "Wildlife Avatar Icons",
"description": "Nature animal avatars for profiles.",
"category": "Icons",
"price": 250,
"currency": "LKR",
"previewUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"downloadUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"createdAt": "2025-06-12T09:00:00Z"
},
{
"id": "asset024",
"title": "Misty Forest Morning",
"description": "Mystical morning fog in forest.",
"category": "Wallpapers",
"price": 200,
"currency": "LKR",
"previewUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"downloadUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"createdAt": "2025-06-13T08:00:00Z"
},
{
"id": "asset025",
"title": "Garden Wedding Invitation",
"description": "Botanical wedding invitation template.",
"category": "Templates",
"price": 500,
"currency": "LKR",
"previewUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"downloadUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"createdAt": "2025-06-13T09:00:00Z"
},
{
"id": "asset026",
"title": "Forest Animal Clipart",
"description": "Cute forest animal clipart set.",
"category": "Clipart",
"price": 180,
"currency": "LKR",
"previewUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"downloadUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"createdAt": "2025-06-14T08:00:00Z"
},
{
"id": "asset027",
"title": "Spring Blossoms",
"description": "Cherry blossom tree wallpaper.",
"category": "Wallpapers",
"price": 250,
"currency": "LKR",
"previewUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"downloadUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"createdAt": "2025-06-14T09:00:00Z"
},
{
"id": "asset028",
"title": "Nature App Glassmorphism UI",
"description": "Eco-friendly glassmorphism UI kit.",
"category": "UI Kits",
"price": 900,
"currency": "LKR",
"previewUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"downloadUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"createdAt": "2025-06-15T08:00:00Z"
},
{
"id": "asset029",
"title": "Nature Font Collection",
"description": "Organic and natural handwritten fonts.",
"category": "Fonts",
"price": 600,
"currency": "LKR",
"previewUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"downloadUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"createdAt": "2025-06-15T09:00:00Z"
},
{
"id": "asset030",
"title": "Forest Friends Stickers",
"description": "Cute forest animal stickers.",
"category": "Stickers",
"price": 150,
"currency": "LKR",
"previewUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"downloadUrl":
"https://images.pexels.com/photos/235986/pexels-photo-235986.jpeg",
"createdAt": "2025-06-16T08:00:00Z"
}
];
// Insert if empty
Future<void> insertAssetsIfEmpty() async {
final snapshot = await _firestore.collection(collectionName).get();
if (snapshot.docs.isEmpty) {
await createCategoriesCollection();
for (var item in initialData) {
final asset = AssetModel.fromMap(item);
await _firestore
.collection(collectionName)
.doc(asset.id)
.set(asset.toMap());
}
print('Assets inserted successfully.');
} else {
print('Assets collection already contains data. Skipping insert.');
}
}
// Create categories collection from initialData
Future<void> createCategoriesCollection() async {
final Set<String> categories =
initialData.map((item) => item['category'] as String).toSet();
final categoriesCollection = _firestore.collection('categories');
for (final category in categories) {
await categoriesCollection
.doc(category.toLowerCase().replaceAll(' ', '_'))
.set({
'id': category.toLowerCase().replaceAll(' ', '_'),
'name': category,
});
}
print('Categories collection created/updated successfully.');
}
}
class AssetModel {
final String id;
final String title;
final String description;
final String category;
final int price;
final String currency;
final String previewUrl;
final String downloadUrl;
final DateTime createdAt;
AssetModel({
required this.id,
required this.title,
required this.description,
required this.category,
required this.price,
required this.currency,
required this.previewUrl,
required this.downloadUrl,
required this.createdAt,
});
Map<String, dynamic> toMap() {
return {
'id': id,
'title': title,
'description': description,
'category': category,
'price': price,
'currency': currency,
'previewUrl': previewUrl,
'downloadUrl': downloadUrl,
'createdAt': createdAt.toUtc(),
};
}
factory AssetModel.fromMap(Map<String, dynamic> map) {
return AssetModel(
id: map['id'],
title: map['title'],
description: map['description'],
category: map['category'],
price: map['price'],
currency: map['currency'],
previewUrl: map['previewUrl'],
downloadUrl: map['downloadUrl'],
createdAt: DateTime.parse(map['createdAt']),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment