From 54b8505a1c229ca4b5fcc931c300be987995dec4 Mon Sep 17 00:00:00 2001 From: Pushkar Pawar <73992274+pushkarpawar@users.noreply.github.com> Date: Tue, 12 Oct 2021 23:37:00 +0530 Subject: [PATCH 1/5] Create main.dart --- Firebase_CRUD_Task/main.dart | 105 +++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 Firebase_CRUD_Task/main.dart diff --git a/Firebase_CRUD_Task/main.dart b/Firebase_CRUD_Task/main.dart new file mode 100644 index 0000000..d403782 --- /dev/null +++ b/Firebase_CRUD_Task/main.dart @@ -0,0 +1,105 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:crud_task/delete_date.dart'; +import 'package:crud_task/read_data.dart'; +import 'package:crud_task/update_data.dart'; +import 'package:firebase_core/firebase_core.dart'; +import 'package:flutter/material.dart'; +import 'create_data.dart'; + + +void main() { + runApp(const MyApp()); + Firebase.initializeApp(); +} + +class MyApp extends StatelessWidget { + const MyApp({Key? key}) : super(key: key); + + @override + Widget build(BuildContext context) { + return MaterialApp( + title: 'Flutter Demo', + theme: ThemeData( + primarySwatch: Colors.blue, + ), + home: const MyHomePage(), + ); + } +} + +class MyHomePage extends StatefulWidget { + const MyHomePage({Key? key}) : super(key: key); + @override + State createState() => _MyHomePageState(); +} + +class _MyHomePageState extends State { + static const title = "CRUD"; + + deleteData() async { + CollectionReference collectionReference = FirebaseFirestore.instance.collection('data'); + QuerySnapshot querySnapshot = await collectionReference.get(); + querySnapshot.docs[1].reference.delete(); + } + + + @override + Widget build(BuildContext context) { + return MaterialApp( + home: Scaffold( + appBar: AppBar( + title: const Text(title), + ), + body: Column( + children: [ + Center( + child: Container( + margin: const EdgeInsets.all(20.0), + child: const Text("FIRESTORE CRUD OPS", style: TextStyle(fontSize: 30.0), + )), + ), + MaterialButton( + child: const Text("CREATE DATA", style: TextStyle(fontSize: 15)), + height: 50, + minWidth: 200, + color: Colors.grey, + onPressed: () => { + Navigator.push(context, MaterialPageRoute(builder: (context)=>const CreateData())) + } + ), + const SizedBox(height: 30,), + MaterialButton( + child: const Text("READ DATA", style: TextStyle(fontSize: 15)), + height: 50, + minWidth: 200, + color: Colors.grey, + onPressed: ()=>{ + Navigator.push(context, MaterialPageRoute(builder: (context)=>const ReadData())) + } + ), + const SizedBox(height: 30,), + MaterialButton( + child: const Text("UPDATE DATA", style: TextStyle(fontSize: 15)), + height: 50, + minWidth: 200, + color: Colors.grey, + onPressed: ()=>{ + Navigator.push(context, MaterialPageRoute(builder: (context)=>const UpdateData())) + } + ), + const SizedBox(height: 30,), + MaterialButton( + child: const Text("DELETE DATA", style: TextStyle(fontSize: 15)), + height: 50, + minWidth: 200, + color: Colors.grey, + onPressed: ()=>{ + Navigator.push(context, MaterialPageRoute(builder: (context)=>const DeleteData())) + } + ), + ], + ), + ), + ); + } +} From 1e1fcd62aea73767eab62e92fccfc1a6fd63b1a5 Mon Sep 17 00:00:00 2001 From: Pushkar Pawar <73992274+pushkarpawar@users.noreply.github.com> Date: Tue, 12 Oct 2021 23:38:32 +0530 Subject: [PATCH 2/5] Add files via upload --- Firebase_CRUD_Task/create_data.dart | 111 ++++++++++++++++++ Firebase_CRUD_Task/delete_date.dart | 51 ++++++++ .../generated_plugin_registrant.dart | 18 +++ Firebase_CRUD_Task/read_data.dart | 57 +++++++++ Firebase_CRUD_Task/update_data.dart | 110 +++++++++++++++++ 5 files changed, 347 insertions(+) create mode 100644 Firebase_CRUD_Task/create_data.dart create mode 100644 Firebase_CRUD_Task/delete_date.dart create mode 100644 Firebase_CRUD_Task/generated_plugin_registrant.dart create mode 100644 Firebase_CRUD_Task/read_data.dart create mode 100644 Firebase_CRUD_Task/update_data.dart diff --git a/Firebase_CRUD_Task/create_data.dart b/Firebase_CRUD_Task/create_data.dart new file mode 100644 index 0000000..bf69a70 --- /dev/null +++ b/Firebase_CRUD_Task/create_data.dart @@ -0,0 +1,111 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:flutter/material.dart'; + +class CreateData extends StatefulWidget { + const CreateData({ Key? key }) : super(key: key); + + @override + _CreateDataState createState() => _CreateDataState(); +} + +class _CreateDataState extends State { + + TextEditingController nameController = TextEditingController(); + TextEditingController emailController = TextEditingController(); + TextEditingController grController = TextEditingController(); + + createData(){ + DocumentReference documentReference = FirebaseFirestore.instance.collection('data').doc(grController.text); + Map fields = { + "GR": grController.text, + "NAME": nameController.text, + "EMAIL": emailController.text + }; + documentReference.set(fields); + } + + String error = ""; + Future checkDoc() async { + var collectionRef = FirebaseFirestore.instance.collection('data'); + var doc = await collectionRef.doc(grController.text).get(); + if(doc.exists) + { + setState(() { + error = "GR Already Exist"; + }); + } + else{ + setState(() { + error = ""; + }); + } + return doc.exists; + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text("CREATE DATA"), + ), + body: Container( + margin: const EdgeInsets.all(30), + child: Column( + children: [ + TextFormField( + controller: grController, + onEditingComplete: checkDoc, + decoration: InputDecoration( + errorText: error, + hintText: "GR", + border: const OutlineInputBorder(), + ), + style: const TextStyle(fontSize: 20), + ), + const SizedBox(height: 30), + TextFormField( + controller: nameController, + decoration: const InputDecoration( + hintText: "Name", + border: OutlineInputBorder(), + ), + style: const TextStyle(fontSize: 20), + ), + const SizedBox(height: 30), + TextFormField( + controller: emailController, + decoration: const InputDecoration( + hintText: "Email", + border: OutlineInputBorder(), + ), + style: const TextStyle(fontSize: 20), + ), + const SizedBox(height: 20), + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + error.isEmpty ? MaterialButton( + child: const Text("CREATE", style: TextStyle(fontSize: 15)), + onPressed: ()=>{ + createData() + }, + color: Colors.blue, + ) : const Text(" "), + const SizedBox(width: 50,), + MaterialButton( + child: const Text("CLEAR", style: TextStyle(fontSize: 15,)), + onPressed: ()=>{ + nameController.clear(), + emailController.clear(), + grController.clear() + }, + color: Colors.red, + ), + ], + ) + ], + ) + ), + ); + } +} \ No newline at end of file diff --git a/Firebase_CRUD_Task/delete_date.dart b/Firebase_CRUD_Task/delete_date.dart new file mode 100644 index 0000000..4a40aa4 --- /dev/null +++ b/Firebase_CRUD_Task/delete_date.dart @@ -0,0 +1,51 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:flutter/material.dart'; + +class DeleteData extends StatefulWidget { + const DeleteData({ Key? key }) : super(key: key); + + @override + _DeleteDataState createState() => _DeleteDataState(); +} + +class _DeleteDataState extends State { + + TextEditingController grController = TextEditingController(); + + deleteData(){ + DocumentReference documentReference = FirebaseFirestore.instance.collection('data').doc(grController.text); + documentReference.delete(); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text("DELTE DATA"), + ), + body: Container( + margin: const EdgeInsets.all(30), + child: Column( + children: [ + TextFormField( + controller: grController, + decoration: const InputDecoration( + hintText: "GR", + border: OutlineInputBorder(), + ), + style: const TextStyle(fontSize: 20), + ), + MaterialButton( + child: const Text("DELETE", style: TextStyle(fontSize: 15,)), + onPressed: ()=>{ + deleteData() + }, + color: Colors.red, + ), + ] + + ) + ) + ); + } +} \ No newline at end of file diff --git a/Firebase_CRUD_Task/generated_plugin_registrant.dart b/Firebase_CRUD_Task/generated_plugin_registrant.dart new file mode 100644 index 0000000..c21ce41 --- /dev/null +++ b/Firebase_CRUD_Task/generated_plugin_registrant.dart @@ -0,0 +1,18 @@ +// +// Generated file. Do not edit. +// + +// ignore_for_file: directives_ordering +// ignore_for_file: lines_longer_than_80_chars + +import 'package:cloud_firestore_web/cloud_firestore_web.dart'; +import 'package:firebase_core_web/firebase_core_web.dart'; + +import 'package:flutter_web_plugins/flutter_web_plugins.dart'; + +// ignore: public_member_api_docs +void registerPlugins(Registrar registrar) { + FirebaseFirestoreWeb.registerWith(registrar); + FirebaseCoreWeb.registerWith(registrar); + registrar.registerMessageHandler(); +} diff --git a/Firebase_CRUD_Task/read_data.dart b/Firebase_CRUD_Task/read_data.dart new file mode 100644 index 0000000..2618a11 --- /dev/null +++ b/Firebase_CRUD_Task/read_data.dart @@ -0,0 +1,57 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:flutter/material.dart'; + + +class ReadData extends StatefulWidget { + const ReadData({ Key? key }) : super(key: key); + + @override + _ReadDataState createState() => _ReadDataState(); +} + +class Data{ + @required late String name; + @required late String email; +} + +class _ReadDataState extends State { + + var mylist = []; + final Stream data = FirebaseFirestore.instance.collection('data').snapshots(); + + @override + Widget build(BuildContext context) { + return Scaffold( + + appBar: AppBar( + title: const Text("READ DATA"), + ), + body: StreamBuilder( + stream: data, + builder: (BuildContext context, AsyncSnapshot snapshot) { + if(snapshot.connectionState==ConnectionState.waiting) + { + return const Text("Loading"); + } + final info = snapshot.requireData; + return ListView.builder( + itemCount: info.size, + itemBuilder: (context, index) { + return Card( + child: Padding( + padding: const EdgeInsets.all(15), + child: Column( + children: [ + Text(info.docs[index]['GR']), + Text(info.docs[index]['NAME']), + Text(info.docs[index]['EMAIL']) + ], + ), + ), + ); + }); + } + ) + ); + } +} \ No newline at end of file diff --git a/Firebase_CRUD_Task/update_data.dart b/Firebase_CRUD_Task/update_data.dart new file mode 100644 index 0000000..d90e4ed --- /dev/null +++ b/Firebase_CRUD_Task/update_data.dart @@ -0,0 +1,110 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:flutter/material.dart'; + +class UpdateData extends StatefulWidget { + const UpdateData({ Key? key }) : super(key: key); + + @override + _UpdateDataState createState() => _UpdateDataState(); +} + +class _UpdateDataState extends State { + + TextEditingController nameController = TextEditingController(); + TextEditingController emailController = TextEditingController(); + TextEditingController grController = TextEditingController(); + + updatedata(){ + DocumentReference documentReference = FirebaseFirestore.instance.collection('data').doc(grController.text); + Map fields = { + "GR": grController.text, + "NAME": nameController.text, + "EMAIL": emailController.text + }; + documentReference.set(fields); + } + + String error = ""; + Future checkDoc() async { + var collectionRef = FirebaseFirestore.instance.collection('data'); + var doc = await collectionRef.doc(grController.text).get(); + if(!doc.exists) + { + setState(() { + error = "GR Not Found"; + }); + } + else{ + setState(() { + error = ""; + }); + } + return doc.exists; + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text("UPDATE DATA"), + ), + body: Container( + margin: const EdgeInsets.all(30), + child: Column( + children: [ + TextFormField( + controller: grController, + onEditingComplete: checkDoc, + decoration: InputDecoration( + hintText: "GR", + errorText: error, + border: const OutlineInputBorder(), + ), + style: const TextStyle(fontSize: 20), + ), + const SizedBox(height: 30), + TextFormField( + controller: nameController, + decoration: const InputDecoration( + hintText: "Name", + border: OutlineInputBorder(), + ), + style: const TextStyle(fontSize: 20), + ), + const SizedBox(height: 30), + TextFormField( + controller: emailController, + decoration: const InputDecoration( + hintText: "Email", + border: OutlineInputBorder(), + ), + style: const TextStyle(fontSize: 20), + ), + const SizedBox(height: 20), + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + error.isEmpty ? MaterialButton( + child: const Text("CREATE", style: TextStyle(fontSize: 15)), + onPressed: ()=>{ + updatedata() + }, + color: Colors.blue, + ) : const Text(" "), + const SizedBox(width: 50,), + MaterialButton( + child: const Text("CLEAR", style: TextStyle(fontSize: 15,)), + onPressed: ()=>{ + nameController.clear(), + emailController.clear() + }, + color: Colors.red, + ), + ], + ) + ], + ) + ), + ); + } +} \ No newline at end of file From 174ef27ae194e7df95e329c46f82e588a399674f Mon Sep 17 00:00:00 2001 From: Pushkar Pawar <73992274+pushkarpawar@users.noreply.github.com> Date: Sun, 31 Oct 2021 18:36:43 +0530 Subject: [PATCH 3/5] Delete Firebase_CRUD_Task directory --- Firebase_CRUD_Task/create_data.dart | 111 ------------------ Firebase_CRUD_Task/delete_date.dart | 51 -------- .../generated_plugin_registrant.dart | 18 --- Firebase_CRUD_Task/main.dart | 105 ----------------- Firebase_CRUD_Task/read_data.dart | 57 --------- Firebase_CRUD_Task/update_data.dart | 110 ----------------- 6 files changed, 452 deletions(-) delete mode 100644 Firebase_CRUD_Task/create_data.dart delete mode 100644 Firebase_CRUD_Task/delete_date.dart delete mode 100644 Firebase_CRUD_Task/generated_plugin_registrant.dart delete mode 100644 Firebase_CRUD_Task/main.dart delete mode 100644 Firebase_CRUD_Task/read_data.dart delete mode 100644 Firebase_CRUD_Task/update_data.dart diff --git a/Firebase_CRUD_Task/create_data.dart b/Firebase_CRUD_Task/create_data.dart deleted file mode 100644 index bf69a70..0000000 --- a/Firebase_CRUD_Task/create_data.dart +++ /dev/null @@ -1,111 +0,0 @@ -import 'package:cloud_firestore/cloud_firestore.dart'; -import 'package:flutter/material.dart'; - -class CreateData extends StatefulWidget { - const CreateData({ Key? key }) : super(key: key); - - @override - _CreateDataState createState() => _CreateDataState(); -} - -class _CreateDataState extends State { - - TextEditingController nameController = TextEditingController(); - TextEditingController emailController = TextEditingController(); - TextEditingController grController = TextEditingController(); - - createData(){ - DocumentReference documentReference = FirebaseFirestore.instance.collection('data').doc(grController.text); - Map fields = { - "GR": grController.text, - "NAME": nameController.text, - "EMAIL": emailController.text - }; - documentReference.set(fields); - } - - String error = ""; - Future checkDoc() async { - var collectionRef = FirebaseFirestore.instance.collection('data'); - var doc = await collectionRef.doc(grController.text).get(); - if(doc.exists) - { - setState(() { - error = "GR Already Exist"; - }); - } - else{ - setState(() { - error = ""; - }); - } - return doc.exists; - } - - @override - Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar( - title: const Text("CREATE DATA"), - ), - body: Container( - margin: const EdgeInsets.all(30), - child: Column( - children: [ - TextFormField( - controller: grController, - onEditingComplete: checkDoc, - decoration: InputDecoration( - errorText: error, - hintText: "GR", - border: const OutlineInputBorder(), - ), - style: const TextStyle(fontSize: 20), - ), - const SizedBox(height: 30), - TextFormField( - controller: nameController, - decoration: const InputDecoration( - hintText: "Name", - border: OutlineInputBorder(), - ), - style: const TextStyle(fontSize: 20), - ), - const SizedBox(height: 30), - TextFormField( - controller: emailController, - decoration: const InputDecoration( - hintText: "Email", - border: OutlineInputBorder(), - ), - style: const TextStyle(fontSize: 20), - ), - const SizedBox(height: 20), - Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - error.isEmpty ? MaterialButton( - child: const Text("CREATE", style: TextStyle(fontSize: 15)), - onPressed: ()=>{ - createData() - }, - color: Colors.blue, - ) : const Text(" "), - const SizedBox(width: 50,), - MaterialButton( - child: const Text("CLEAR", style: TextStyle(fontSize: 15,)), - onPressed: ()=>{ - nameController.clear(), - emailController.clear(), - grController.clear() - }, - color: Colors.red, - ), - ], - ) - ], - ) - ), - ); - } -} \ No newline at end of file diff --git a/Firebase_CRUD_Task/delete_date.dart b/Firebase_CRUD_Task/delete_date.dart deleted file mode 100644 index 4a40aa4..0000000 --- a/Firebase_CRUD_Task/delete_date.dart +++ /dev/null @@ -1,51 +0,0 @@ -import 'package:cloud_firestore/cloud_firestore.dart'; -import 'package:flutter/material.dart'; - -class DeleteData extends StatefulWidget { - const DeleteData({ Key? key }) : super(key: key); - - @override - _DeleteDataState createState() => _DeleteDataState(); -} - -class _DeleteDataState extends State { - - TextEditingController grController = TextEditingController(); - - deleteData(){ - DocumentReference documentReference = FirebaseFirestore.instance.collection('data').doc(grController.text); - documentReference.delete(); - } - - @override - Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar( - title: const Text("DELTE DATA"), - ), - body: Container( - margin: const EdgeInsets.all(30), - child: Column( - children: [ - TextFormField( - controller: grController, - decoration: const InputDecoration( - hintText: "GR", - border: OutlineInputBorder(), - ), - style: const TextStyle(fontSize: 20), - ), - MaterialButton( - child: const Text("DELETE", style: TextStyle(fontSize: 15,)), - onPressed: ()=>{ - deleteData() - }, - color: Colors.red, - ), - ] - - ) - ) - ); - } -} \ No newline at end of file diff --git a/Firebase_CRUD_Task/generated_plugin_registrant.dart b/Firebase_CRUD_Task/generated_plugin_registrant.dart deleted file mode 100644 index c21ce41..0000000 --- a/Firebase_CRUD_Task/generated_plugin_registrant.dart +++ /dev/null @@ -1,18 +0,0 @@ -// -// Generated file. Do not edit. -// - -// ignore_for_file: directives_ordering -// ignore_for_file: lines_longer_than_80_chars - -import 'package:cloud_firestore_web/cloud_firestore_web.dart'; -import 'package:firebase_core_web/firebase_core_web.dart'; - -import 'package:flutter_web_plugins/flutter_web_plugins.dart'; - -// ignore: public_member_api_docs -void registerPlugins(Registrar registrar) { - FirebaseFirestoreWeb.registerWith(registrar); - FirebaseCoreWeb.registerWith(registrar); - registrar.registerMessageHandler(); -} diff --git a/Firebase_CRUD_Task/main.dart b/Firebase_CRUD_Task/main.dart deleted file mode 100644 index d403782..0000000 --- a/Firebase_CRUD_Task/main.dart +++ /dev/null @@ -1,105 +0,0 @@ -import 'package:cloud_firestore/cloud_firestore.dart'; -import 'package:crud_task/delete_date.dart'; -import 'package:crud_task/read_data.dart'; -import 'package:crud_task/update_data.dart'; -import 'package:firebase_core/firebase_core.dart'; -import 'package:flutter/material.dart'; -import 'create_data.dart'; - - -void main() { - runApp(const MyApp()); - Firebase.initializeApp(); -} - -class MyApp extends StatelessWidget { - const MyApp({Key? key}) : super(key: key); - - @override - Widget build(BuildContext context) { - return MaterialApp( - title: 'Flutter Demo', - theme: ThemeData( - primarySwatch: Colors.blue, - ), - home: const MyHomePage(), - ); - } -} - -class MyHomePage extends StatefulWidget { - const MyHomePage({Key? key}) : super(key: key); - @override - State createState() => _MyHomePageState(); -} - -class _MyHomePageState extends State { - static const title = "CRUD"; - - deleteData() async { - CollectionReference collectionReference = FirebaseFirestore.instance.collection('data'); - QuerySnapshot querySnapshot = await collectionReference.get(); - querySnapshot.docs[1].reference.delete(); - } - - - @override - Widget build(BuildContext context) { - return MaterialApp( - home: Scaffold( - appBar: AppBar( - title: const Text(title), - ), - body: Column( - children: [ - Center( - child: Container( - margin: const EdgeInsets.all(20.0), - child: const Text("FIRESTORE CRUD OPS", style: TextStyle(fontSize: 30.0), - )), - ), - MaterialButton( - child: const Text("CREATE DATA", style: TextStyle(fontSize: 15)), - height: 50, - minWidth: 200, - color: Colors.grey, - onPressed: () => { - Navigator.push(context, MaterialPageRoute(builder: (context)=>const CreateData())) - } - ), - const SizedBox(height: 30,), - MaterialButton( - child: const Text("READ DATA", style: TextStyle(fontSize: 15)), - height: 50, - minWidth: 200, - color: Colors.grey, - onPressed: ()=>{ - Navigator.push(context, MaterialPageRoute(builder: (context)=>const ReadData())) - } - ), - const SizedBox(height: 30,), - MaterialButton( - child: const Text("UPDATE DATA", style: TextStyle(fontSize: 15)), - height: 50, - minWidth: 200, - color: Colors.grey, - onPressed: ()=>{ - Navigator.push(context, MaterialPageRoute(builder: (context)=>const UpdateData())) - } - ), - const SizedBox(height: 30,), - MaterialButton( - child: const Text("DELETE DATA", style: TextStyle(fontSize: 15)), - height: 50, - minWidth: 200, - color: Colors.grey, - onPressed: ()=>{ - Navigator.push(context, MaterialPageRoute(builder: (context)=>const DeleteData())) - } - ), - ], - ), - ), - ); - } -} diff --git a/Firebase_CRUD_Task/read_data.dart b/Firebase_CRUD_Task/read_data.dart deleted file mode 100644 index 2618a11..0000000 --- a/Firebase_CRUD_Task/read_data.dart +++ /dev/null @@ -1,57 +0,0 @@ -import 'package:cloud_firestore/cloud_firestore.dart'; -import 'package:flutter/material.dart'; - - -class ReadData extends StatefulWidget { - const ReadData({ Key? key }) : super(key: key); - - @override - _ReadDataState createState() => _ReadDataState(); -} - -class Data{ - @required late String name; - @required late String email; -} - -class _ReadDataState extends State { - - var mylist = []; - final Stream data = FirebaseFirestore.instance.collection('data').snapshots(); - - @override - Widget build(BuildContext context) { - return Scaffold( - - appBar: AppBar( - title: const Text("READ DATA"), - ), - body: StreamBuilder( - stream: data, - builder: (BuildContext context, AsyncSnapshot snapshot) { - if(snapshot.connectionState==ConnectionState.waiting) - { - return const Text("Loading"); - } - final info = snapshot.requireData; - return ListView.builder( - itemCount: info.size, - itemBuilder: (context, index) { - return Card( - child: Padding( - padding: const EdgeInsets.all(15), - child: Column( - children: [ - Text(info.docs[index]['GR']), - Text(info.docs[index]['NAME']), - Text(info.docs[index]['EMAIL']) - ], - ), - ), - ); - }); - } - ) - ); - } -} \ No newline at end of file diff --git a/Firebase_CRUD_Task/update_data.dart b/Firebase_CRUD_Task/update_data.dart deleted file mode 100644 index d90e4ed..0000000 --- a/Firebase_CRUD_Task/update_data.dart +++ /dev/null @@ -1,110 +0,0 @@ -import 'package:cloud_firestore/cloud_firestore.dart'; -import 'package:flutter/material.dart'; - -class UpdateData extends StatefulWidget { - const UpdateData({ Key? key }) : super(key: key); - - @override - _UpdateDataState createState() => _UpdateDataState(); -} - -class _UpdateDataState extends State { - - TextEditingController nameController = TextEditingController(); - TextEditingController emailController = TextEditingController(); - TextEditingController grController = TextEditingController(); - - updatedata(){ - DocumentReference documentReference = FirebaseFirestore.instance.collection('data').doc(grController.text); - Map fields = { - "GR": grController.text, - "NAME": nameController.text, - "EMAIL": emailController.text - }; - documentReference.set(fields); - } - - String error = ""; - Future checkDoc() async { - var collectionRef = FirebaseFirestore.instance.collection('data'); - var doc = await collectionRef.doc(grController.text).get(); - if(!doc.exists) - { - setState(() { - error = "GR Not Found"; - }); - } - else{ - setState(() { - error = ""; - }); - } - return doc.exists; - } - - @override - Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar( - title: const Text("UPDATE DATA"), - ), - body: Container( - margin: const EdgeInsets.all(30), - child: Column( - children: [ - TextFormField( - controller: grController, - onEditingComplete: checkDoc, - decoration: InputDecoration( - hintText: "GR", - errorText: error, - border: const OutlineInputBorder(), - ), - style: const TextStyle(fontSize: 20), - ), - const SizedBox(height: 30), - TextFormField( - controller: nameController, - decoration: const InputDecoration( - hintText: "Name", - border: OutlineInputBorder(), - ), - style: const TextStyle(fontSize: 20), - ), - const SizedBox(height: 30), - TextFormField( - controller: emailController, - decoration: const InputDecoration( - hintText: "Email", - border: OutlineInputBorder(), - ), - style: const TextStyle(fontSize: 20), - ), - const SizedBox(height: 20), - Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - error.isEmpty ? MaterialButton( - child: const Text("CREATE", style: TextStyle(fontSize: 15)), - onPressed: ()=>{ - updatedata() - }, - color: Colors.blue, - ) : const Text(" "), - const SizedBox(width: 50,), - MaterialButton( - child: const Text("CLEAR", style: TextStyle(fontSize: 15,)), - onPressed: ()=>{ - nameController.clear(), - emailController.clear() - }, - color: Colors.red, - ), - ], - ) - ], - ) - ), - ); - } -} \ No newline at end of file From 2fd3c1c168b0f5fcdd12c14eae666378e43bc489 Mon Sep 17 00:00:00 2001 From: Pushkar Pawar <73992274+pushkarpawar@users.noreply.github.com> Date: Sun, 31 Oct 2021 18:37:31 +0530 Subject: [PATCH 4/5] Create main.dart --- CRUD_TASK/main.dart | 105 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 CRUD_TASK/main.dart diff --git a/CRUD_TASK/main.dart b/CRUD_TASK/main.dart new file mode 100644 index 0000000..d403782 --- /dev/null +++ b/CRUD_TASK/main.dart @@ -0,0 +1,105 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:crud_task/delete_date.dart'; +import 'package:crud_task/read_data.dart'; +import 'package:crud_task/update_data.dart'; +import 'package:firebase_core/firebase_core.dart'; +import 'package:flutter/material.dart'; +import 'create_data.dart'; + + +void main() { + runApp(const MyApp()); + Firebase.initializeApp(); +} + +class MyApp extends StatelessWidget { + const MyApp({Key? key}) : super(key: key); + + @override + Widget build(BuildContext context) { + return MaterialApp( + title: 'Flutter Demo', + theme: ThemeData( + primarySwatch: Colors.blue, + ), + home: const MyHomePage(), + ); + } +} + +class MyHomePage extends StatefulWidget { + const MyHomePage({Key? key}) : super(key: key); + @override + State createState() => _MyHomePageState(); +} + +class _MyHomePageState extends State { + static const title = "CRUD"; + + deleteData() async { + CollectionReference collectionReference = FirebaseFirestore.instance.collection('data'); + QuerySnapshot querySnapshot = await collectionReference.get(); + querySnapshot.docs[1].reference.delete(); + } + + + @override + Widget build(BuildContext context) { + return MaterialApp( + home: Scaffold( + appBar: AppBar( + title: const Text(title), + ), + body: Column( + children: [ + Center( + child: Container( + margin: const EdgeInsets.all(20.0), + child: const Text("FIRESTORE CRUD OPS", style: TextStyle(fontSize: 30.0), + )), + ), + MaterialButton( + child: const Text("CREATE DATA", style: TextStyle(fontSize: 15)), + height: 50, + minWidth: 200, + color: Colors.grey, + onPressed: () => { + Navigator.push(context, MaterialPageRoute(builder: (context)=>const CreateData())) + } + ), + const SizedBox(height: 30,), + MaterialButton( + child: const Text("READ DATA", style: TextStyle(fontSize: 15)), + height: 50, + minWidth: 200, + color: Colors.grey, + onPressed: ()=>{ + Navigator.push(context, MaterialPageRoute(builder: (context)=>const ReadData())) + } + ), + const SizedBox(height: 30,), + MaterialButton( + child: const Text("UPDATE DATA", style: TextStyle(fontSize: 15)), + height: 50, + minWidth: 200, + color: Colors.grey, + onPressed: ()=>{ + Navigator.push(context, MaterialPageRoute(builder: (context)=>const UpdateData())) + } + ), + const SizedBox(height: 30,), + MaterialButton( + child: const Text("DELETE DATA", style: TextStyle(fontSize: 15)), + height: 50, + minWidth: 200, + color: Colors.grey, + onPressed: ()=>{ + Navigator.push(context, MaterialPageRoute(builder: (context)=>const DeleteData())) + } + ), + ], + ), + ), + ); + } +} From 263ca48b678c0fafe68836912a6d597a721afa25 Mon Sep 17 00:00:00 2001 From: Pushkar Pawar <73992274+pushkarpawar@users.noreply.github.com> Date: Sun, 31 Oct 2021 18:37:50 +0530 Subject: [PATCH 5/5] Add files via upload --- CRUD_TASK/create_data.dart | 111 +++++++++++++++++++++ CRUD_TASK/delete_date.dart | 51 ++++++++++ CRUD_TASK/generated_plugin_registrant.dart | 18 ++++ CRUD_TASK/read_data.dart | 57 +++++++++++ CRUD_TASK/update_data.dart | 110 ++++++++++++++++++++ 5 files changed, 347 insertions(+) create mode 100644 CRUD_TASK/create_data.dart create mode 100644 CRUD_TASK/delete_date.dart create mode 100644 CRUD_TASK/generated_plugin_registrant.dart create mode 100644 CRUD_TASK/read_data.dart create mode 100644 CRUD_TASK/update_data.dart diff --git a/CRUD_TASK/create_data.dart b/CRUD_TASK/create_data.dart new file mode 100644 index 0000000..bf69a70 --- /dev/null +++ b/CRUD_TASK/create_data.dart @@ -0,0 +1,111 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:flutter/material.dart'; + +class CreateData extends StatefulWidget { + const CreateData({ Key? key }) : super(key: key); + + @override + _CreateDataState createState() => _CreateDataState(); +} + +class _CreateDataState extends State { + + TextEditingController nameController = TextEditingController(); + TextEditingController emailController = TextEditingController(); + TextEditingController grController = TextEditingController(); + + createData(){ + DocumentReference documentReference = FirebaseFirestore.instance.collection('data').doc(grController.text); + Map fields = { + "GR": grController.text, + "NAME": nameController.text, + "EMAIL": emailController.text + }; + documentReference.set(fields); + } + + String error = ""; + Future checkDoc() async { + var collectionRef = FirebaseFirestore.instance.collection('data'); + var doc = await collectionRef.doc(grController.text).get(); + if(doc.exists) + { + setState(() { + error = "GR Already Exist"; + }); + } + else{ + setState(() { + error = ""; + }); + } + return doc.exists; + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text("CREATE DATA"), + ), + body: Container( + margin: const EdgeInsets.all(30), + child: Column( + children: [ + TextFormField( + controller: grController, + onEditingComplete: checkDoc, + decoration: InputDecoration( + errorText: error, + hintText: "GR", + border: const OutlineInputBorder(), + ), + style: const TextStyle(fontSize: 20), + ), + const SizedBox(height: 30), + TextFormField( + controller: nameController, + decoration: const InputDecoration( + hintText: "Name", + border: OutlineInputBorder(), + ), + style: const TextStyle(fontSize: 20), + ), + const SizedBox(height: 30), + TextFormField( + controller: emailController, + decoration: const InputDecoration( + hintText: "Email", + border: OutlineInputBorder(), + ), + style: const TextStyle(fontSize: 20), + ), + const SizedBox(height: 20), + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + error.isEmpty ? MaterialButton( + child: const Text("CREATE", style: TextStyle(fontSize: 15)), + onPressed: ()=>{ + createData() + }, + color: Colors.blue, + ) : const Text(" "), + const SizedBox(width: 50,), + MaterialButton( + child: const Text("CLEAR", style: TextStyle(fontSize: 15,)), + onPressed: ()=>{ + nameController.clear(), + emailController.clear(), + grController.clear() + }, + color: Colors.red, + ), + ], + ) + ], + ) + ), + ); + } +} \ No newline at end of file diff --git a/CRUD_TASK/delete_date.dart b/CRUD_TASK/delete_date.dart new file mode 100644 index 0000000..4a40aa4 --- /dev/null +++ b/CRUD_TASK/delete_date.dart @@ -0,0 +1,51 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:flutter/material.dart'; + +class DeleteData extends StatefulWidget { + const DeleteData({ Key? key }) : super(key: key); + + @override + _DeleteDataState createState() => _DeleteDataState(); +} + +class _DeleteDataState extends State { + + TextEditingController grController = TextEditingController(); + + deleteData(){ + DocumentReference documentReference = FirebaseFirestore.instance.collection('data').doc(grController.text); + documentReference.delete(); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text("DELTE DATA"), + ), + body: Container( + margin: const EdgeInsets.all(30), + child: Column( + children: [ + TextFormField( + controller: grController, + decoration: const InputDecoration( + hintText: "GR", + border: OutlineInputBorder(), + ), + style: const TextStyle(fontSize: 20), + ), + MaterialButton( + child: const Text("DELETE", style: TextStyle(fontSize: 15,)), + onPressed: ()=>{ + deleteData() + }, + color: Colors.red, + ), + ] + + ) + ) + ); + } +} \ No newline at end of file diff --git a/CRUD_TASK/generated_plugin_registrant.dart b/CRUD_TASK/generated_plugin_registrant.dart new file mode 100644 index 0000000..c21ce41 --- /dev/null +++ b/CRUD_TASK/generated_plugin_registrant.dart @@ -0,0 +1,18 @@ +// +// Generated file. Do not edit. +// + +// ignore_for_file: directives_ordering +// ignore_for_file: lines_longer_than_80_chars + +import 'package:cloud_firestore_web/cloud_firestore_web.dart'; +import 'package:firebase_core_web/firebase_core_web.dart'; + +import 'package:flutter_web_plugins/flutter_web_plugins.dart'; + +// ignore: public_member_api_docs +void registerPlugins(Registrar registrar) { + FirebaseFirestoreWeb.registerWith(registrar); + FirebaseCoreWeb.registerWith(registrar); + registrar.registerMessageHandler(); +} diff --git a/CRUD_TASK/read_data.dart b/CRUD_TASK/read_data.dart new file mode 100644 index 0000000..2618a11 --- /dev/null +++ b/CRUD_TASK/read_data.dart @@ -0,0 +1,57 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:flutter/material.dart'; + + +class ReadData extends StatefulWidget { + const ReadData({ Key? key }) : super(key: key); + + @override + _ReadDataState createState() => _ReadDataState(); +} + +class Data{ + @required late String name; + @required late String email; +} + +class _ReadDataState extends State { + + var mylist = []; + final Stream data = FirebaseFirestore.instance.collection('data').snapshots(); + + @override + Widget build(BuildContext context) { + return Scaffold( + + appBar: AppBar( + title: const Text("READ DATA"), + ), + body: StreamBuilder( + stream: data, + builder: (BuildContext context, AsyncSnapshot snapshot) { + if(snapshot.connectionState==ConnectionState.waiting) + { + return const Text("Loading"); + } + final info = snapshot.requireData; + return ListView.builder( + itemCount: info.size, + itemBuilder: (context, index) { + return Card( + child: Padding( + padding: const EdgeInsets.all(15), + child: Column( + children: [ + Text(info.docs[index]['GR']), + Text(info.docs[index]['NAME']), + Text(info.docs[index]['EMAIL']) + ], + ), + ), + ); + }); + } + ) + ); + } +} \ No newline at end of file diff --git a/CRUD_TASK/update_data.dart b/CRUD_TASK/update_data.dart new file mode 100644 index 0000000..d90e4ed --- /dev/null +++ b/CRUD_TASK/update_data.dart @@ -0,0 +1,110 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:flutter/material.dart'; + +class UpdateData extends StatefulWidget { + const UpdateData({ Key? key }) : super(key: key); + + @override + _UpdateDataState createState() => _UpdateDataState(); +} + +class _UpdateDataState extends State { + + TextEditingController nameController = TextEditingController(); + TextEditingController emailController = TextEditingController(); + TextEditingController grController = TextEditingController(); + + updatedata(){ + DocumentReference documentReference = FirebaseFirestore.instance.collection('data').doc(grController.text); + Map fields = { + "GR": grController.text, + "NAME": nameController.text, + "EMAIL": emailController.text + }; + documentReference.set(fields); + } + + String error = ""; + Future checkDoc() async { + var collectionRef = FirebaseFirestore.instance.collection('data'); + var doc = await collectionRef.doc(grController.text).get(); + if(!doc.exists) + { + setState(() { + error = "GR Not Found"; + }); + } + else{ + setState(() { + error = ""; + }); + } + return doc.exists; + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text("UPDATE DATA"), + ), + body: Container( + margin: const EdgeInsets.all(30), + child: Column( + children: [ + TextFormField( + controller: grController, + onEditingComplete: checkDoc, + decoration: InputDecoration( + hintText: "GR", + errorText: error, + border: const OutlineInputBorder(), + ), + style: const TextStyle(fontSize: 20), + ), + const SizedBox(height: 30), + TextFormField( + controller: nameController, + decoration: const InputDecoration( + hintText: "Name", + border: OutlineInputBorder(), + ), + style: const TextStyle(fontSize: 20), + ), + const SizedBox(height: 30), + TextFormField( + controller: emailController, + decoration: const InputDecoration( + hintText: "Email", + border: OutlineInputBorder(), + ), + style: const TextStyle(fontSize: 20), + ), + const SizedBox(height: 20), + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + error.isEmpty ? MaterialButton( + child: const Text("CREATE", style: TextStyle(fontSize: 15)), + onPressed: ()=>{ + updatedata() + }, + color: Colors.blue, + ) : const Text(" "), + const SizedBox(width: 50,), + MaterialButton( + child: const Text("CLEAR", style: TextStyle(fontSize: 15,)), + onPressed: ()=>{ + nameController.clear(), + emailController.clear() + }, + color: Colors.red, + ), + ], + ) + ], + ) + ), + ); + } +} \ No newline at end of file