GOOGLE SHEETS × FLUTTER
Table of Contents
Introduction
In this tutorial, we will explore how to integrate Google Sheets with Flutter to create efficient applications. This connection allows you to manage data effectively, making it ideal for habit trackers, expense trackers, and other applications. By the end of this guide, you'll have a clear understanding of how to set up your Flutter app to interact with Google Sheets.
Step 1: Set Up Google Sheets
-
Create a Google Sheet:
- Open Google Sheets and create a new sheet.
- Name your sheet appropriately (e.g., "Habit Tracker").
-
Format Your Data:
- Set up headers for your data (e.g., Date, Habit, Status).
- Fill in some example data to test the integration.
-
Publish the Sheet:
- Go to File > Publish to the web.
- Select the entire document or specific sheets to publish.
- Click "Publish" and copy the link provided.
Step 2: Set Up Google Sheets API
-
Access Google Cloud Console:
- Go to the Google Cloud Console at console.cloud.google.com.
- Create a new project or select an existing one.
-
Enable Google Sheets API:
- In the project dashboard, click on "API & Services."
- Click on "Library" and search for "Google Sheets API."
- Click "Enable" to activate the API for your project.
-
Create Credentials:
- Navigate to "Credentials" on the left sidebar.
- Click on "Create Credentials" and choose "Service account."
- Fill in the necessary details and create the service account.
- Generate a JSON key and save it securely; this will be used in your Flutter app.
-
Share Your Sheet:
- Open your Google Sheet and click on "Share."
- Add the service account email (found in your JSON key) with edit permissions.
Step 3: Set Up Flutter Project
-
Create a New Flutter Project:
- Run the command:
flutter create google_sheets_integration
- Navigate into your project directory:
cd google_sheets_integration
- Run the command:
-
Add Dependencies:
- Open
pubspec.yaml
and add the following dependencies:dependencies
- Run
flutter pub get
to install the packages.
http: ^0.13.3 googleapis: ^7.0.0 googleapis_auth: ^1.0.0
- Open
Step 4: Implement Google Sheets API in Flutter
-
Load Credentials:
- Create a file named
credentials.dart
and store your JSON key there. - Use the following code to load the credentials:
import 'dart:convert'; import 'package:http/http.dart' as http; import 'package:googleapis/sheets/v4.dart'; import 'package:googleapis_auth/auth_io.dart'; void main() async { final jsonString = await rootBundle.loadString('credentials.json'); final credentials = jsonDecode(jsonString); final client = await clientViaServiceAccount( ServiceAccountCredentials.fromJson(credentials), [SheetsApi.SpreadsheetsScope]); }
- Create a file named
-
Create Functions to Interact with Google Sheets:
- Create functions to read and write data to your Google Sheet. Here's an example to read data:
Future<void> readData(SheetsApi api, String spreadsheetId) async { var response = await api.spreadsheets.values.get(spreadsheetId, 'Sheet1!A1:C10'); print(response.values); }
- Create functions to read and write data to your Google Sheet. Here's an example to read data:
-
Implement UI:
- Design your Flutter UI to display and interact with data from Google Sheets. Use appropriate Flutter widgets to create forms, lists, or charts.
Conclusion
You have now set up a basic integration between Google Sheets and your Flutter app, enabling data management for applications like habit trackers or expense trackers. Experiment with further functionalities, such as adding new entries or updating existing ones. Explore the Google Sheets API documentation for advanced features and best practices. Happy coding!