Flutter Templates

Building an E-commerce App with Flutter, Back4App, and Stripe Integration via Cloud Code

48min

Introduction

Developing an e-commerce app involves multiple components, including product listings, shopping cart functionality, secure payment processing, order tracking, and user reviews. Combining Flutter's powerful UI toolkit with Back4App's scalable backend services simplifies the development process. Additionally, integrating Stripe for payment processing via Back4App Cloud Code allows for secure and professional transaction handling.

In this tutorial, you will build an e-commerce app with the following features:

  • Product Listings: Display products with images, descriptions, and prices.
  • Shopping Cart: Add and remove products from the cart.
  • User Accounts: Manage user profiles and addresses.
  • Secure Checkout: Process payments securely using Stripe via Back4App Cloud Code.
  • Order Tracking: Track order status and history.
  • Reviews and Ratings: Allow users to submit reviews and ratings.

Prerequisites

To follow this tutorial, you will need:

  • Flutter SDK installed on your machine. Follow the Flutter installation guide.
  • Basic knowledge of Flutter and Dart.
  • An IDE or text editor like Visual Studio Code or Android Studio.
  • A Back4App account. Sign up at Back4App.
  • Stripe account. Sign up at Stripe to obtain API keys.
  • Node.js and npm installed for Cloud Code development.



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:

  • parse_server_sdk_flutter for Back4App integration.
  • provider for state management.
  • cached_network_image for efficient image loading.
  • flutter_stripe for Stripe integration on the client side.
  • uuid for generating unique identifiers.

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 "EcommerceApp" as the application name and click "Create".

2.2. Set Up the Data Models

We need to create several classes in Back4App:

  • Product
  • User (Built-in class)
  • Order
  • OrderItem
  • Review

2.2.1. Product Class

  1. Navigate to the "Database" section.
  2. Click on "Create a class".
  3. Select "Custom" and enter "Product" as the class name.
  4. Add the following columns:
    • name: String
    • description: String
    • price: Number
    • image: File
    • category: String
    • inventory: Number

2.2.2. Order Class

Create an "Order" class with the following columns:

  • user: Pointer<_User>
  • totalAmount: Number
  • status: String (Values: "pending", "paid", "shipped", "delivered")
  • paymentIntentId: String (To track Stripe payment)
  • shippingAddress: String

2.2.3. OrderItem Class

Create an "OrderItem" class with the following columns:

  • order: Pointer
  • product: Pointer
  • quantity: Number
  • price: Number

2.2.4. Review Class

Create a "Review" class with the following columns:

  • product: Pointer
  • user: Pointer<_User>
  • rating: Number
  • comment: String

2.3. Obtain Application Credentials

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

Step 3 – Implementing Product Management

3.1. Initializing Parse in Flutter

Open lib/main.dart and modify it:

Dart


Replace 'YOUR_APPLICATION_ID' and 'YOUR_CLIENT_KEY' with your Back4App credentials.

3.2. Creating the Product Model

Create a directory models under lib and add product.dart:

Dart


3.3. Implementing Product Service

Create a directory services under lib and add product_service.dart:

Dart


3.4. Creating the Home Screen

Create screens directory under lib and add home_screen.dart:

Dart


3.5. Product Detail Screen

Create product_detail_screen.dart under lib/screens/:

Dart


Step 4 – Implementing User Accounts

4.1. Authentication Service

Add auth_service.dart under lib/services/:

Dart


4.2. Authentication Screens

Create login_screen.dart and signup_screen.dart under lib/screens/.

Login Screen:

Dart


Signup Screen:

Dart


4.3. Update Main to Include AuthService

In main.dart, wrap the MaterialApp with MultiProvider:

Dart


4.4. Redirect Based on Authentication State

Modify main.dart to check authentication status:

Dart


Step 5 – Shopping Cart Functionality

5.1. Create Cart Service

Add cart_service.dart under lib/services/:

Dart


5.2. Create Cart Screen

Add cart_screen.dart under lib/screens/:

Dart


Step 6 – Secure Checkout with Stripe Integration via Cloud Code

6.1. Set Up Stripe Account

  1. Sign up for a Stripe account.
  2. Obtain your Publishable Key and Secret Key from the Stripe Dashboard under Developers > API keys.

6.2. Install Stripe SDK in Cloud Code

Back4App supports Cloud Code functions written in JavaScript. We'll write Cloud Code functions to handle payment processing.

6.2.1. Create Cloud Code Project

  1. In your Back4App app dashboard, go to App Settings > Cloud Code Functions.
  2. Click "Edit code" to open the Cloud Code editor.

6.2.2. Initialize npm and Install Stripe Package

In the Cloud Code terminal (provided in the editor), run:

Bash


Note: Back4App's Cloud Code uses Node.js version 14.x, so ensure compatibility.

6.3. Create Cloud Function for Payment Intent

Create or modify main.js in the Cloud Code editor:

JS


Replace 'YOUR_STRIPE_SECRET_KEY' with your actual Stripe secret key.

Security Note: Never expose your secret key on the client side. Keep it secure in Cloud Code.

6.4. Deploy Cloud Code

Click "Deploy" in the Cloud Code editor to deploy your functions.

6.5. Implement Payment in Flutter

6.5.1. Initialize Stripe in Flutter

In main.dart, after Parse().initialize, add:

Dart


Replace 'YOUR_STRIPE_PUBLISHABLE_KEY' with your Stripe publishable key.

6.5.2. Create Checkout Screen

Add checkout_screen.dart under lib/screens/:

Dart


6.5.3. Handle Payment Confirmation

Create order_confirmation_screen.dart under lib/screens/:

Dart


6.6. Saving Order to Back4App

Modify the _processPayment method to save the order details:

Dart


Step 7 – Order Tracking

7.1. Create Order Service

Add order_service.dart under lib/services/:

Dart


7.2. Create Order Model

Add order.dart under lib/models/:

Dart


7.3. Create Orders Screen

Add orders_screen.dart under lib/screens/:

Dart


Step 8 – Reviews and Ratings

8.1. Create Review Service

Add review_service.dart under lib/services/:

Dart


8.2. Create Review Model

Add review.dart under lib/models/:

Dart


8.3. Update Product Detail Screen

In product_detail_screen.dart, add a section to display and submit reviews.

Dart


Conclusion

In this comprehensive tutorial, you've built an e-commerce app using Flutter and Back4App, integrated with Stripe for secure payment processing via Cloud Code. You implemented key features such as product listings, shopping cart functionality, user authentication, order tracking, and reviews.

Key Takeaways

  • Back4App Integration: Simplifies backend management for your Flutter app.
  • Stripe Integration via Cloud Code: Securely processes payments without exposing sensitive keys.
  • Modular Architecture: Separating services and models improves maintainability.

Next Steps

  • Enhance Security: Implement proper error handling and input validation.
  • UI/UX Improvements: Refine the user interface for better user experience.
  • Inventory Management: Update product inventory upon purchase.
  • Email Notifications: Send order confirmation emails to users.
  • Admin Panel: Create an admin interface for managing products and orders.

Additional Resources

Happy Coding!