Advertisement
anik11556

deparment sub collection

Jan 3rd, 2025
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.87 KB | None | 0 0
  1. import 'package:cloud_firestore/cloud_firestore.dart';
  2. import 'package:firebase_core/firebase_core.dart';
  3. import 'package:flutter/material.dart';
  4.  
  5. void main() async {
  6.   WidgetsFlutterBinding.ensureInitialized();
  7.   await Firebase.initializeApp();
  8.   runApp(MyApp());
  9. }
  10.  
  11. class MyApp extends StatelessWidget {
  12.   @override
  13.   Widget build(BuildContext context) {
  14.     return MaterialApp(
  15.       title: 'Add Subcollections to Existing Departments',
  16.       home: AddSubcollectionsPage(),
  17.     );
  18.   }
  19. }
  20.  
  21. class AddSubcollectionsPage extends StatelessWidget {
  22.   // List of department names (existing documents in 'departments' collection)
  23.   final List<String> collectionsToDelete = [
  24.     'Biology',
  25.     'Chemistry',
  26.     'Computer Science and Engineering',
  27.     'Electrical and Electronic Engineering',
  28.     'Mathematics',
  29.     'Physics',
  30.   ];
  31.  
  32.   // List of subcollections for each department
  33.   final List<String> subCollections = [
  34.     '1st year',
  35.     '2nd year',
  36.     '3rd year',
  37.     '4th year',
  38.     'MS',
  39.     'Files',
  40.   ];
  41.  
  42.   // Method to add subcollections under each existing department
  43.   Future<void> addSubcollections() async {
  44.     try {
  45.       // Reference to the 'departments' collection
  46.       CollectionReference departments = FirebaseFirestore.instance.collection('departments');
  47.  
  48.       for (var department in collectionsToDelete) {
  49.         // Reference to the department document
  50.         DocumentReference departmentDoc = departments.doc(department);
  51.  
  52.         // Check if the department document exists
  53.         var docSnapshot = await departmentDoc.get();
  54.         if (docSnapshot.exists) {
  55.           // Create subcollections under the department
  56.           for (var subCollection in subCollections) {
  57.             // Add a document to each subcollection
  58.             await departmentDoc.collection(subCollection).doc('example').set({
  59.               'name': subCollection,
  60.               'createdAt': Timestamp.now(),
  61.             });
  62.             print('Subcollection $subCollection created under department $department.');
  63.           }
  64.         } else {
  65.           print('Department $department does not exist.');
  66.         }
  67.       }
  68.     } catch (e) {
  69.       print('Error adding subcollections: $e');
  70.     }
  71.   }
  72.  
  73.   @override
  74.   Widget build(BuildContext context) {
  75.     return Scaffold(
  76.       appBar: AppBar(
  77.         title: Text('Add Subcollections to Existing Departments'),
  78.       ),
  79.       body: Center(
  80.         child: Text('Press the button to add subcollections under each department'),
  81.       ),
  82.       floatingActionButton: FloatingActionButton(
  83.         onPressed: () async {
  84.           await addSubcollections();
  85.           ScaffoldMessenger.of(context).showSnackBar(
  86.             SnackBar(content: Text('Subcollections added to existing departments!')),
  87.           );
  88.         },
  89.         child: Icon(Icons.add),
  90.         tooltip: 'Add Subcollections',
  91.       ),
  92.     );
  93.   }
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement