https://github.com/sayed3li97/flutter-firestore-crud
A simple flutter app that preforms the basic create, retrieve, update and delete functionalities for Firebase Firestore
https://github.com/sayed3li97/flutter-firestore-crud
android firebase firestore flutter ios
Last synced: 2 months ago
JSON representation
A simple flutter app that preforms the basic create, retrieve, update and delete functionalities for Firebase Firestore
- Host: GitHub
- URL: https://github.com/sayed3li97/flutter-firestore-crud
- Owner: sayed3li97
- Created: 2019-09-30T14:17:17.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-12-10T11:07:10.000Z (over 3 years ago)
- Last Synced: 2025-04-15T00:18:12.926Z (2 months ago)
- Topics: android, firebase, firestore, flutter, ios
- Language: Dart
- Homepage:
- Size: 11.8 MB
- Stars: 23
- Watchers: 0
- Forks: 13
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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 firestoreThis source code is designed for beginners in Firebase Firestore, and it demonstrates the simplest way to the basic functionalities above.
## Demo
## Screenshots
![]()
![]()
![]()
# 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.[](https://www.youtube.com/watch?v=DqJ_KjFzL9I)
# Insert a row to Firestore (Create)
```dart
Map newBook = new Map();
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
```dart
Map updateBook = new Map();
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.
```dart
Map updateBook = new Map();
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)
```dart
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)
```dart
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: Firestore.instance.collection('books').snapshots(),
builder: (BuildContext context, AsyncSnapshot 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(),
);
}
},
);
}
```