Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:cloud_firestore/cloud_firestore.dart';
- import 'package:firebaser/student.dart';
- import 'package:flutter/material.dart';
- import 'package:firebase_core/firebase_core.dart';
- import 'package:firebaser/firebase_options.dart';
- void main() async {
- WidgetsFlutterBinding.ensureInitialized();
- await Firebase.initializeApp(
- options: DefaultFirebaseOptions.currentPlatform,
- );
- runApp(MaterialApp(
- theme: ThemeData(
- brightness: Brightness.light,
- primaryColor: Colors.blue,
- colorScheme: ColorScheme.fromSwatch().copyWith(secondary: Colors.cyan),
- ),
- home: const MyApp(),
- ));
- }
- class MyApp extends StatefulWidget {
- const MyApp({super.key});
- @override
- State<MyApp> createState() => _MyAppState();
- }
- class _MyAppState extends State<MyApp> {
- TextEditingController ct1 = TextEditingController();
- TextEditingController ct2 = TextEditingController();
- TextEditingController ct3 = TextEditingController();
- TextEditingController ct4 = TextEditingController();
- CollectionReference cref = FirebaseFirestore.instance.collection("Students");
- void enterData(TextEditingController ct1, TextEditingController ct2,
- TextEditingController ct3, TextEditingController ct4) {
- String n = ct1.text;
- String i = ct2.text;
- String c = ct3.text;
- double g = double.parse(ct4.text);
- Map<String, dynamic> map = {
- "Name": n,
- "ID": i,
- "Course": c,
- "GPA": g,
- };
- cref.doc(i).set(map);
- }
- void getData(TextEditingController ct1, TextEditingController ct2,
- TextEditingController ct3, TextEditingController ct4) async {
- final element = await cref.doc(ct2.text).get();
- ct1.text = element["Name"];
- ct2.text = element["ID"].toString();
- ct3.text = element["Course"];
- ct4.text = element["GPA"].toString();
- }
- void updateData(TextEditingController ct1, TextEditingController ct2,
- TextEditingController ct3, TextEditingController ct4) async {
- String n = ct1.text;
- String i = ct2.text;
- String c = ct3.text;
- double g = double.parse(ct4.text);
- Map<String, dynamic> map = {
- "Name": n,
- "ID": i,
- "Course": c,
- "GPA": g,
- };
- cref.doc(i).set(map);
- }
- void deleteData(TextEditingController ct1, TextEditingController ct2,
- TextEditingController ct3, TextEditingController ct4) async {
- await cref.doc(ct2.text).delete();
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: const Text("Airbase inc."),
- ),
- body: Column(
- children: <Widget>[
- Padding(
- padding: const EdgeInsets.all(8.0),
- child: TextFormField(
- controller: ct1,
- decoration: const InputDecoration(
- labelText: "Student Name",
- fillColor: Colors.white,
- focusedBorder: OutlineInputBorder(
- borderSide: BorderSide(color: Colors.blue, width: 2.0))),
- onChanged: (String str) {},
- ),
- ),
- Padding(
- padding: const EdgeInsets.all(8.0),
- child: TextFormField(
- controller: ct2,
- decoration: const InputDecoration(
- labelText: "Student ID",
- fillColor: Colors.white,
- focusedBorder: OutlineInputBorder(
- borderSide: BorderSide(color: Colors.blue, width: 2.0))),
- onChanged: (String str) {},
- ),
- ), //
- Padding(
- padding: const EdgeInsets.all(8.0),
- child: TextFormField(
- controller: ct3,
- decoration: const InputDecoration(
- labelText: "Course",
- fillColor: Colors.white,
- focusedBorder: OutlineInputBorder(
- borderSide: BorderSide(color: Colors.blue, width: 2.0))),
- onChanged: (String str) {},
- ),
- ),
- Padding(
- padding: const EdgeInsets.all(8.0),
- child: TextFormField(
- controller: ct4,
- decoration: const InputDecoration(
- labelText: "GPA",
- fillColor: Colors.white,
- focusedBorder: OutlineInputBorder(
- borderSide: BorderSide(color: Colors.blue, width: 2.0))),
- onChanged: (String str) {},
- ),
- ), //
- Row(
- mainAxisAlignment: MainAxisAlignment.spaceAround,
- children: <Widget>[
- ElevatedButton(
- onPressed: () {
- setState(() {
- getData(ct1, ct2, ct3, ct4);
- });
- },
- child: const Text("Read"),
- ),
- ElevatedButton(
- onPressed: () {
- enterData(ct1, ct2, ct3, ct4);
- },
- child: const Text("Create"),
- ),
- ElevatedButton(
- onPressed: () {
- updateData(ct1, ct2, ct3, ct4);
- },
- child: const Text("Update"),
- ),
- ElevatedButton(
- onPressed: () {
- // enterData(ct1, ct2, ct3, ct4);
- deleteData(ct1, ct2, ct3, ct4);
- },
- child: const Text("Delete"),
- ),
- ],
- )
- ],
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement