FoodDeliveryApp 4 - FirebaseFirestore DataBase CRUD Operations

 

 

Reading Tasks

  Widget _getTasks() {
    return StreamBuilder(
        stream: Firestore.instance
            .collection('tasks')
            .orderBy('timestamp', descending: true)
            .snapshots(),
        builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
          if (snapshot.hasData) {
            return ListView.builder(
              padding: const EdgeInsets.all(10.0),
              itemBuilder: (BuildContext context, int index) =>
                Text(snapshot.data.documents[index]['content']),
              itemCount: snapshot.data.documents.length,
            );
          } else {
            return Container();
          }
        });
  }

 Firebase CRUD Operations With Flutter Part 2 (Collection Reference)

Updating & Deleting

import 'dart:async';
import 'package:cloud_firestore/cloud_firestore.dart';

class Database {
  static final Firestore _db = Firestore.instance;

  static Future<void> addTask(Map<String, dynamic> task) async {
    await _db.collection('tasks').document().setData(task).catchError((e) {
      print(e);
    });
    return true;
  }

  static Future<void> updateTask(String id, Map<String, dynamic> task) async {
    await _db.collection('tasks').document(id).updateData(task).catchError((e) {
      print(e);
    });
    return true;
  }

  static Future<void> deleteTask(String id) async {
    await _db.collection('tasks').document(id).delete().catchError((e) {
      print(e);
    });
    return true;
  }
}

Comments

Popular posts from this blog

Flutter : Introduction

How to release your Flutter app

Firebase Cloud Messaging