Skip to content

A simple flutter app that preforms the basic create, retrieve, update and delete functionalities for Firebase Firestore

Notifications You must be signed in to change notification settings

hermozo/CRUD-con-Flutter-y-Firestore

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Flutter Firestore CRUD

This is a simple Flutter project that demonstrate all the CRUD functionality:

  • Create / insert from firestore
  • Retrive / View from firestore
  • Update / Edit from firestore
  • Delete / Remove from firestore

This source code is designed for beginners in Firebase Firestore, and it demonstrates the simplest way to the basic functionalities above.

Demo

Screenshots

Home Page Add Page Add Page Update Page

Getting Started

To get started with this project, you should do the following steps:

  1. Sign in/up to firebase
  2. Go to console
  3. Start a new project
  4. Create a Firestore database
  5. Create a "books" collection
  6. Add a new record with "title" and "author" fields
  7. Download and input google-service.json to the correct location
  8. Run flutter pub get

If you are new to flutter and firebase, check the video below to get more information on how to connect your flutter app with the firebase project.

How to connect your flutter app with firebase project

Insert a row to Firestore (Create)

Map<String, dynamic> newBook = new Map<String,dynamic>();
    newBook["title"] = "title value";
    newBook["author"] = "author value";

Firestore.instance
        .collection("books")
        .add(newBook)
        .whenComplete((){
        // You can add your desire action after the row is added
      } );

Edit a row in firestore (Update)

Basic Update function

Map<String, dynamic> updateBook = new Map<String,dynamic>();
 updateBook["title"] = "title value";
 updateBook["author"] = "author value";
 // Updae Firestore record information regular way
Firestore.instance
    .collection("books")
    .document(document.documentID)
    .updateData(updateBook)
    .whenComplete((){
         // You can add your desire action after the row is updated 
});

Or update using a transaction.

Map<String, dynamic> updateBook = new Map<String,dynamic>();
 updateBook["title"] = "title value";
 updateBook["author"] = "author value";
Firestore.instance.runTransaction((transaction) async {
    await transaction.update(document.reference, updateBook)
        .then((error){
     // You can add your desire action after the row is updated 
    });
  });
},

Delete a row in firestore (Delete)

Firestore.instance
     .collection("books")
     .document(document.documentID) // Replace the document.documentID with the row id that you need to delete
     .delete()
     .catchError((e){
   print(e);
 });

View all the rows in a collection from firestore (Retrieve)

 @override
  Widget build(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
        stream: Firestore.instance.collection('books').snapshots(),
        builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
          if (snapshot.hasError)
            return new Text('Error: ${snapshot.error}');
          switch (snapshot.connectionState) {
            case ConnectionState.waiting: return Center(child: CircularProgressIndicator(),);
            default:
              return new ListView(
                padding: EdgeInsets.only(bottom: 80),
                children: snapshot.data.documents.map((DocumentSnapshot document) {
                  return Padding(
                    padding: EdgeInsets.symmetric(vertical: 3, horizontal: 10),
                    child: Card(
                      child: ListTile(
                        title: new Text("Title " + document['title']),
                        subtitle: new Text("Author " + document['author']),
                      ),
                    ),
                  );
                }).toList(),
              );
          }
        },
      );
  }

About

A simple flutter app that preforms the basic create, retrieve, update and delete functionalities for Firebase Firestore

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Dart 93.3%
  • Swift 3.4%
  • Kotlin 3.0%
  • Objective-C 0.3%