💜

Building Flutter Chat App using Firebase with Geo location sharing.
💜 (In Depth)

❗️❗️Task 5 — Flutter App Development (LW)❗️❗️

  • Firebase Authorization: Sign in, Sign up or Sing Out
  • Firebase Firestore for Installing Plugins: Upload, Remove and Retrieve Data Stored in Cloud Firestore
  • Creating the Layout of the Chat App Screen
  • Final Linking of the Flutter Chat App with Firebase
  • Adding Geolocation sharing.

What is Cloud Firestore? 🔥

  1. Collections — Collections are nothing but a set of document.
  2. Documents — Documents are set of sub-collections and field.
  3. Fields — Field consists of attribute and value. The values are of the type of string, number, Boolean, map, array, null, timestamp, geopoints and reference.
  • If you observe the building blocks of Firestore you can find that it is similar to JSON structure where you can store data in any form, any structure and any depth.

Any complex feature can be implemented easily with proper model design. In this article we will learn how to define and implement model both abstract and specific for programming language

Basic user flow of our chat app.

Now lets get started:

Step 0 : Setting up the Basic architecture of app of easier development process.

Open the pubspec.yaml file in your project and add the following dependencies into it.

firebase_core: "0.5.0" 
shared_preferences: ^0.5.12 // To store the login state of the user
simple_animations: ^1.3.3 //(optional) for Fade in animation
firebase_auth: ^0.18.1+1 //for firebase authentication
google_sign_in: "^4.5.1" //for google signin functionality
geolocator: ^5.3.2+2 // for accessing user geolocation
google_maps_flutter: ^0.5.30 // google map integration
  • Singin.dart
  • Singup.dart
  • Home.dart(Home page to display active converstaions).
  • Conversation_screen.dart
  • map.dart // for displaying the map
  • search.dart // user search screen
  • Auth.dart
  • database.dart
  • Helper.dart // Shared preference Will allow us to Save so info loke login status and logged in Username and email to store in phone memory.
import 'package:shared_preferences/shared_preferences.dart';import 'package:flutter/material.dart';class HelperFunction {
static String sharedPreferencesUserLoggedInKey = "ISLOGGEDIN";
static String sharedPreferencesUserNameKey = "USERNAMEKEY";
static String sharedPreferencesUserEmailKey = "USEREMAILKEY";
static Future<void> saveuserLoggedInSharedPreference(
bool isUserLoggedIn) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return await prefs.setBool(
sharedPreferencesUserLoggedInKey, isUserLoggedIn);
}
static Future<void> saveuserNameSharedPreference(String userName) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return await prefs.setString(sharedPreferencesUserNameKey, userName);
}
static Future<void> saveuserEmailSharedPreference(String userEmail) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return await prefs.setString(sharedPreferencesUserEmailKey, userEmail);
}
//getting the data form shared preferences
static Future<bool> GetuserLoggedInSharedPreference() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getBool(sharedPreferencesUserLoggedInKey);
}
static Future<String> getuserNameSharedPreference() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getString(sharedPreferencesUserNameKey);
}
static Future<String> getuserEmailSharedPreference() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getString(sharedPreferencesUserEmailKey);
}
}
  • authenticate.dart // This file will will help user to choose to signup or signin with will be the base file which will redirecte the user to different scaffold for signup and signin.
import 'package:flutter/material.dart';
class Authenticate extends StatefulWidget {
@override
_AuthenticateState createState() => _AuthenticateState();
}
class _AuthenticateState extends State<Authenticate> {
bool showlogin = true;
@override
Widget build(BuildContext context) {
void toggleView() {
setState(() {
showlogin = !showlogin;
});
}
if (showlogin) {
return LoginPage(toggleView);
} else {
return SignUp(toggleView);
}
}
}
  • Constants.dart // you can add more value that you want to save in phone memeory.
  • SearchModel.dart(model data for user searchlist)

Step 1: Firebase Authentication in the App: Sign in, Sign up or Sing Out

//code for google signUp using email and password
Future signUpWithEmailAndPassword(String email, String password) async {
try {
UserCredential userCredential = await _auth
.createUserWithEmailAndPassword(email: email, password: password);
User user = userCredential.user;
return _usercheck(user);
} on FirebaseAuthException catch (e) {
if (e.code == 'weak-password') {
print('The password provided is too weak.');
return 0;
}
else if (e.code == 'email-already-in-use') {
print('The account already exists for that email.');return 1;
}
} catch (e) {
print(e.toString());
}
}
//Code for Google Signin using email and password
User1 _usercheck(User user) {
if (user != null) {
return User1(userId: user.uid);
} else {
return null;
}
}
Future signinWithEmailAndPassword(String email, String password) async {
try {
UserCredential userCredential = await _auth.signInWithEmailAndPassword(
email: email, password: password);
User user = userCredential.user;
return _usercheck(user);
} on FirebaseAuthException catch (e) {
if (e.code == 'user-not-found') {
print('No user found for that email.');
return 0;
} else if (e.code == 'wrong-password') {
print('Wrong password provided for that user.');
return 1;
}
}
}
//Sign Out Function
Future signOut() async {
try {
BotToast.showText(
text: "logged out Successfully",
);
HelperFunction.saveuserLoggedInSharedPreference(false);
return await _auth.signOut();
} catch (e) {
print(e.toString());
}
}
}
// We have to call this function as soon as user signup this function will upload the user data to Firestore for registration.
uploaduserInfo(userMap) {
FirebaseFirestore.instance.collection("users").add(userMap);
}

Step 2: Implementing Chat Home Screen

Chat Tile

Step 3: Implementing Search Screen.

This Function Will Search The user by its username is database.
//getusername search function will fetch the username.

//This function will append all searched user in List of type searchmodel

Now to show the fetched users list we will use the following list widget which will return the search tile which two parameter that is username and email id.

Now after clicking on each tile we want to the conversation screen where we can chat to the searched user. But First we want to create the chat room in database. for that call the following fucntion which will create the chatroom with a specific unique id that id will be like username1_username2 .

it will create the chatroom in firestore database.
This function will create the unique id for the chatroom.

Step 4: Implementing Conversation Screen.

Step 5: Implementing Geo Location feature.

this function will fetch the location of your friend.
widget for displaying google map

That’s it by now i guess you had a pretty good idea how to build a chat app using flutter. for more detail you can check out the GitHub link for the the project : Link will be added soon.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store