Advertisement
CastelShal

Firebase Full

Feb 16th, 2024
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 5.56 KB | None | 0 0
  1. import 'package:cloud_firestore/cloud_firestore.dart';
  2. import 'package:firebaser/student.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:firebase_core/firebase_core.dart';
  5. import 'package:firebaser/firebase_options.dart';
  6.  
  7. void main() async {
  8.   WidgetsFlutterBinding.ensureInitialized();
  9.   await Firebase.initializeApp(
  10.     options: DefaultFirebaseOptions.currentPlatform,
  11.   );
  12.  
  13.   runApp(MaterialApp(
  14.     theme: ThemeData(
  15.       brightness: Brightness.light,
  16.       primaryColor: Colors.blue,
  17.       colorScheme: ColorScheme.fromSwatch().copyWith(secondary: Colors.cyan),
  18.     ),
  19.     home: const MyApp(),
  20.   ));
  21. }
  22.  
  23. class MyApp extends StatefulWidget {
  24.   const MyApp({super.key});
  25.  
  26.   @override
  27.   State<MyApp> createState() => _MyAppState();
  28. }
  29.  
  30. class _MyAppState extends State<MyApp> {
  31.   TextEditingController ct1 = TextEditingController();
  32.   TextEditingController ct2 = TextEditingController();
  33.   TextEditingController ct3 = TextEditingController();
  34.   TextEditingController ct4 = TextEditingController();
  35.   CollectionReference cref = FirebaseFirestore.instance.collection("Students");
  36.  
  37.   void enterData(TextEditingController ct1, TextEditingController ct2,
  38.       TextEditingController ct3, TextEditingController ct4) {
  39.     String n = ct1.text;
  40.     String i = ct2.text;
  41.     String c = ct3.text;
  42.     double g = double.parse(ct4.text);
  43.  
  44.     Map<String, dynamic> map = {
  45.       "Name": n,
  46.       "ID": i,
  47.       "Course": c,
  48.       "GPA": g,
  49.     };
  50.  
  51.     cref.doc(i).set(map);
  52.   }
  53.  
  54.   void getData(TextEditingController ct1, TextEditingController ct2,
  55.       TextEditingController ct3, TextEditingController ct4) async {
  56.     final element = await cref.doc(ct2.text).get();
  57.     ct1.text = element["Name"];
  58.     ct2.text = element["ID"].toString();
  59.     ct3.text = element["Course"];
  60.     ct4.text = element["GPA"].toString();
  61.   }
  62.  
  63.   void updateData(TextEditingController ct1, TextEditingController ct2,
  64.       TextEditingController ct3, TextEditingController ct4) async {
  65.     String n = ct1.text;
  66.     String i = ct2.text;
  67.     String c = ct3.text;
  68.     double g = double.parse(ct4.text);
  69.  
  70.     Map<String, dynamic> map = {
  71.       "Name": n,
  72.       "ID": i,
  73.       "Course": c,
  74.       "GPA": g,
  75.     };
  76.  
  77.     cref.doc(i).set(map);
  78.   }
  79.  
  80.   void deleteData(TextEditingController ct1, TextEditingController ct2,
  81.       TextEditingController ct3, TextEditingController ct4) async {
  82.     await cref.doc(ct2.text).delete();
  83.   }
  84.  
  85.   @override
  86.   Widget build(BuildContext context) {
  87.     return Scaffold(
  88.       appBar: AppBar(
  89.         title: const Text("Airbase inc."),
  90.       ),
  91.       body: Column(
  92.         children: <Widget>[
  93.           Padding(
  94.             padding: const EdgeInsets.all(8.0),
  95.             child: TextFormField(
  96.               controller: ct1,
  97.               decoration: const InputDecoration(
  98.                   labelText: "Student Name",
  99.                   fillColor: Colors.white,
  100.                   focusedBorder: OutlineInputBorder(
  101.                       borderSide: BorderSide(color: Colors.blue, width: 2.0))),
  102.               onChanged: (String str) {},
  103.             ),
  104.           ),
  105.           Padding(
  106.             padding: const EdgeInsets.all(8.0),
  107.             child: TextFormField(
  108.               controller: ct2,
  109.               decoration: const InputDecoration(
  110.                   labelText: "Student ID",
  111.                   fillColor: Colors.white,
  112.                   focusedBorder: OutlineInputBorder(
  113.                       borderSide: BorderSide(color: Colors.blue, width: 2.0))),
  114.               onChanged: (String str) {},
  115.             ),
  116.           ), //
  117.           Padding(
  118.             padding: const EdgeInsets.all(8.0),
  119.             child: TextFormField(
  120.               controller: ct3,
  121.               decoration: const InputDecoration(
  122.                   labelText: "Course",
  123.                   fillColor: Colors.white,
  124.                   focusedBorder: OutlineInputBorder(
  125.                       borderSide: BorderSide(color: Colors.blue, width: 2.0))),
  126.               onChanged: (String str) {},
  127.             ),
  128.           ),
  129.           Padding(
  130.             padding: const EdgeInsets.all(8.0),
  131.             child: TextFormField(
  132.               controller: ct4,
  133.               decoration: const InputDecoration(
  134.                   labelText: "GPA",
  135.                   fillColor: Colors.white,
  136.                   focusedBorder: OutlineInputBorder(
  137.                       borderSide: BorderSide(color: Colors.blue, width: 2.0))),
  138.               onChanged: (String str) {},
  139.             ),
  140.           ), //
  141.           Row(
  142.             mainAxisAlignment: MainAxisAlignment.spaceAround,
  143.             children: <Widget>[
  144.               ElevatedButton(
  145.                 onPressed: () {
  146.                   setState(() {
  147.                     getData(ct1, ct2, ct3, ct4);
  148.                   });
  149.                 },
  150.                 child: const Text("Read"),
  151.               ),
  152.               ElevatedButton(
  153.                 onPressed: () {
  154.                   enterData(ct1, ct2, ct3, ct4);
  155.                 },
  156.                 child: const Text("Create"),
  157.               ),
  158.               ElevatedButton(
  159.                 onPressed: () {
  160.                   updateData(ct1, ct2, ct3, ct4);
  161.                 },
  162.                 child: const Text("Update"),
  163.               ),
  164.               ElevatedButton(
  165.                 onPressed: () {
  166.                   // enterData(ct1, ct2, ct3, ct4);
  167.                   deleteData(ct1, ct2, ct3, ct4);
  168.                 },
  169.                 child: const Text("Delete"),
  170.               ),
  171.             ],
  172.           )
  173.         ],
  174.       ),
  175.     );
  176.   }
  177. }
  178.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement