CastelShal

Firestore thing

Jan 25th, 2024
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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: 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 = new TextEditingController();
  32.   TextEditingController ct2 = new TextEditingController();
  33.   TextEditingController ct3 = new TextEditingController();
  34.   TextEditingController ct4 = new 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
  52.         .add(map)
  53.         .then((value) => print("User added"))
  54.         .catchError((error) => print("Errrrrrrrrrr "));
  55.   }
  56.  
  57.   void getData(String doc, TextEditingController ct1, TextEditingController ct2,
  58.       TextEditingController ct3, TextEditingController ct4) {
  59.     cref.doc(doc).get().then((value) {
  60.       dynamic obj = value.data();
  61.       print(obj["Name"]);
  62.       print(obj["Course"]);
  63.       print(obj["ID"]);
  64.       print(obj["GPA"]);
  65.     });
  66.   }
  67.  
  68.   @override
  69.   Widget build(BuildContext context) {
  70.     return Scaffold(
  71.       appBar: AppBar(
  72.         title: const Text("Airbase inc."),
  73.       ),
  74.       body: Column(
  75.         children: <Widget>[
  76.           Padding(
  77.             padding: const EdgeInsets.all(8.0),
  78.             child: TextFormField(
  79.               controller: ct1,
  80.               decoration: const InputDecoration(
  81.                   labelText: "Student Name",
  82.                   fillColor: Colors.white,
  83.                   focusedBorder: OutlineInputBorder(
  84.                       borderSide: BorderSide(color: Colors.blue, width: 2.0))),
  85.               onChanged: (String str) {},
  86.             ),
  87.           ),
  88.           Padding(
  89.             padding: const EdgeInsets.all(8.0),
  90.             child: TextFormField(
  91.               controller: ct2,
  92.               decoration: const InputDecoration(
  93.                   labelText: "Student ID",
  94.                   fillColor: Colors.white,
  95.                   focusedBorder: OutlineInputBorder(
  96.                       borderSide: BorderSide(color: Colors.blue, width: 2.0))),
  97.               onChanged: (String str) {},
  98.             ),
  99.           ), //
  100.           Padding(
  101.             padding: const EdgeInsets.all(8.0),
  102.             child: TextFormField(
  103.               controller: ct3,
  104.               decoration: const InputDecoration(
  105.                   labelText: "Course",
  106.                   fillColor: Colors.white,
  107.                   focusedBorder: OutlineInputBorder(
  108.                       borderSide: BorderSide(color: Colors.blue, width: 2.0))),
  109.               onChanged: (String str) {},
  110.             ),
  111.           ),
  112.           Padding(
  113.             padding: const EdgeInsets.all(8.0),
  114.             child: TextFormField(
  115.               controller: ct4,
  116.               decoration: const InputDecoration(
  117.                   labelText: "GPA",
  118.                   fillColor: Colors.white,
  119.                   focusedBorder: OutlineInputBorder(
  120.                       borderSide: BorderSide(color: Colors.blue, width: 2.0))),
  121.               onChanged: (String str) {},
  122.             ),
  123.           ), //
  124.           Row(
  125.             mainAxisAlignment: MainAxisAlignment.center,
  126.             children: <Widget>[
  127.               ElevatedButton(
  128.                 onPressed: () {
  129.                   // enterData(ct1, ct2, ct3, ct4);
  130.                   getData("cTohq82nNzzrmzc32KR7", ct1, ct2, ct3, ct4);
  131.                 },
  132.                 child: Icon(Icons.memory_sharp),
  133.               )
  134.             ],
  135.           )
  136.         ],
  137.       ),
  138.     );
  139.   }
  140. }
  141.  
Add Comment
Please, Sign In to add comment