How do I get a Flutter project running on MacOS to successfully use Firestore?

1,042

Solution 1

My app was in App Sandbox mode, and didn't have the proper entitlement. I could solve the problem in two different ways:

  1. Add the com.apple.security.network.client entitlement to both entitlement files (DebugProfile.entitlements and Release.entitlements, both under macos/Runner. This is the preferred option, if I want to be able to release my app in the App Store.

  2. I could turn the App Sandbox mode off by setting the com.apple.security.app-sandbox entitlement to false, again in the same entitlement files.

The problem of being unable to make a network request was masked in my case because the Firestore client defaults to a local cache if it can't reach the cloud. So in my case, the local cache had no data, and I got an empty response as opposed to an error.

See https://flutter.dev/desktop#entitlements-and-the-app-sandbox for more information.

Solution 2

I had to add

    <key>com.apple.security.network.client</key>
    <true/>
    <key>com.apple.security.network.server</key>
    <true/>

to DebugProfile.Entitlements to access Firestore from my MacOS app.

I left

    <key>com.apple.security.app-sandbox</key>
    <true/>

alone.

Share:
1,042
Evan Pon
Author by

Evan Pon

Updated on December 22, 2022

Comments

  • Evan Pon
    Evan Pon over 1 year

    I have a simple Flutter project. All it does is connect to a Firestore collection, pull some documents, and display them. This works fine on iOS. However, when I try to run it in macOS, I'm unable to retrieve the documents. I don't see any exceptions, just the absence of success.

    The only things I changed from the initial default project was one of the build methods (below), and importing 'package:cloud_firestore/cloud_firestore.dart'.

    My build method:

      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: StreamBuilder(
            stream: Firestore.instance.collection('mycollection').snapshots(),
            builder: (context, snapshot) {
              if (!snapshot.hasData) return const Text('Loading');
              return ListView.builder(
                  itemExtent: 80,
                  itemCount: snapshot.data.documents.length,
                  itemBuilder: (context, index) => ListTile(
                          title: Row(
                        children: [
                          Expanded(
                            child:
                                Text(snapshot.data.documents[index].data['title']),
                          )
                        ],
                      )));
            },
          ),
    
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: Icon(Icons.add),
          ), // This trailing comma makes auto-formatting nicer for build methods.
        );
      }
    

    From reading around, it seems that macOS is not fully supported by Firebase. That said, it appears people have been able to make it work - for example, Swift macOS Firebase. I've also seen that perhaps I should be using the FirebaseCore pod as opposed to Firebase/Core pod as seen here. While I tried to manually add the FirebaseCore pod, it appears I still have the Firebase/Core pod, and I don't understand how the pubspec/pods mechanism enough yet to pull it out.

    More background:

    ▶ flutter doctor
    Doctor summary (to see all details, run flutter doctor -v):
    [✓] Flutter (Channel master, 1.21.0-1.0.pre, on Mac OS X 10.15.5 19F101, locale en-US)
    [✗] Android toolchain - develop for Android devices
        ✗ Unable to locate Android SDK.
          Install Android Studio from: https://developer.android.com/studio/index.html
          On first launch it will assist you in installing the Android SDK components.
          (or visit https://flutter.dev/docs/get-started/install/macos#android-setup for detailed instructions).
          If the Android SDK has been installed to a custom location, set ANDROID_SDK_ROOT to that location.
          You may also want to add it to your PATH environment variable.
    
    
    [✓] Xcode - develop for iOS and macOS (Xcode 11.6)
    [✓] Chrome - develop for the web
    [!] Android Studio (not installed)
    [✓] VS Code (version 1.47.0)
    [✓] Connected device (4 available)
    
    • chihau
      chihau almost 4 years
      This screenshot was taken from the official flutterfire repository README.md ![enter image description here](i.stack.imgur.com/kGVIM.png) But I can't find more information
    • Chris32
      Chris32 almost 4 years
      Does this documentation helps you?
    • Evan Pon
      Evan Pon over 3 years
      @chihau - appreciate the comment! I had seen the link, but was likewise unable to find the information I needed.
    • Evan Pon
      Evan Pon over 3 years
      @Chris32 - appreciate the link! Unfortunately that link is perhaps poorly titled - it's about building an iOS app while using a mac, as opposed to building a macos app.