Flutter for Desktop — using Firebase on Windows

iteo
6 min readJul 6, 2023

--

What is Firebase?

Firebase is a collection of hosted services which make scaling and expanding digital products easier. In that way, we don’t need a custom backend for every, even simplest, feature.

Depending on the product, Firebase may be crucial or only an additional part of the tech stack. Nevertheless, since its start it is hard not to use its sweet facilities like cloud messaging or a remote database.

With Firebase, we can easily achieve:

  • User authentication
  • Firestore
  • Push notifications
  • Dynamic configuration

In this article I am going to verify which of these work and how well on Windows.

Support for Flutter

For a long time Firebase was considered mainly for mobile platforms, and for them we can observe the largest support and adoption.

With the expansion of Flutter outside the mobile world, we have to ask how far we can go not only with the pure Dart and framework, but also with dependencies that we rely on.

When it comes to Firebase, there is no official support for Windows, as shown below:

https://firebase.google.com/docs/flutter/setup

Does it mean that there is no way to use any of these in Microsoft’s environment? The answer is not straightforward, but fortunately there are some options.

Setup

Under the hood almost all packages use CLI (Command Line Interface) — a program that via text commands invokes Firebase’s functions with acceptance of parameters.

For the Dart environment there is a dedicated FlutterFire CLI. For the most cases at least project setup is executed via simple instructions.

For the Windows cmd

> npm install -g firebase-tools
> firebase login
> dart pub global activate flutterfire_cli
> flutterfire configure

The installation via NPM is highly recommended as the distributed binary file for Windows is more handy but mostly generates problems — you can read about them in the StackOverflow thread.

Then, in the code, you have to only declare project options. They are generated from your Firebase project’s data for every platform.

Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
runApp(MyApp());
}

User authentication

This is probably the best covered aspect. From the great open source contributor — invertase, we gain firebase_auth_desktop, that is the platform implementation of firebase_auth.

This is the first segment that users will see in our app. Let’s try to investigate if logging is possible and compatible with other platforms. It turns out that it is easy as that:

FirebaseAuth auth = FirebaseAuth.instance;
UserCredential credential = await auth.signInWithEmailAndPassword(
'Email',
'Password'
);

We have successfully tested creating and signing-in a new user with email and password. There is also anonymous logging-in working perfectly, if you need one.

One thing to remember is to enable authentication providers in the Firebase Console. Without that you can encounter strange errors.

Generally, you should be able to create user’s session with:

  • Email
  • EmailLink
  • PhoneNumber
  • OAuthCredential
  • AuthProvider (Google, OAuth, Facebook, Github, Twitter)
  • CustomToken
  • PopUp
  • Redirect

Package is responsible not only for signing-in users, but also for resetting passwords, verifying via captcha, and many more.

Thanks to extra desktop_webview_auth | Flutter Package and firebase_ui_auth | Flutter Package, the authentication process on Windows can be both easy and feature rich.

Firestore

A document oriented database called Firestore is also quite easy to implement in a project. Thanks to firedart | Dart Package, we can perform simple operations like reading and writing documents.

Basic functionalities like fetching, adding and observing documents work perfectly.

// Simple fetch
Firestore.initialize(projectId);
var map = await Firestore.instance.collection("users").get();
// Observe changes
firestore
.collection("users")
.snapshots()
.listen((snapshot) async {
// Handle changes
},
);

What also should work:

  • Queries
  • Ordering and limiting
  • Cursors
  • Custom converters
  • Document and collection references
  • Indexes
  • Offline cache

There are of course things not working partially or at all:

  • Transactions
  • CollectionGroup
  • Regular StreamBuilder (there is provided correct CollectionBuilder)
  • Some functions like: isNotEqualTo or limitToLast

This project has not been updated since April 2021 and seems to be abandoned. When implementing this package, you should keep migration to other solutions in mind.

Push Notifications

Notifications are a pretty important topic in mobile development. They allow for asynchronous communication with users, and so improve advertising and product engagement.

These advantages are also spotted by other technologies. Since Windows 7, we can expect different popups, and since Windows 10 — floating messages similar to mobile notifications.

Year by year in iOS and Android systems this piece of cake becomes more complex and platform-dependent creating a demanding feature.

Because of that, the community created flutter_local_notifications | Flutter Package and awesome_notifications | Flutter Package packages. These are undeniably great, but don’t support Windows applications.

Luckily, there are some ideas to bring flutter notifications on desktop, local_notifier | Flutter Package and windows_notification | Flutter Package seems promising.

The first one is completely basic and can show only the title and message. The second package allows much more and it is not so simple to implement.

The biggest problem for now is a lack of native support for Firebase implementation for Windows. Official firebase_messaging | Flutter Package does not even provide the PlatformChannel plugin in Microsoft’s environment.

One of the solutions is to gear Windows App SDK or any other push message SDK with Flutter App and show notification manually.

This probably will force a developer to write its own platform channel which can consume a lot of time and effort.

Remote configuration

There is no surprise that Remote Config also does not work as expected from the start. Again, the problem is the missing platform channel implementation.

Assuming that we need only simple configuration fetching on app start, there is a possibility!

Thanks to firebaseapis | Dart Package, we can connect any Dart application to any Firebase API via one of the provided authentication methods.

To access the API, you can try to connect to the existing Firebase Google Cloud project or just manually create a new one.

To connect with any Google Cloud project, you must:

  1. Install GCloud CLI on a local machine
  2. Create a project on Google Cloud Console
  3. Enable Firebase Remote Config API
  4. Create gcloud\application_default_credentials.json via gcloud auth application-default login command

Then we can authorize and fetch configuration:

final authClient = await auth.clientViaApplicationDefaultCredentials(
scopes: [
firebase_rules.FirebaseRulesApi.firebaseScope,
firebase_auth.IdentityToolkitApi.firebaseScope,
remote_config.FirebaseRemoteConfigApi.cloudPlatformScope,
]);
final remoteConfig = remote_config.FirebaseRemoteConfigApi(authClient);final config = await remoteConfig.projects
.getRemoteConfig('projects/flutter-windows');

Solution For All

To speed up the testing of some of the described features, I have used a completely new library firebase_for_all | Flutter Package.

Its main responsibility is to wrap most of the Dart Firebase implementations and use appropriate ones for the selected platform.

For example, it tries to use firebase_auth, and when it detects it is run on a computer, then it will switch to firebase_auth_desktop. Just a convenient shortcut for us.

This is a very early stage and many things still need correction. Definitely should not be considered as production ready yet.

Summary (TL;DR)

Digging through different repositories, we can encounter situations where development for Windows has been started but never ended.

For now, we have to say that, next to Linux, Windows is the most unsupported platform and working with it won’t be neither easy nor quick.

The examined state of functionalities:

Want to learn if Flutter is the best choice for your project? We can help!

--

--

iteo
iteo

Written by iteo

iteo is an international digital product studio founded in Poland, that helps businesses benefit from technology better. Visit us on www.iteo.com

Responses (2)