Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Folder Name Updated #18

Open
wants to merge 5 commits into
base: tasks
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions CRUD_TASK/create_data.dart
Original file line number Diff line number Diff line change
@@ -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<CreateData> {

TextEditingController nameController = TextEditingController();
TextEditingController emailController = TextEditingController();
TextEditingController grController = TextEditingController();

createData(){
DocumentReference documentReference = FirebaseFirestore.instance.collection('data').doc(grController.text);
Map<String, String> fields = {
"GR": grController.text,
"NAME": nameController.text,
"EMAIL": emailController.text
};
documentReference.set(fields);
}

String error = "";
Future<bool> 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,
),
],
)
],
)
),
);
}
}
51 changes: 51 additions & 0 deletions CRUD_TASK/delete_date.dart
Original file line number Diff line number Diff line change
@@ -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<DeleteData> {

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,
),
]

)
)
);
}
}
18 changes: 18 additions & 0 deletions CRUD_TASK/generated_plugin_registrant.dart
Original file line number Diff line number Diff line change
@@ -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();
}
105 changes: 105 additions & 0 deletions CRUD_TASK/main.dart
Original file line number Diff line number Diff line change
@@ -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<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
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()))
}
),
],
),
),
);
}
}
57 changes: 57 additions & 0 deletions CRUD_TASK/read_data.dart
Original file line number Diff line number Diff line change
@@ -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<ReadData> {

var mylist = [];
final Stream<QuerySnapshot> data = FirebaseFirestore.instance.collection('data').snapshots();

@override
Widget build(BuildContext context) {
return Scaffold(

appBar: AppBar(
title: const Text("READ DATA"),
),
body: StreamBuilder<QuerySnapshot>(
stream: data,
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> 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'])
],
),
),
);
});
}
)
);
}
}
Loading