Flutter Templates

Building a Social Networking App with Flutter and Back4App

54min

Introduction

Creating a social networking app can be a complex task, but with Flutter and Back4App, you can streamline the development process. This tutorial will guide you through building a full-featured social networking app that includes user authentication, profile management, news feeds, friend connections, messaging, and notifications.

By the end of this tutorial, you will have a functional social networking app with the following features:

  • User Authentication: Secure sign-up and login processes.
  • User Profiles: Editable profiles with user information.
  • News Feeds: Display posts from friends and the user.
  • Friend Connections: Ability to send and accept friend requests.
  • Messaging: Real-time chat between users.
  • Notifications: Push notifications for friend requests, messages, and post interactions.

Prerequisites

To follow along with this tutorial, you will need:

  • Flutter SDK installed on your machine. Follow the official Flutter installation guide for your operating system.
  • Basic knowledge of Flutter and Dart. If you're new to Flutter, review the Flutter documentation to familiarize yourself with the basics.
  • An IDE or text editor such as Visual Studio Code or Android Studio.
  • A Back4App account. Sign up for a free account at Back4App.
  • Parse Server SDK for Flutter added to your project. Learn how to set it up by following the Back4App Flutter SDK Guide.

Step 1 – Setting Up the Flutter Project

1.1. Create a New Flutter Project

Open your terminal and run:

Bash


Navigate to the project directory:

Bash


1.2. Add Dependencies

Open pubspec.yaml and add the following dependencies:

YAML


Run flutter pub get to install the packages.

Note: We are using:

  • parse_server_sdk_flutter for Back4App integration.
  • provider for state management.
  • image_picker for selecting profile and post images.
  • cached_network_image for efficient image loading.
  • firebase_messaging for push notifications.
  • uuid for generating unique IDs.

Step 2 – Setting Up Back4App

2.1. Create a New Back4App Application

  1. Log in to your Back4App dashboard.
  2. Click on "Create new App".
  3. Enter a name for your application, e.g., "SocialApp", and click "Create".

2.2. Set Up the Data Models

We'll need to create several classes in Back4App:

  • User: Built-in class for user authentication.
  • Profile: Stores user profile information.
  • Post: Stores user posts.
  • FriendRequest: Manages friend requests between users.
  • Message: Stores messages between users.

2.2.1. Profile Class

  1. Navigate to the "Database" section.
  2. Click on "Create a class".
  3. In the modal:
    • Select "Custom".
    • Enter "Profile" as the class name.
    • Click "Create class".

Add the following columns:

  • user: Type Pointer<_User>
  • username: Type String
  • fullName: Type String
  • bio: Type String
  • profilePicture: Type File

2.2.2. Post Class

Create a "Post" class with the following columns:

  • user: Type Pointer<_User>
  • content: Type String
  • image: Type File
  • createdAt: Type Date

2.2.3. FriendRequest Class

Create a "FriendRequest" class with the following columns:

  • fromUser: Type Pointer<_User>
  • toUser: Type Pointer<_User>
  • status: Type String (Values: "pending", "accepted", "rejected")

2.2.4. Message Class

Create a "Message" class with the following columns:

  • fromUser: Type Pointer<_User>
  • toUser: Type Pointer<_User>
  • content: Type String
  • createdAt: Type Date

2.3. Obtain Application Credentials

  1. Navigate to App Settings > Security & Keys.
  2. Note down your Application ID and Client Key.

Step 3 – Initializing Parse in Your Flutter App

Open lib/main.dart and modify it as follows:

Dart

  • Replace 'YOUR_APPLICATION_ID' and 'YOUR_CLIENT_KEY' with your Back4App credentials.
  • We are using ChangeNotifierProvider to manage authentication state.

Step 4 – Implementing User Authentication

4.1. Create the Authentication Service

Create a new directory called services under lib and add a file named auth_service.dart:

Dart


4.2. Create the Login and Signup Screens

Create a directory called screens under lib and add login_screen.dart and signup_screen.dart.

4.2.1. Login Screen

Dart


4.2.2. Signup Screen

Dart


Step 5 – Setting Up User Profiles

5.1. Create Profile Service

Add a file named profile_service.dart under lib/services/:

Dart


5.2. Create Profile Model

Add a file named profile.dart under lib/models/:

Dart


5.3. Create Profile Screen

Add a file named profile_screen.dart under lib/screens/:

Dart


Step 6 – Implementing News Feeds

6.1. Create Post Service

Add a file named post_service.dart under lib/services/:

Dart


6.2. Create Post Model

Add a file named post.dart under lib/models/:

Dart


6.3. Create Home Screen

Modify home_screen.dart under lib/screens/:

Dart


Step 7 – Adding Friend Connections

In this step, we will implement friend connections between users. Users can send friend requests, accept or reject them, and view their list of friends. We will modify the data models, create services, and update the UI to support this functionality.

7.1. Update the Data Model

We have already created the FriendRequest class in Back4App with the following columns:

  • fromUser: Pointer to _User
  • toUser: Pointer to _User
  • status: String (Values: "pending", "accepted", "rejected")

Additionally, we need to track a user's list of friends. We can do this by adding a friends relation in the _User class.

7.1.1. Add Friends Relation to User Class

  1. In Back4App, go to the User class.
  2. Click on the "+" button to add a new column.
  3. Name the column "friends" and set the type to Relation <_User>.

7.2. Create Friend Request Service

Create a file named friend_service.dart under lib/services/:

Dart


7.3. Update the UI

7.3.1. Add Search Users Screen

Create a file named search_users_screen.dart under lib/screens/:

Dart


7.3.2. Add Friend Requests Screen

Create a file named friend_requests_screen.dart under lib/screens/:

Dart


7.3.3. Update Home Screen Navigation

In home_screen.dart, add navigation to the search users and friend requests screens:

Dart


7.4. Update News Feed to Show Friends' Posts

Modify the getPosts method in post_service.dart to fetch posts from the user and their friends.

Dart


Step 8 – Implementing Messaging

In this step, we'll add real-time messaging between users using Live Queries.

8.1. Enable Live Queries in Back4App

  1. In your Back4App app dashboard, go to App Settings > Server Settings.
  2. Under Server URL, note down your server URL (e.g., https://YOUR_APP_NAME.back4app.io).
  3. Live Queries are enabled by default on Back4App.

8.2. Set Up Live Queries in Flutter

The parse_server_sdk_flutter package includes Live Query support.

8.3. Create Messaging Service

Create a file named message_service.dart under lib/services/:

Dart


8.4. Create Message Model

Add a file named message.dart under lib/models/:

Dart


8.5. Create Chat Screen

Add a file named chat_screen.dart under lib/screens/:

Dart


8.6. Update UI to Initiate Chat

You can initiate the chat from the friends list or search users screen.

In the friends list, when displaying friends, add a button to start a chat:

Dart


Step 9 – Adding Push Notifications

Implementing push notifications involves setting up Firebase Cloud Messaging (FCM) and integrating it with Back4App.

9.1. Configure Firebase Cloud Messaging

9.1.1. Set Up Firebase Project

  1. Go to Firebase Console and create a new project.
  2. Add an Android and/or iOS app to your project.
    • For Android, you need the package name (applicationId).
    • For iOS, you need the Bundle Identifier.

9.1.2. Download Configuration Files

  • For Android: Download google-services.json and place it in android/app/.
  • For iOS: Download GoogleService-Info.plist and add it to your Xcode project under Runner.

9.2. Add firebase_messaging Package

Ensure you have added firebase_messaging to your pubspec.yaml:

YAML


Run flutter pub get to install.

9.3. Initialize Firebase in Flutter

Modify main.dart:

Dart


9.4. Configure Firebase Messaging

Create a file named push_notification_service.dart under lib/services/:

Dart


9.5. Update main.dart to Initialize Push Notifications

In main.dart, after initializing Parse, initialize the PushNotificationService:

Dart


9.6. Sending Push Notifications from Back4App

You can send push notifications using Cloud Code or directly from your server.

9.6.1. Sending Notification on Friend Request

When a user sends a friend request, you can send a push notification to the recipient.

In friend_service.dart, modify the sendFriendRequest method:

Dart


Note: Ensure that you have associated the installation with the user.

9.7. Associate Installation with User

When a user logs in, associate their installation with their user account.

In auth_service.dart, after successful login:

Dart


Note: Push notifications require additional setup on both the client and server sides. You need to handle different scenarios, such as background notifications and user permissions, which are beyond the scope of this tutorial.



Conclusion

Congratulations! You've built the foundation of a social networking app using Flutter and Back4App. This app includes user authentication, profile management, and a news feed feature. While implementing full friend connections, messaging, and notifications is beyond this tutorial's scope, you now have the necessary structure to continue expanding your app.

Next Steps

  • Friend Connections: Implement friend request functionalities.
  • Messaging: Add real-time chat features using Live Queries.
  • Notifications: Integrate push notifications for user engagement.
  • UI Enhancements: Improve the user interface and user experience.
  • Security: Ensure data security and privacy by setting appropriate ACLs in Back4App.

Additional Resources

Happy Coding!