flutterfire icon indicating copy to clipboard operation
flutterfire copied to clipboard

🐛 [cloud_firestore] FireStore Web query.startAfter actually runs startAt

Open mhan8796 opened this issue 1 year ago • 11 comments

Bug report

Describe the bug FireStore Web query.startAfter actually runs startAt

Steps to reproduce

Steps to reproduce the behavior:

In Flutter Web. Run a code to load query doc in pages.

Expected behavior

Like flutter firestore doc said:

Creates and returns a new [Query] that starts after the provided document (exclusive).

Sample project


/* firestore data structure
    final Map<String, dynamic> newCmtDoc = {
      'msg': cmtData, // String
      'from_uid': user?.uid,
      'to_uid': _post?['from_uid'],
      'timestamp': Timestamp.now(),
      'type': cmtType, // String
    };
*/


  DocumentSnapshot? _lastDocument; // Track the last loaded document
  final int _pageSize = 10; // Number of items to load per page

 Future<void> _loadCmts() async {
    Query? query = db
        .collection('comments')
        .orderBy('timestamp', descending: true);

    if (_lastDocument != null) {
      query = query.startAfterDocument(_lastDocument!);
    }
    query = query.limit(_pageSize);

    try {
      QuerySnapshot querySnapshot = await query.get();

      // Convert the QuerySnapshot to a List of DocumentSnapshot
      List<DocumentSnapshot> cmtDocs = querySnapshot.docs;

      if (cmtDocs.isNotEmpty) {
        _lastDocument = cmtDocs.last;
      }

        if (!kReleaseMode) {
          debugPrint('comments docs are : ${cmtDocs}');
        }

      for (var doc in cmtDocs) {
        if (!kReleaseMode) {
          //  debugPrint('id is  : ${doc.id}');
        }

        if (doc.exists && doc.data() != null) {
          final cmtDocData = doc.data() as Map<String, dynamic>;
          cmtDocData['id'] = doc.id;

        //  if (!_comments.any((comment) => comment['id'] == doc.id)) {  // this line actually is a temp workaround
            if (mounted) {
              setState(() {
                _comments.add(cmtDocData);
              });
            }
        //  }
        }
      }
    } catch (e) {
      if (!kReleaseMode) {
        debugPrint('Error getting comments: $e');
      }
    }
  }

Additional context

Issue not occurred in Android. It is in Web only.

Expected behavior in Android: Screenshot 2024-01-02 at 8 01 21 PM

Unexpected behavior in Web: Screenshot 2024-01-02 at 8 02 33 PM


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 stable, 3.13.9, on macOS 14.2.1 23C71 darwin-arm64, locale en-US)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 15.0.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2023.1)
[✓] VS Code (version 1.85.1)
[✓] Connected device (5 available)
[✓] Network resources

• No issues found!

Flutter dependencies

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

Click To Expand
Dart SDK 3.1.5
Flutter SDK 3.13.9
voco 2.0.0+43

dependencies:
- audioplayers 5.2.1 [audioplayers_android audioplayers_darwin audioplayers_linux audioplayers_platform_interface audioplayers_web audioplayers_windows file flutter http meta path_provider synchronized uuid]
- avatar_glow 2.0.2 [flutter]
- cloud_firestore 4.13.1 [cloud_firestore_platform_interface cloud_firestore_web collection firebase_core firebase_core_platform_interface flutter meta]
- email_validator 2.1.17
- firebase_analytics 10.7.0 [firebase_analytics_platform_interface firebase_analytics_web firebase_core firebase_core_platform_interface flutter]
- firebase_app_check 0.2.1+4 [firebase_app_check_platform_interface firebase_app_check_web firebase_core firebase_core_platform_interface flutter]
- firebase_auth 4.14.0 [firebase_auth_platform_interface firebase_auth_web firebase_core firebase_core_platform_interface flutter meta]
- firebase_core 2.24.1 [firebase_core_platform_interface firebase_core_web flutter meta]
- firebase_crashlytics 3.4.4 [firebase_core firebase_core_platform_interface firebase_crashlytics_platform_interface flutter stack_trace]
- firebase_messaging 14.7.7 [firebase_core firebase_core_platform_interface firebase_messaging_platform_interface firebase_messaging_web flutter meta]
- firebase_storage 11.5.1 [firebase_core firebase_core_platform_interface firebase_storage_platform_interface firebase_storage_web flutter]
- flutter 0.0.0 [characters collection material_color_utilities meta vector_math web sky_engine]
- flutter_cache_manager 3.3.1 [clock collection file flutter http path path_provider rxdart sqflite uuid]
- flutter_dotenv 5.1.0 [flutter]
- flutter_image_compress 2.1.0 [flutter flutter_image_compress_platform_interface flutter_image_compress_common flutter_image_compress_web flutter_image_compress_macos]
- font_awesome_flutter 10.6.0 [flutter]
- foundation 0.0.5 [flutter]
- go_router 11.1.4 [collection flutter flutter_web_plugins logging meta]
- google_fonts 6.1.0 [flutter http path_provider crypto]
- google_mobile_ads 3.1.0 [meta flutter visibility_detector webview_flutter_android webview_flutter_wkwebview webview_flutter]
- google_sign_in 6.1.6 [flutter google_sign_in_android google_sign_in_ios google_sign_in_platform_interface google_sign_in_web]
- http 1.1.0 [async http_parser meta]
- image_cropper 5.0.0 [flutter image_cropper_platform_interface image_cropper_for_web]
- image_picker 1.0.4 [flutter image_picker_android image_picker_for_web image_picker_ios image_picker_linux image_picker_macos image_picker_platform_interface image_picker_windows]
- in_app_purchase 3.1.11 [flutter in_app_purchase_android in_app_purchase_platform_interface in_app_purchase_storekit]
- intl 0.18.1 [clock meta path]
- livekit_client 1.5.3 [flutter_web_plugins flutter async collection connectivity_plus cryptography fixnum meta http logging uuid synchronized protobuf flutter_webrtc flutter_window_close device_info_plus js platform_detect dart_webrtc sdp_transform]
- package_info_plus 4.2.0 [ffi flutter flutter_web_plugins http meta path package_info_plus_platform_interface win32]
- path 1.8.3
- path_provider 2.1.1 [flutter path_provider_android path_provider_foundation path_provider_linux path_provider_platform_interface path_provider_windows]
- permission_handler 11.0.1 [flutter meta permission_handler_android permission_handler_apple permission_handler_windows permission_handler_platform_interface]
- profanity_filter 2.0.0
- provider 6.1.1 [collection flutter nested]
- record 5.0.4 [flutter uuid record_platform_interface record_web record_windows record_linux record_android record_darwin]
- sqflite 2.3.0 [flutter sqflite_common path]
- store_redirect 2.0.1 [flutter]
- universal_html 2.2.4 [async csslib charcode collection html meta source_span typed_data universal_io]
- url_launcher 6.2.1 [flutter url_launcher_android url_launcher_ios url_launcher_linux url_launcher_macos url_launcher_platform_interface url_launcher_web url_launcher_windows]
- video_compress 3.1.2 [flutter]
- video_player 2.8.1 [flutter html video_player_android video_player_avfoundation video_player_platform_interface video_player_web]

dev dependencies:
- flutter_lints 2.0.3 [lints]
- flutter_test 0.0.0 [flutter test_api matcher path fake_async clock stack_trace vector_math async boolean_selector characters collection material_color_utilities meta source_span stream_channel string_scanner term_glyph web]

transitive dependencies:
- _fe_analyzer_shared 61.0.0 [meta]
- _flutterfire_internals 1.3.15 [collection firebase_core firebase_core_platform_interface flutter meta]
- analyzer 5.13.0 [_fe_analyzer_shared collection convert crypto glob meta package_config path pub_semver source_span watcher yaml]
- args 2.4.2
- async 2.11.0 [collection meta]
- audioplayers_android 4.0.3 [audioplayers_platform_interface flutter]
- audioplayers_darwin 5.0.2 [audioplayers_platform_interface flutter]
- audioplayers_linux 3.1.0 [audioplayers_platform_interface flutter]
- audioplayers_platform_interface 6.1.0 [flutter meta plugin_platform_interface]
- audioplayers_web 4.1.0 [audioplayers_platform_interface flutter flutter_web_plugins js]
- audioplayers_windows 3.1.0 [audioplayers_platform_interface flutter]
- boolean_selector 2.1.1 [source_span string_scanner]
- characters 1.3.0
- charcode 1.3.1
- clock 1.1.1
- cloud_firestore_platform_interface 6.0.5 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface]
- cloud_firestore_web 3.8.5 [_flutterfire_internals cloud_firestore_platform_interface collection firebase_core firebase_core_web flutter flutter_web_plugins js]
- collection 1.17.2
- connectivity_plus 5.0.1 [flutter flutter_web_plugins connectivity_plus_platform_interface js meta nm]
- connectivity_plus_platform_interface 1.2.4 [flutter meta plugin_platform_interface]
- convert 3.1.1 [typed_data]
- cross_file 0.3.3+6 [js meta]
- crypto 3.0.3 [typed_data]
- cryptography 2.7.0 [collection crypto ffi js meta typed_data]
- csslib 1.0.0 [source_span]
- dart_webrtc 1.1.3 [collection js platform_detect webrtc_interface]
- dbus 0.7.10 [args ffi meta xml]
- device_info_plus 9.1.0 [device_info_plus_platform_interface ffi file flutter flutter_web_plugins meta win32 win32_registry]
- device_info_plus_platform_interface 7.0.0 [flutter meta plugin_platform_interface]
- fake_async 1.3.1 [clock collection]
- ffi 2.1.0
- file 7.0.0 [meta path]
- file_selector_linux 0.9.2+1 [cross_file file_selector_platform_interface flutter]
- file_selector_macos 0.9.3+3 [cross_file file_selector_platform_interface flutter]
- file_selector_platform_interface 2.6.1 [cross_file flutter http plugin_platform_interface]
- file_selector_windows 0.9.3+1 [cross_file file_selector_platform_interface flutter]
- firebase_analytics_platform_interface 3.8.0 [_flutterfire_internals firebase_core flutter meta plugin_platform_interface]
- firebase_analytics_web 0.5.5+7 [_flutterfire_internals firebase_analytics_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins js]
- firebase_app_check_platform_interface 0.1.0+6 [_flutterfire_internals firebase_core flutter meta plugin_platform_interface]
- firebase_app_check_web 0.1.0+6 [_flutterfire_internals firebase_app_check_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins js]
- firebase_auth_platform_interface 7.0.4 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface]
- firebase_auth_web 5.8.7 [firebase_auth_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins http_parser js meta]
- firebase_core_platform_interface 5.0.0 [collection flutter flutter_test meta plugin_platform_interface]
- firebase_core_web 2.10.0 [firebase_core_platform_interface flutter flutter_web_plugins js meta]
- firebase_crashlytics_platform_interface 3.6.12 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface]
- firebase_messaging_platform_interface 4.5.16 [_flutterfire_internals firebase_core flutter meta plugin_platform_interface]
- firebase_messaging_web 3.5.16 [_flutterfire_internals firebase_core firebase_core_web firebase_messaging_platform_interface flutter flutter_web_plugins js meta]
- firebase_storage_platform_interface 5.0.2 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface]
- firebase_storage_web 3.6.13 [_flutterfire_internals async firebase_core firebase_core_web firebase_storage_platform_interface flutter flutter_web_plugins http js meta]
- fixnum 1.1.0
- flutter_image_compress_common 1.0.3 [flutter flutter_image_compress_platform_interface]
- flutter_image_compress_macos 1.0.1 [flutter flutter_image_compress_platform_interface]
- flutter_image_compress_platform_interface 1.0.3 [flutter plugin_platform_interface cross_file]
- flutter_image_compress_web 0.1.3+2 [flutter flutter_web_plugins flutter_image_compress_platform_interface js]
- flutter_plugin_android_lifecycle 2.0.17 [flutter]
- flutter_web_plugins 0.0.0 [flutter characters collection material_color_utilities meta vector_math web]
- flutter_webrtc 0.9.46 [collection dart_webrtc flutter path_provider webrtc_interface]
- flutter_window_close 0.2.2 [flutter flutter_web_plugins]
- glob 2.1.2 [async collection file path string_scanner]
- google_identity_services_web 0.2.2 [js meta]
- google_sign_in_android 6.1.20 [flutter google_sign_in_platform_interface]
- google_sign_in_ios 5.6.5 [flutter google_sign_in_platform_interface pigeon]
- google_sign_in_platform_interface 2.4.2 [flutter plugin_platform_interface quiver]
- google_sign_in_web 0.12.1 [flutter flutter_web_plugins google_identity_services_web google_sign_in_platform_interface http js]
- html 0.15.4 [csslib source_span]
- http_parser 4.0.2 [collection source_span string_scanner typed_data]
- image_cropper_for_web 3.0.0 [flutter flutter_web_plugins image_cropper_platform_interface js]
- image_cropper_platform_interface 5.0.0 [flutter plugin_platform_interface http]
- image_picker_android 0.8.8+2 [flutter flutter_plugin_android_lifecycle image_picker_platform_interface]
- image_picker_for_web 3.0.1 [flutter flutter_web_plugins image_picker_platform_interface mime]
- image_picker_ios 0.8.8+4 [flutter image_picker_platform_interface]
- image_picker_linux 0.2.1+1 [file_selector_linux file_selector_platform_interface flutter image_picker_platform_interface]
- image_picker_macos 0.2.1+1 [file_selector_macos file_selector_platform_interface flutter image_picker_platform_interface]
- image_picker_platform_interface 2.9.1 [cross_file flutter http plugin_platform_interface]
- image_picker_windows 0.2.1+1 [file_selector_platform_interface file_selector_windows flutter image_picker_platform_interface]
- in_app_purchase_android 0.3.0+15 [collection flutter in_app_purchase_platform_interface json_annotation]
- in_app_purchase_platform_interface 1.3.6 [flutter plugin_platform_interface]
- in_app_purchase_storekit 0.3.7 [collection flutter in_app_purchase_platform_interface json_annotation]
- js 0.6.7 [meta]
- json_annotation 4.8.1 [meta]
- lints 2.1.1
- logging 1.2.0
- matcher 0.12.16 [async meta stack_trace term_glyph test_api]
- material_color_utilities 0.5.0 [collection]
- meta 1.9.1
- mime 1.0.4
- nested 1.0.0 [flutter]
- nm 0.5.0 [dbus]
- package_config 2.1.0 [path]
- package_info_plus_platform_interface 2.0.1 [flutter meta plugin_platform_interface]
- path_provider_android 2.2.1 [flutter path_provider_platform_interface]
- path_provider_foundation 2.3.1 [flutter path_provider_platform_interface]
- path_provider_linux 2.2.1 [ffi flutter path path_provider_platform_interface xdg_directories]
- path_provider_platform_interface 2.1.1 [flutter platform plugin_platform_interface]
- path_provider_windows 2.2.1 [ffi flutter path path_provider_platform_interface win32]
- permission_handler_android 11.1.0 [flutter permission_handler_platform_interface]
- permission_handler_apple 9.1.4 [flutter permission_handler_platform_interface]
- permission_handler_platform_interface 3.12.0 [flutter meta plugin_platform_interface]
- permission_handler_windows 0.1.3 [flutter permission_handler_platform_interface]
- petitparser 5.4.0 [meta]
- pigeon 11.0.1 [analyzer args collection meta path yaml]
- platform 3.1.3
- platform_detect 2.0.11 [meta pub_semver]
- plugin_platform_interface 2.1.7 [meta]
- protobuf 3.1.0 [collection fixnum meta]
- pub_semver 2.1.4 [collection meta]
- quiver 3.2.1 [matcher]
- record_android 1.0.4 [flutter record_platform_interface]
- record_darwin 1.0.1 [flutter record_platform_interface]
- record_linux 0.7.1 [flutter record_platform_interface]
- record_platform_interface 1.0.2 [flutter plugin_platform_interface]
- record_web 1.0.4 [flutter flutter_web_plugins js record_platform_interface]
- record_windows 1.0.2 [flutter record_platform_interface path]
- rxdart 0.27.7
- sdp_transform 0.3.2
- sky_engine 0.0.99
- source_span 1.10.0 [collection path term_glyph]
- sprintf 7.0.0
- sqflite_common 2.5.0+2 [synchronized path meta]
- stack_trace 1.11.0 [path]
- stream_channel 2.1.1 [async]
- string_scanner 1.2.0 [source_span]
- synchronized 3.1.0
- term_glyph 1.2.1
- test_api 0.6.0 [async boolean_selector collection meta source_span stack_trace stream_channel string_scanner term_glyph]
- typed_data 1.3.2 [collection]
- universal_io 2.2.2 [collection meta typed_data]
- url_launcher_android 6.2.0 [flutter url_launcher_platform_interface]
- url_launcher_ios 6.2.1 [flutter url_launcher_platform_interface]
- url_launcher_linux 3.1.0 [flutter url_launcher_platform_interface]
- url_launcher_macos 3.1.0 [flutter url_launcher_platform_interface]
- url_launcher_platform_interface 2.2.0 [flutter plugin_platform_interface]
- url_launcher_web 2.2.0 [flutter flutter_web_plugins url_launcher_platform_interface]
- url_launcher_windows 3.1.0 [flutter url_launcher_platform_interface]
- uuid 4.2.1 [crypto sprintf meta]
- vector_math 2.1.4
- video_player_android 2.4.10 [flutter video_player_platform_interface]
- video_player_avfoundation 2.5.2 [flutter video_player_platform_interface]
- video_player_platform_interface 6.2.1 [flutter plugin_platform_interface]
- video_player_web 2.1.2 [flutter flutter_web_plugins video_player_platform_interface]
- visibility_detector 0.4.0+2 [flutter]
- watcher 1.1.0 [async path]
- web 0.1.4-beta
- webrtc_interface 1.1.2
- webview_flutter 4.4.2 [flutter webview_flutter_android webview_flutter_platform_interface webview_flutter_wkwebview]
- webview_flutter_android 3.12.1 [flutter webview_flutter_platform_interface]
- webview_flutter_platform_interface 2.6.0 [flutter meta plugin_platform_interface]
- webview_flutter_wkwebview 3.9.4 [flutter path webview_flutter_platform_interface]
- win32 5.1.0 [ffi]
- win32_registry 1.1.2 [ffi win32]
- xdg_directories 1.0.3 [meta path]
- xml 6.3.0 [collection meta petitparser]
- yaml 3.1.2 [collection source_span string_scanner]

mhan8796 avatar Jan 03 '24 01:01 mhan8796

@mhan8796 Can you check https://github.com/firebase/flutterfire/issues/7946#issuecomment-1090107428 and see if it helps in your case or not ?

darshankawar avatar Jan 03 '24 12:01 darshankawar

@darshankawar Look similar, but In my case, I don't have a .microseconds() as timestamp. I just use Timestamp.now() as timestamp in the db. Is it not working in web?

mhan8796 avatar Jan 03 '24 23:01 mhan8796

@mhan8796 Check if this helps. If not, can you provide the runnable code in text format that we can directly use to verify it further ?

darshankawar avatar Jan 04 '24 11:01 darshankawar

@darshankawar that post is to convert Timestamp to Datetime. I used that in my code but it is not related to this problem.

I updated my code to be text. so basically just that one loadCmt function. It goes through the db by pages. when it is empty, Android and iOS returns empty list [], while web returns last entry.

mhan8796 avatar Jan 04 '24 13:01 mhan8796

Thanks for the update. I tried using the code sample but maybe need the firestore structure as well that I can setup locally so as to properly retrieve the data as you reported. Can you provide a sample firestore structure ?

darshankawar avatar Jan 05 '24 10:01 darshankawar

Thanks for the update. I tried using the code sample but maybe need the firestore structure as well that I can setup locally so as to properly retrieve the data as you reported. Can you provide a sample firestore structure ?

updated it in the original post. Let me know if you need anything else.

mhan8796 avatar Jan 05 '24 12:01 mhan8796

Sorry for coming back late on this. I tried to use the code sample you provided, but am unable to properly replicate the issue reported. So if you could provide us complete runnable code sample would be helpful further.

Also, revisiting the code sample, I see that you are using query.startAfterDocument and not startAfter which are two different methods.

`startAfter` : Takes a list of values, creates and returns a new [Query](https://pub.dev/documentation/cloud_firestore/latest/cloud_firestore/Query-class.html) that starts after the provided fields relative to the order of the query.

The values must be in order of [orderBy](https://pub.dev/documentation/cloud_firestore/latest/cloud_firestore/Query/orderBy.html) filters.

While startAfterDocument which you are using is https://pub.dev/documentation/cloud_firestore/latest/cloud_firestore/Query/startAfterDocument.html

darshankawar avatar Jan 09 '24 11:01 darshankawar

@darshankawar thanks for reminding me. I forgot to comment out a line that serves as temp workaround for this issue. I now comment it out and added another print statement above it. Could you check again?

I just use scrollcontroller to call it.

    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
        _loadCmts();
      }
    });


ListView(
    controller: _scrollController,
    children: [
    // bunch of text widgets
]),

mhan8796 avatar Jan 09 '24 14:01 mhan8796

Thanks for the update and patience. I am trying to include your code snippet in the plugin example so that I have runnable code, but it isn't letting me properly replicate it, thinking I might be missing to properly represent the behavior. Can you provide the code sample in terms of or with respect to plugin example itself so that we can make it issue actionable ?

Also, to make sure the implementation is correct, are you already following as below:

  • Given an ordered list of documents, the methods startAt and startAfter let you move to the document with a specific field value in the list (not to a numeric offset within the list).

  • you'd need to do something like orderBy('name').startAfter('three') to skip to the entries with a name field that is > 'three'.

darshankawar avatar Jan 10 '24 11:01 darshankawar

@darshankawar I refactored the entire widget. Try this widget as a whole scaffold.

I am using startAtDocument code, not really startAt or startAfter, but I will keep them in mind for future reference.

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:go_router/go_router.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:http/http.dart' as http;
import 'package:intl/intl.dart';
import 'package:share_plus/share_plus.dart';
import 'package:flutter/services.dart';

import 'dart:convert';

class ViewPost extends StatefulWidget {
  final String id;
  final Map<String, dynamic>? post;

  const ViewPost({
    Key? key,
    required this.id,
    this.post,
  }) : super(key: key);

  @override
  State<ViewPost> createState() => _ViewPostState();
}

class _ViewPostState extends State<ViewPost> {
  DocumentSnapshot? _lastDocument; // Track the last loaded document

  final int _pageSize = 10; // Number of items to load per page
  final DateFormat _dateFormat = DateFormat('MMM d, yyyy');
  final _formKey = GlobalKey<FormState>();
  final ScrollController _scrollController = ScrollController();
  final List<Map<String, dynamic>> _comments = [];

  @override
  void initState() {
    super.initState();

    _loadCmts();

    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
        _loadCmts();
      }
    });
  }

  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }

  Future<void> _loadCmts() async {
    final db =
        Provider.of<VocoDataProvider>(context, listen: false).getFirestore();

    Query? query =
        db.collection('comments').orderBy('timestamp', descending: true);

    if (_lastDocument != null) {
      query = query.startAfterDocument(_lastDocument!);
    }
    query = query.limit(_pageSize);

    try {
      QuerySnapshot querySnapshot = await query.get();

      // Convert the QuerySnapshot to a List of DocumentSnapshot
      List<DocumentSnapshot> cmtDocs = querySnapshot.docs;

      if (cmtDocs.isNotEmpty) {
        _lastDocument = cmtDocs.last;
      }

      if (!kReleaseMode) {
        debugPrint('comments docs are : ${cmtDocs}');
      }

      for (var doc in cmtDocs) {
        if (!kReleaseMode) {
          debugPrint('id is  : ${doc.id}');
        }

        final cmtDocData = doc.data() as Map<String, dynamic>;
        cmtDocData['id'] = doc.id;

        if (!_comments.any((comment) => comment['id'] == doc.id)) {
          if (mounted) {
            setState(() {
              _comments.add(cmtDocData);
            });
          }
        }
      }
    } catch (e) {
      if (!kReleaseMode) {
        debugPrint('Error getting comments in view_post: $e');
      }
    }

    if (!kReleaseMode) {
      // debugPrint('comments are : $_comments');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(fit: StackFit.loose, alignment: Alignment.center, children: [
        ListView(controller: _scrollController, children: [
          Stack(fit: StackFit.loose, alignment: Alignment.center, children: [
            Container(
              constraints: BoxConstraints(maxWidth: maxWebWidth),
              child: Column(
                children: [
                  const Text(
                    'Comments',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: 16,
                    ),
                  ),
                  ...List.generate(_comments.length, (index) {
                    Offset? offset;
                    Widget? cmtDisplay;

                    List<Widget> columnWidget = [
                      Text(
                        '${_comments[index]['from_display_name']}  - ${_dateFormat.format(
                          (_comments[index]['timestamp'] as Timestamp).toDate(),
                        )}',
                        softWrap: true,
                        textAlign: TextAlign.center,
                      ),
                    ];

                    if (_comments[index]['type'] == 'cmtText') {
                      columnWidget.add(
                        Text(
                          '${_comments[index]['msg']}',
                          softWrap: true,
                        ),
                      );
                    }

                    cmtDisplay = Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisSize: MainAxisSize.min,
                      children: columnWidget,
                    );

                    return Column(
                      children: [
                        const Divider(
                          indent: 20,
                          endIndent: 20,
                          height: 20,
                        ),
                        Padding(
                          padding: const EdgeInsets.all(10),
                          child: Row(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Expanded(
                                child: InkWell(
                                    splashColor: Colors.blue.withAlpha(30),
                                    onLongPress: () {},
                                    onTap: () {},
                                    child: cmtDisplay),
                              ),
                              const SizedBox(
                                width: 16,
                              ),
                            ],
                          ),
                        ),
                      ],
                    );
                  }),
                  const SizedBox(
                    height: 100,
                  ),
                ],
              ),
            )
          ])
        ]),
      ]),
    );
  }
}

mhan8796 avatar Jan 11 '24 03:01 mhan8796

Thanks for the update. Keeping the issue open for team's input on reported and expected behavior as I am not sure if this would be actionable from plugin side or from native.

darshankawar avatar Jan 12 '24 11:01 darshankawar

I just tested this on iOS and web, the result was the same. I used this code to test:

// Last doc in collection
final docSnap = await FirebaseFirestore.instance
                      .collection('flutter-tests')
                      .doc('yoyoyoyoy')
                      .get();
                      print('object: ${docSnap.data()}');
                  final result = await FirebaseFirestore.instance
                      .collection('flutter-tests')
                      .startAfterDocument(docSnap)
                      .get();
                  print('RRRR: ${result.docs.length}');

Result for both platforms:

object: {name: John Something}
RRRR: 0

Not sure if this was fixed in web refactor for WASM support or wasn't an issue in the first place. Closing as there is nothing to fix.

russellwheatley avatar Jul 05 '24 10:07 russellwheatley