2023-06-30

[Flutter] Ultra-fast app development with Masamune (2) “Easy connection to Firestore”

Hello. I'm Masaru Hirose.

I introduced the Masamune framework, which enables stable and fast development of applications in Flutter.

Hello. My name is Masaru Hirose.
https://medium.comhttps://medium.com
title

I would like to write several introductory articles to let you know its charm and how to use it.

By mastering the Masamune framework, you will be able to develop ultra-fast, stable, and high-quality applications.

In the previous issue, I talked about creating a project.

The second session is "Easy Connection to Firestore".

Create Firestore

First, make Firestore available.

Create a project from the Firebase console.

https://console.firebase.google.comhttps://console.firebase.google.com
title

Remember the Project ID that are created together here.

This time, I will create it with masamune-test.

image block

GoogleAnalyitics is at your disposal.

image block

A project is created.

image block

Create a new Firestore.

image block

The mode should be set to TestMode.

image block

Specify the region.

Remember the region specified here.

Here asia-northeast1 is specified.

image block

Firestore has been created.

image block

Firebase configuration in a Flutter project

Return to the previously created Flutter project and reflect the Firebase settings.

Open katana.yaml.

Enter the Firebase project ID you just created in project_id and set firestore enable to true.

# katana.yaml

# This section contains information related to Firebase.
# Firebase関連の情報を記載します。
firebase:
  # Set the Firebase project ID.
  # FirebaseのプロジェクトIDを設定します。
  project_id: masamune-test

  # Enable Firebase Firestore.
  # Firebase Firestoreを有効にします。
  firestore:
    enable: true # false -> true
    overwrite_rule: false

  # Enable Firebase Authentication.
  # Firebase Authenticationを有効にします。
  authentication:
    enable: false

Open a terminal and login to the account where you created the Firebase project.

*If you have logged in to a different account, please log out and log in again.

firebase login

Finally, run the katana apply command.

katana apply

This will create a configuration for the platform in the Firebase project and automatically create a configuration in the Flutter project to connect to Firebase.

Adapter replacement

Rewrite main.dart.

Replace modelAdapter with FirestoreModelAdapter from LocalModelAdapter with DefaultFirebaseOptions.currentPlatform.

import 'firebase_options.dart'; // Add

~~~~~

final modelAdapter = FirestoreModelAdapter(
  options: DefaultFirebaseOptions.currentPlatform,
);
// final modelAdapter = LocalModelAdapter();
// final modelAdapter = RuntimeModelAdapter();

When debugging, the counter application is launched.

image block

If you count up, you can see that the data stored in Firestore has changed.

image block

In the last issue, I explained the use of fields of type ModelCounter in the data model.

When ModelCounter is used with FirestoreModelAdapter, FieldValue.increment is used internally when the value is increased or decreased.

Thus, values can be increased or decreased without inconsistency even when many users access the same field.

Conclusion

With just a few lines of change, we were able to easily connect to Firestore.

Thus, the Masamune framework can be easily configured outside the Dart with a single command, and within the Dart, settings can be easily switched by replacing the adapter.

I will proceed with the actual application development in the next issue. Please look forward to it!

The Masamune framework is available here, and I welcome issues and PullRequests!

If you have any further job requests, please contact me directly through my Twitter or website!

Offers app development and apps using Flutter and Unity. Includes information on music and videos created by the company. Distribution of images and video materials. We also accept orders for work.
https://mathru.nethttps://mathru.net
title

GitHub Sponsors

Sponsors are always welcome. Thank you for your support!

Developed the katana/masamune framework, which has dramatically improved the overall efficiency of Flutter-based application development.
https://github.comhttps://github.com
title
[Flutter] Ultra-fast app development with Masamune (3) "Notepad application creation - Display of notes”

Development Flutter Dart pub Plugins Masamune

◀︎ Next
[Flutter] Ultra-fast app development with Masamune (1) "Create a Project”

Development Flutter Dart pub Plugins Masamune

▶︎ Previous