<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Uche Emmanuel</title>
    <description>The latest articles on DEV Community by Uche Emmanuel (@uche_emma).</description>
    <link>https://dev.to/uche_emma</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F815812%2F5e1af39b-aea9-464e-bc5a-4b495baa26a6.jpg</url>
      <title>DEV Community: Uche Emmanuel</title>
      <link>https://dev.to/uche_emma</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/uche_emma"/>
    <language>en</language>
    <item>
      <title>Building CRUD App With Flutter And Firebase</title>
      <dc:creator>Uche Emmanuel</dc:creator>
      <pubDate>Mon, 28 Mar 2022 00:35:39 +0000</pubDate>
      <link>https://dev.to/uche_emma/building-crud-app-with-flutter-and-firebase-5a43</link>
      <guid>https://dev.to/uche_emma/building-crud-app-with-flutter-and-firebase-5a43</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;CRUD (Create, Read, Update and Delete) operations are an essential part of every modern application. Most moderns apps today tend to make use of these operations. For end-users, CRUD operations allow them to fill out forms, edit data, manage their admin panels, user settings, and so on, and in this tutorial, we would look at how we can implement these operations using Flutter and Firebase.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  GOAL
&lt;/h2&gt;

&lt;p&gt;In this article,  we’ll be building a Todo List App and readers will learn the mode of operation of CRUD functionality, and how to easily integrate it for use in an application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;To follow up with this tutorial, background knowledge of Flutter is required. Also, you need to have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Latest Flutter SDK installed on your machine&lt;/li&gt;
&lt;li&gt;Firebase Account&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is Firebase?
&lt;/h2&gt;

&lt;p&gt;Firebase is a NoSQL database program that stores data in a JSON-like format. It is a Backend-as-a-service (Baas) developed by Google to help enable developers to develop Android Applications, IOS Applications, Web Applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Firebase?
&lt;/h2&gt;

&lt;p&gt;Firebase makes app development relatively easy. With Firebase, one can focus on developing mobile or web applications this is because the internal functions needed to make the app interactive are being handled by the Firebase Interface. Therefore, you have more time to yourself developing the apps that users are going to want to use.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting Up Firebase
&lt;/h2&gt;

&lt;p&gt;If you are new to Firebase, head over to &lt;a href="https://firebase.google.com/" rel="noopener noreferrer"&gt;Firebase&lt;/a&gt; to get started in creating your account and creating a Firebase project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating A Flutter Project
&lt;/h2&gt;

&lt;p&gt;In your desired text editor or IDE (I’ll be using VS Code for this tutorial), open the terminal and type the following: &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;flutter create todo_app 
cd todo_app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This creates a flutter project with the name todo_app and this is where we will write the User Interface of our Todo List Application.&lt;/p&gt;

&lt;h3&gt;
  
  
  File Structure
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647904918457_FileStructure.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647904918457_FileStructure.PNG" alt="File Structure"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Building The User Interface
&lt;/h2&gt;

&lt;p&gt;Here, I do not depend on the UI. We just need to consider the functionalities. This is the UI of our app written in the main. dart file:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1648218131547_Screenshot_20220325-151054.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1648218131547_Screenshot_20220325-151054.png" alt="UI Design"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Firebase Plugins
&lt;/h2&gt;

&lt;p&gt;Here we will add the firebase plugins to our flutter project. The first plugin firebase_core is a Flutter plugin to be used to access the Firebase Core API, which enables connecting to multiple Firebase apps. While the second plugin cloud_firestore is a Flutter plugin used to access the &lt;a href="https://firebase.google.com/docs/firestore/" rel="noopener noreferrer"&gt;Cloud Firestore API&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In your terminal enter the following code:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    flutter pub add firebase_core
    flutter pub add cloud_firestore
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This will add firebase dependencies in our `pubspec.yaml file as shown below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647881710630_AddedDependencies.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647881710630_AddedDependencies.PNG" alt="Added Files"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once the installations are complete, import them into your main.dart file as shown below:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'package:firebase_core/firebase_core.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Copy and Paste the code below on your main. dart file&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'package:flutter/material.dart';

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

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(const MyApp());
}
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}
class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: FloatingActionButton(
        onPressed: () {
          showModalBottomSheet(
            context: context,
            builder: showBottomSheet,
          );
        },
        child: const Icon(Icons.add),
      ),
      appBar: AppBar(
        title: const Text('Todo App'),
        centerTitle: true,
      ),
      body: Center(child: Text('Our Todo Will Be Here')),
    );
  }
}
Widget showBottomSheet(BuildContext context) {
  String? value;
  return Padding(
    padding: const EdgeInsets.only(top: 20),
    child: Column(
      children: [
        SizedBox(
          width: MediaQuery.of(context).size.width * 0.9,
          child: TextField(
            decoration: const InputDecoration(
              border: OutlineInputBorder(),
              labelText: 'Add Todo',
              hintText: 'Enter An Item',
            ),
            onChanged: (String _val) {
              value = _val;
            },
          ),
        ),
        TextButton(
            style: ButtonStyle(
                backgroundColor:
                    MaterialStateProperty.all(Colors.lightBlueAccent)),
            onPressed: () {
              // Todo: The Create Functionality will be here
            },
            child: const Text(
              'ADD',
              style: TextStyle(color: Colors.white),
            ))
      ],
    ),
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the code above, we have an asynchronous function that initializes our Firebase. We need to do that otherwise, we will have an error.&lt;br&gt;
We also have a stateless widget that returns a Scaffold and we also have a button where we can add our to-do list items in our app.&lt;/p&gt;
&lt;h2&gt;
  
  
  Creating A Firebase Project
&lt;/h2&gt;

&lt;p&gt;Navigate your browser to Console to go to the firebase console so that we can create our firebase project.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647877037770_FirebaseConsole.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647877037770_FirebaseConsole.PNG" alt="Firebase Console"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on the Create A Project button to start creating our firebase project&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647877205207_CreatingFirebaseProject.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647877205207_CreatingFirebaseProject.PNG" alt="Create Firebase Project"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Add the desired name of our project in my case TodoApp and also accept the Firebase terms and hit continue.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647877396698_EnableGoogleAnalytics.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647877396698_EnableGoogleAnalytics.PNG" alt="Accept Firebase terms"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Enable Google Analytics for your project and hit continue.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647877481578_ChooseLocation.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647877481578_ChooseLocation.PNG" alt="Enable Google Analytics"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Choose Your Location and accept the Google Analytics Terms and hit the Create Project button to continue.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647877600653_CreatingProject.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647877600653_CreatingProject.PNG" alt="Choose Location"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This will create resources that we can use in creating our application. Hit the continue button when completed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647877758725_Project.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647877758725_Project.PNG" alt="Adding Resources"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This takes us to our firebase project page, you can see the name of our app above (TodoApp in my case). &lt;/p&gt;
&lt;h2&gt;
  
  
  Adding Firebase Dependencies To Our Flutter Project
&lt;/h2&gt;

&lt;p&gt;Now we will add or register an app to our firebase project click on the android logo as seen on the image above to register your android app.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647878329929_RegisterApp1.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647878329929_RegisterApp1.PNG" alt="Register App Step 1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To get your android package name should follow this format com.example.your-flutter-project-name. In my case com.example.todo_app. Add a nickname of your choice and click on the Register App button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647878570907_DownloadTheServiceFile.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647878570907_DownloadTheServiceFile.PNG" alt="Download the googleservices.json file"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, download the &lt;em&gt;google-services.json&lt;/em&gt; file and it to your app module of our flutter project as shown below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647914047229_AddingServiceFolderToApp.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647914047229_AddingServiceFolderToApp.PNG" alt="Adding downloaded file to our project"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hit The Next Button if you have done that.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647879160795_AddingFirebaseSDK.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647879160795_AddingFirebaseSDK.PNG" alt="Adding Firebase SDK"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In our android level build.gradle file, add the following code in your flutter project in my case I have this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647914196433_AddingFunctionsToOurApp.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647914196433_AddingFunctionsToOurApp.PNG" alt="Added Dependencies to our android level build.gradle"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here I have added the codes in our build.gradle file. Then head over to the app-level build.gradle file(android&amp;gt;app&amp;gt;build.gradle) and add the code as seen above. In my case, I have this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647914516190_AddingFunctionsToOurApp2.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647914516190_AddingFunctionsToOurApp2.PNG" alt="Added Dependencies to app level build.gradle"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note: Firebase uses a &lt;code&gt;minSdkVersion&lt;/code&gt; of 19 upwards and set the &lt;code&gt;multiDexEnabled true&lt;/code&gt; to add multidex support.&lt;/p&gt;

&lt;p&gt;If that is completed go back to firebase and hit the Next button to continue.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647880773950_AddingFunctionaltiesCompleted.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647880773950_AddingFunctionaltiesCompleted.PNG" alt="Complete adding dependencies"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hit the Continue To Console button to continue.&lt;/p&gt;

&lt;p&gt;I’ll be building the android app only. For IOS users registration of your app is easy and almost the same step in registering our android app. &lt;/p&gt;

&lt;p&gt;Now run the code. If everything runs perfectly then congratulations you just added Firebase to our Flutter project. If you still encounter an error, remember to change your &lt;code&gt;minSdkVersion 19&lt;/code&gt; and also add the &lt;code&gt;MultiDexEnabled true&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Creating A Database&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647953100361_AccessingDatabase.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647953100361_AccessingDatabase.PNG" alt="Creating Database Step 1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On the Build Panel, click on the Firestore Database so we can create a database and a collection&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647953284818_CreatingDatabase.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647953284818_CreatingDatabase.PNG" alt="Creating Database Step 2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on the Create database button to continue.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647953417171_StartInTestMode.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647953417171_StartInTestMode.PNG" alt="Creating Database Step 3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While creating the database click on start in test mode and hit the Next button to continue.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647953598694_FireStoreLocation.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647953598694_FireStoreLocation.PNG" alt="Setting Location"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In setting the cloud firestore location, you can choose a location of your choice. I will be using the nam5(us-central) as seen above. Click the Enable button when done.&lt;/p&gt;
&lt;h2&gt;
  
  
  Creating A Collection
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647953828321_DatabaseOverview.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647953828321_DatabaseOverview.PNG" alt="Creating Collection"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is our database and here you can add a lot of features to your app. We will create a Collection for our database. Click on the Start collection to continue.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647954239018_AddingCollection1.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647954239018_AddingCollection1.PNG" alt="Database Image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Set the Collection ID and hit Next.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647954342618_AddingCollection2.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647954342618_AddingCollection2.PNG" alt="Set Collection ID"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For the Document ID click on Auto-ID and it will generate one for you add a field and set a value and click Save.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647954475600_AddingCollectionCompleted.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_DF9616ABC0E4C9C85074FB4B0E5CE54B768EDFB763B18A6B0614777C76E21229_1647954475600_AddingCollectionCompleted.PNG" alt="Starting a Collection"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here you can see that we have added a collection with the name todos and it has a field of buy milk.&lt;/p&gt;
&lt;h2&gt;
  
  
  Implementing Operations
&lt;/h2&gt;

&lt;p&gt;Here, we will begin Implementing our operations to make our app functional.&lt;/p&gt;
&lt;h3&gt;
  
  
  Creating/Adding An Item
&lt;/h3&gt;

&lt;p&gt;Copy Paste the code below:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(const MyApp());
}
final db = FirebaseFirestore.instance;
String? value;

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}
class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: FloatingActionButton(
        onPressed: () {
          showModalBottomSheet(
            context: context,
            builder: showBottomSheet,
          );
        },
        child: const Icon(Icons.add),
      ),
      appBar: AppBar(
        title: const Text('Todo App'),
        centerTitle: true,
      ),
      body: Text("Our Todo's will be here"),
      ),
    );
  }
}
Widget showBottomSheet(BuildContext context) {
  return Padding(
    padding: const EdgeInsets.only(top: 20),
    child: Column(
      // mainAxisAlignment: MainAxisAlignment.center,
      children: [
        SizedBox(
          width: MediaQuery.of(context).size.width * 0.9,
          child: TextField(
            decoration: const InputDecoration(
              border: OutlineInputBorder(),
              labelText: 'Add Todo',
              hintText: 'Enter An Item',
            ),
            onChanged: (String _val) {
              value = _val;
            },
          ),
        ),
        TextButton(
            style: ButtonStyle(
                backgroundColor:
                    MaterialStateProperty.all(Colors.lightBlueAccent)),
            onPressed: () {
              db.collection('todos').add({'todo': value});
              Navigator.pop(context);
            },
            child: const Text(
              'ADD',
              style: TextStyle(color: Colors.white),
            ))
      ],
    ),
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;In line 9, we created an instance of our Firebase and passed it in the variable db. Line 10 will hold the value of our todo’s. Line 77 shows you how to add data to your Database. So if we click on the add button, the value of our textfield is stored in the database although we don't see it on our app if you head over to the database we created, you'll see the item added to our database.&lt;/p&gt;
&lt;h3&gt;
  
  
  Retrieving An Item
&lt;/h3&gt;

&lt;p&gt;Firebase provides us with a DocumentSnapshot. To access these snapshots in our Flutter app, we are going to use a widget called StreamBuilder. The StreamBuilder widget listens for changes made in our database and displays those changes in our app.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;StreamBuilder(
        stream: db.collection('todos').snapshots(),
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (!snapshot.hasData) {
            return const Center(
              child: CircularProgressIndicator(),
            );
          }
          return ListView.builder(
            itemCount: snapshot.data?.docs.length,
            itemBuilder: (context, int index) {
              DocumentSnapshot documentSnapshot = snapshot.data.docs[index];
              return ListTile(
                title: Text(documentSnapshot['todo']),
                onTap: () {

                  showModalBottomSheet(
                    context: context,
                    builder: (BuildContext context) {
                      return showBottomSheet(context);
                    },
                  );
                },
                trailing: IconButton(
                  icon: const Icon(
                    Icons.delete_outline,
                  ),
                  onPressed: () {
                    // Here We Will Add The Delete Feature
                  },
                ),
              );
            },
          );
        },
      ),
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;In the body property, add the code above. Here we added a StreamBuilder widget that listens for changes in our app and displays those changes through the ListTile widget built by the Flutter team. Lastly, we checked to see if the snapshot has data before retrieving it because the stream can emit an empty snapshot.&lt;/p&gt;
&lt;h3&gt;
  
  
  Updating And Delete An Item
&lt;/h3&gt;

&lt;p&gt;Here, we will add both the Update And Delete functionalities of our Todo App&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(const MyApp());
}

final db = FirebaseFirestore.instance;
String? value;

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}
class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: FloatingActionButton(
        onPressed: () {
          // When the User clicks on the button, display a BottomSheet
          showModalBottomSheet(
            context: context,
            builder: (context) {
              return showBottomSheet(context, false, null);
            },
          );
        },
        child: const Icon(Icons.add),
      ),
      appBar: AppBar(
        title: const Text('Todo App'),
        centerTitle: true,
      ),
      body: StreamBuilder(
        // Reading Items form our Database Using the StreamBuilder widget
        stream: db.collection('todos').snapshots(),
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (!snapshot.hasData) {
            return const Center(
              child: CircularProgressIndicator(),
            );
          }
          return ListView.builder(
            itemCount: snapshot.data?.docs.length,
            itemBuilder: (context, int index) {
              DocumentSnapshot documentSnapshot = snapshot.data.docs[index];
              return ListTile(
                title: Text(documentSnapshot['todo']),
                onTap: () {
                  // Here We Will Add The Update Feature and passed the value 'true' to the is update
                  // feature.
                  showModalBottomSheet(
                    context: context,
                    builder: (BuildContext context) {
                      return showBottomSheet(context, true, documentSnapshot);
                    },
                  );
                },
                trailing: IconButton(
                  icon: const Icon(
                    Icons.delete_outline,
                  ),
                  onPressed: () {
                    // Here We Will Add The Delete Feature
                    db.collection('todos').doc(documentSnapshot.id).delete();
                  },
                ),
              );
            },
          );
        },
      ),
    );
  }
}
showBottomSheet(
    BuildContext context, bool isUpdate, DocumentSnapshot? documentSnapshot) {
  // Added the isUpdate argument to check if our item has been updated
  return Padding(
    padding: const EdgeInsets.only(top: 20),
    child: Column(
      children: [
        SizedBox(
          width: MediaQuery.of(context).size.width * 0.9,
          child: TextField(
            decoration: InputDecoration(
              border: const OutlineInputBorder(),
              // Used a ternary operator to check if isUpdate is true then display
              // Update Todo.
              labelText: isUpdate ? 'Update Todo' : 'Add Todo',
              hintText: 'Enter An Item',
            ),
            onChanged: (String _val) {
              // Storing the value of the text entered in the variable value.
              value = _val;
            },
          ),
        ),
        TextButton(
            style: ButtonStyle(
              backgroundColor:
                  MaterialStateProperty.all(Colors.lightBlueAccent),
            ),
            onPressed: () {
              // Check to see if isUpdate is true then update the value else add the value
              if (isUpdate) {
                db.collection('todos').doc(documentSnapshot?.id).update({
                  'todo': value,
                });
              } else {
                db.collection('todos').add({'todo': value});
              }
              Navigator.pop(context);
            },
            child: isUpdate
                ? const Text(
                    'UPDATE',
                    style: TextStyle(color: Colors.white),
                  )
                : const Text('ADD', style: TextStyle(color: Colors.white))),
      ],
    ),
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Here we added the Update and Delete Functionality for our app as seen in the code above. Here we added new arguments &lt;code&gt;isUpdate&lt;/code&gt; which is of type bool and &lt;code&gt;documtentSnapshot&lt;/code&gt; which is of type DocumentSnapshot and at the onTap property of our ListTile we passed isUpdate to be true because we want to update the item when we tap on the ListTile. &lt;br&gt;
We used ternary-operators in lines 99 and also in lines 124-129 to display different values in our app.&lt;/p&gt;
&lt;h2&gt;
  
  
  Testing The Application
&lt;/h2&gt;

&lt;p&gt;&lt;iframe src="https://player.vimeo.com/video/692905865" width="710" height="399"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;We have finally come to the end of this tutorial. We’ve learned how to build a CRUD app with Flutter and Firebase, and how to perform CRUD operations.&lt;/p&gt;

&lt;p&gt;To check out the full code, visit the &lt;a href="https://github.com/UcFlutter/Todo-App" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>flutter</category>
      <category>firebase</category>
      <category>dart</category>
      <category>mobile</category>
    </item>
  </channel>
</rss>
