flutterfire icon indicating copy to clipboard operation
flutterfire copied to clipboard

🐛 [cloud_firestore] Using cloud_firestore on macOS appears to only use local cache

Open evanpon opened this issue 6 years ago • 3 comments

Bug report

When building a Flutter project with cloud_firestore for macOS, it appears to use the local cache only for both reads and writes. The same project built on iOS will read/write from the online Firestore.

Steps to reproduce

  1. Setup a default Flutter project.
  2. Add cloud_firestore to pubspec.yaml. I'm using firebase_core 0.5.0 and cloud_firestore 0.14.0.
  3. Create a project in Firestore. Create a collection. Create a single simple document in that collection.
  4. Follow the instructions at https://firebase.flutter.dev/docs/installation/macos to install my GoogleService-Info.plist file.
  5. Initialize Firebase, following the FutureBuilder example here: https://firebase.flutter.dev/docs/overview/#initializing-flutterfire
  6. Add import 'package:cloud_firestore/cloud_firestore.dart';
  7. Update the build method in the _MyHomePageState class (which was created by default) to display the document from Firestore. The relevant snippet of code for me:
body: StreamBuilder(
          stream: FirebaseFirestore.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().toString()),
                        )
                      ],
                    )));
          }),
  1. In this case, nothing will display.

Expected behavior

I expected the document to be displayed. If I follow these steps on iOS, I get the correct result.

If I create an item prior to retrieving it, I do see it on macOS. However, if I query the isFromCache property, it always returns true, whereas on iOS it returns false. This is what leads me to believe that on macOS, I'm only interacting with the local cache.


Additional context


Flutter doctor

Run flutter doctor and paste the output below:

Click To Expand
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel master, 1.22.0-10.0.pre.28, on Mac OS X 10.15.5 19F101, locale en-US)
 
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.1)
[✓] Xcode - develop for iOS and macOS (Xcode 11.6)
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.0)
[✓] VS Code (version 1.48.2)
[✓] Connected device (4 available)

• No issues found!


Flutter dependencies

Run flutter pub deps -- --style=compact and paste the output below:

Click To Expand
Dart SDK 2.10.0-76.0.dev
Flutter SDK 1.22.0-10.0.pre.28
myproject 1.0.0+1

dependencies:
- cloud_firestore 0.14.0 [flutter meta quiver firebase_core firebase_core_platform_interface cloud_firestore_platform_interface cloud_firestore_web]
- cupertino_icons 0.1.3
- firebase_core 0.5.0 [firebase_core_platform_interface flutter quiver meta firebase_core_web]
- flutter 0.0.0 [characters collection meta typed_data vector_math sky_engine]

dev dependencies:
- flutter_test 0.0.0 [flutter test_api path fake_async clock stack_trace vector_math async boolean_selector characters charcode collection matcher meta source_span stream_channel string_scanner term_glyph typed_data]

transitive dependencies:
- async 2.5.0-nullsafety [collection]
- boolean_selector 2.1.0-nullsafety [source_span string_scanner]
- characters 1.1.0-nullsafety.2
- charcode 1.2.0-nullsafety
- clock 1.1.0-nullsafety
- cloud_firestore_platform_interface 2.0.1 [flutter meta collection firebase_core plugin_platform_interface]
- cloud_firestore_web 0.2.0+1 [flutter flutter_web_plugins firebase http_parser meta firebase_core cloud_firestore_platform_interface js]
- collection 1.15.0-nullsafety.2
- fake_async 1.1.0-nullsafety [clock collection]
- firebase 7.3.0 [http http_parser js]
- firebase_core_platform_interface 2.0.0 [flutter meta plugin_platform_interface quiver]
- firebase_core_web 0.2.0 [firebase firebase_core_platform_interface flutter flutter_web_plugins meta js]
- flutter_web_plugins 0.0.0 [flutter characters collection meta typed_data vector_math]
- http 0.12.2 [http_parser path pedantic]
- http_parser 3.1.4 [charcode collection source_span string_scanner typed_data]
- js 0.6.2
- matcher 0.12.10-nullsafety [stack_trace]
- meta 1.3.0-nullsafety.2
- path 1.8.0-nullsafety
- pedantic 1.9.2 [meta]
- plugin_platform_interface 1.0.2 [meta]
- quiver 2.1.3 [matcher meta]
- sky_engine 0.0.99
- source_span 1.8.0-nullsafety [charcode collection path term_glyph]
- stack_trace 1.10.0-nullsafety [path]
- stream_channel 2.1.0-nullsafety [async]
- string_scanner 1.1.0-nullsafety [charcode source_span]
- term_glyph 1.2.0-nullsafety
- test_api 0.2.19-nullsafety [async boolean_selector collection meta path source_span stack_trace stream_channel string_scanner term_glyph matcher]
- typed_data 1.3.0-nullsafety.2 [collection]
- vector_math 2.1.0-nullsafety.2

evanpon avatar Sep 05 '20 17:09 evanpon

Also, if I try to use a GetOptions object to specify Source.server, I get an exception.

GetOptions options = GetOptions(source: Source.server);
    var value =
        await FirebaseFirestore.instance.collection('myCollection').get(options);
    print("value: ${value.docs.first.data().toString()}");

This results in the following:

[ERROR:flutter/lib/ui/ui_dart_state.cc(171)] Unhandled Exception: [cloud_firestore/unavailable] The service is currently unavailable. This is a most likely a transient condition and may be corrected by retrying with a backoff.

evanpon avatar Sep 08 '20 18:09 evanpon

macOS

flutter: 0
Screenshot 2020-09-10 at 5 49 33 PM

iOS

flutter: {title: John Wick, author: John Wick}
flutter: 1

Simulator Screen Shot - iPhone 11 - 2020-09-10 at 17 57 45

code sample
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  FirebaseApp app = await Firebase.initializeApp();
  assert(app != null);
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Material App',
      theme: ThemeData.dark(),
      home: Home(),
    );
  }
}

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Firestore Example'),
      ),
      body: Center(
        child: BookList(),
      ),
    );
  }
}

class BookList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
      stream: FirebaseFirestore.instance.collection('books').snapshots(),
      builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
        if (snapshot.hasError) return new Text('Error: ${snapshot.error}');

        if (snapshot.hasData) {
          for (var item in snapshot.data.docs) {
            print(item.data());
          }

          print(snapshot.data.docs.length);
        }

        switch (snapshot.connectionState) {
          case ConnectionState.waiting:
            return new Text('Loading...');
          default:
            return new ListView(
              children: snapshot.data.docs.map((DocumentSnapshot document) {
                return new ListTile(
                  title: new Text(document.data()['title'] ?? 'null'),
                  subtitle: new Text(document.data()['author'] ?? 'null'),
                );
              }).toList(),
            );
        }
      },
    );
  }
}


flutter doctor -v
[✓] Flutter (Channel dev, 1.22.0-9.0.pre, on Mac OS X 10.15.6 19G2021, locale
    en-GB)
    • Flutter version 1.22.0-9.0.pre at /Users/tahatesser/Code/flutter_dev
    • Framework revision 7a43175198 (12 days ago), 2020-08-28 23:18:04 -0400
    • Engine revision 07e2520d5d
    • Dart version 2.10.0 (build 2.10.0-73.0.dev)

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
    • Android SDK at /Users/tahatesser/Code/sdk
    • Platform android-30, build-tools 30.0.2
    • ANDROID_HOME = /Users/tahatesser/Code/sdk
    • Java binary at: /Applications/Android
      Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build
      1.8.0_242-release-1644-b3-6222593)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.7)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.7, Build version 11E801a
    • CocoaPods version 1.9.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 4.0)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 49.0.2
    • Dart plugin version 193.7547
    • Java version OpenJDK Runtime Environment (build
      1.8.0_242-release-1644-b3-6222593)

[✓] VS Code (version 1.48.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.14.1

 
[✓] Connected device (4 available)            
    • RMX2001 (mobile) • EUYTFEUSQSRGDA6D • android-arm64  • Android 10 (API 29)
    • macOS (desktop)  • macos            • darwin-x64     • Mac OS X 10.15.6
      19G2021
    • Web Server (web) • web-server       • web-javascript • Flutter Tools
    • Chrome (web)     • chrome           • web-javascript • Google Chrome
      85.0.4183.102

• No issues found!
  firebase_core: ^0.5.0
  cloud_firestore: ^0.14.0+2

TahaTesser avatar Sep 10 '20 12:09 TahaTesser

Closing this issue. I was running my app in App Sandbox mode, and hadn't provided the com.apple.security.network.client entitlement.

evanpon avatar Sep 19 '20 23:09 evanpon