Advertisement
fahimkamal63

Firebase create, update, delete Flutter

Jan 12th, 2022
915
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 5.37 KB | None | 0 0
  1. import 'package:cloud_firestore/cloud_firestore.dart';
  2. import 'package:flutter/material.dart';
  3.  
  4. class DatabaseBasic extends StatefulWidget {
  5.   const DatabaseBasic({Key? key}) : super(key: key);
  6.  
  7.   @override
  8.   _DatabaseBasicState createState() => _DatabaseBasicState();
  9. }
  10.  
  11. class _DatabaseBasicState extends State<DatabaseBasic> {
  12.  
  13.   TextEditingController name = TextEditingController();
  14.   TextEditingController email = TextEditingController();
  15.  
  16.   create() async {
  17.     try{
  18.       await FirebaseFirestore.instance.collection('user').doc(email.text).set(
  19.         {
  20.           'name': name.text,
  21.           'email': email.text
  22.         }
  23.       );
  24.       name.clear();
  25.       email.clear();
  26.     } catch(e){
  27.       print(e);
  28.     }
  29.   }
  30.  
  31.   update() async {
  32.     try{
  33.       await FirebaseFirestore.instance.collection('user').doc(email.text).update({
  34.         'name': name.text
  35.       });
  36.       name.clear();
  37.       email.clear();
  38.     } catch(e){
  39.       print(e);
  40.     }
  41.   }
  42.  
  43.   delete() async {
  44.     try{
  45.       await FirebaseFirestore.instance.collection('user').doc(email.text).delete();
  46.       name.clear();
  47.       email.clear();
  48.     } catch(e){
  49.       print(e);
  50.     }
  51.   }
  52.  
  53.   @override
  54.   Widget build(BuildContext context) {
  55.     return Scaffold(
  56.       appBar: AppBar(
  57.         title: Text("Database Basic"),
  58.         centerTitle: true,
  59.       ),
  60.       body: Padding(
  61.         padding: const EdgeInsets.all(10),
  62.         child: Column(
  63.           children: [
  64.             TextField(
  65.               controller: name,
  66.               decoration: InputDecoration(
  67.                 label: Text("Name"),
  68.                 border: OutlineInputBorder(
  69.                   borderSide: BorderSide(color: Colors.red, width: 2),
  70.                   borderRadius: BorderRadius.circular(15)
  71.                 ),
  72.                 enabledBorder: OutlineInputBorder(
  73.                     borderSide: BorderSide(color: Colors.red, width: 2),
  74.                     borderRadius: BorderRadius.circular(15)
  75.                 ),
  76.                 focusedBorder: OutlineInputBorder(
  77.                     borderSide: BorderSide(color: Colors.red, width: 2),
  78.                     borderRadius: BorderRadius.circular(15)
  79.                 ),
  80.               ),
  81.             ),
  82.             SizedBox(height: 10,),
  83.             TextField(
  84.               controller: email,
  85.               decoration: InputDecoration(
  86.                 label: Text("Email"),
  87.                 border: OutlineInputBorder(
  88.                   borderSide: BorderSide(color: Colors.red, width: 2),
  89.                   borderRadius: BorderRadius.circular(15)
  90.                 ),
  91.                 enabledBorder: OutlineInputBorder(
  92.                     borderSide: BorderSide(color: Colors.red, width: 2),
  93.                     borderRadius: BorderRadius.circular(15)
  94.                 ),
  95.                 focusedBorder: OutlineInputBorder(
  96.                     borderSide: BorderSide(color: Colors.red, width: 2),
  97.                     borderRadius: BorderRadius.circular(15)
  98.                 ),
  99.               ),
  100.             ),
  101.             SizedBox(height: 10,),
  102.             Row(
  103.               mainAxisAlignment: MainAxisAlignment.center,
  104.               children: [
  105.                 ElevatedButton(
  106.                   onPressed: create,
  107.                   child: Text("Create"),
  108.                 ),
  109.                 SizedBox(width: 20,),
  110.                 ElevatedButton(
  111.                   onPressed: update,
  112.                   child: Text("Update"),
  113.                 ),
  114.                 SizedBox(width: 20,),
  115.                 ElevatedButton(
  116.                   onPressed: delete,
  117.                   child: Text("Delete"),
  118.                 ),
  119.               ],
  120.             ),
  121.             SizedBox(height: 10,),
  122.             Expanded(
  123.               child: Container(
  124.                 child: StreamBuilder(
  125.                   stream: FirebaseFirestore.instance.collection('user').snapshots(),
  126.                   builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
  127.                     if(snapshot.hasData){
  128.                       return ListView.builder(
  129.                         shrinkWrap: true,
  130.                         physics: BouncingScrollPhysics(),
  131.                         primary: true,
  132.                         itemCount: snapshot.data!.docs.length,
  133.                         itemBuilder: (context, index){
  134.                           QueryDocumentSnapshot networkData = snapshot.data!.docs[index];
  135.                           return Card(
  136.                             child: ListTile(
  137.                               onLongPress: (){
  138.                                 setState(() {
  139.                                   name.text = networkData['name'];
  140.                                   email.text = networkData['email'];
  141.                                 });
  142.                               },
  143.                               title: Text(networkData['name']),
  144.                               subtitle: Text(networkData['email']),
  145.                               // trailing: Text(networkData['test2']),
  146.                               // leading: Text(networkData['test']),
  147.                             ),
  148.                           );
  149.                         }
  150.                       );
  151.                     }
  152.                     return const Center(child: CircularProgressIndicator());
  153.                   }
  154.                 ),
  155.               ),
  156.             ),
  157.           ],
  158.         ),
  159.       ),
  160.     );
  161.   }
  162. }
  163.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement