Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cloud_Firestore store query returns null when the app is used in a different time zone : Flutter #2685

Closed
momo1606 opened this issue Jun 2, 2020 · 5 comments
Labels

Comments

@momo1606
Copy link

momo1606 commented Jun 2, 2020

I have developed an app in UAE(GMT +4 gulf standard time). When the following query is executed with system time zone being as mentioned above , i get the required data. When the app is used in a different country or if i change my system time to a different time zone(i changed it to GMT+5:30 Indian standard time), the query returns null and i get an error when extracting the data.

Future getdetails() async {
  DateTime dt=DateTime.now().toUtc();
  QuerySnapshot r = await Firestore.instance.collection('maqarat_male').
  where('date',isEqualTo: Timestamp.fromDate(DateTime(dt.year,dt.month,dt.day))).getDocuments();

  print(r.documents[0].documentID);
  return r;

}

Data is present in cloud firestore no matter what time is fetched using DateTime.now().
Even if I use DateTime.now().toUtc() i get the same error.

i also queried the above using static values instead of DateTime generated values.

Future getdetails() async {
  //DateTime dt=DateTime.now().toUtc();
  QuerySnapshot r = await Firestore.instance.collection('maqarat_male').
  where('date',isEqualTo: Timestamp.fromDate(DateTime(2020,6,2))).getDocuments();

  print(r.documents[0].documentID);
  return r;

}

now with my system time being set to time zone GMT+4 i get the data and am able to execute the print statement
image

and after changing my system time to a different timezone(this time GMT+3 Arabian Standars Time) i ran across this error while executing the same function.

image

Is the issue with the cloud_function package? i am using cloud_firestore: ^0.13.5

i could also create a document in firestore with any time zone.
So the problem is with the read operations.

these are the logs

E/flutter ( 9032): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: RangeError (index): Invalid value: Valid value range is empty: 0
E/flutter ( 9032): #0      List.[] (dart:core-patch/growable_array.dart:146:60)
E/flutter ( 9032): #1      getdetails (package:mahadalzahra/main.dart:96:20)
E/flutter ( 9032): <asynchronous suspension>
E/flutter ( 9032): #2      MyApp.build (package:mahadalzahra/main.dart:104:5)
E/flutter ( 9032): #3      StatelessElement.build (package:flutter/src/widgets/framework.dart:4576:28)
E/flutter ( 9032): #4      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4502:15)
E/flutter ( 9032): #5      Element.rebuild (package:flutter/src/widgets/framework.dart:4218:5)
E/flutter ( 9032): #6      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4481:5)
E/flutter ( 9032): #7      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4476:5)
E/flutter ( 9032): #8      Element.inflateWidget (package:flutter/src/widgets/framework.dart:3446:14)
E/flutter ( 9032): #9      Element.updateChild (package:flutter/src/widgets/framework.dart:3214:18)
E/flutter ( 9032): #10     RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1148:16)
E/flutter ( 9032): #11     RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1119:5)
E/flutter ( 9032): #12     RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:1061:17)
E/flutter ( 9032): #13     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2607:19)
E/flutter ( 9032): #14     RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1060:13)
E/flutter ( 9032): #15     WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:941:7)
E/flutter ( 9032): #16     WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:922:7)
E/flutter ( 9032): #17     _rootRun (dart:async/zone.dart:1180:38)
E/flutter ( 9032): #18     _CustomZone.run (dart:async/zone.dart:1077:19)
E/flutter ( 9032): #19     _CustomZone.runGuarded (dart:async/zone.dart:979:7)
E/flutter ( 9032): #20     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1019:23)
E/flutter ( 9032): #21     _rootRun (dart:async/zone.dart:1184:13)
E/flutter ( 9032): #22     _CustomZone.run (dart:async/zone.dart:1077:19)
E/flutter ( 9032): #23     _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:1003:23)
E/flutter ( 9032): #24     Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:23:15)
E/flutter ( 9032): #25     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:398:19)
E/flutter ( 9032): #26     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:429:5)
E/flutter ( 9032): #27     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)
E/flutter ( 9032): 
E/flutter ( 9032): [ERROR:flutter/shell/common/shell.cc(213)] Dart Error: Unhandled exception:
E/flutter ( 9032): RangeError (index): Invalid value: Valid value range is empty: 0
E/flutter ( 9032): #0      List.[] (dart:core-patch/growable_array.dart:146:60)
E/flutter ( 9032): #1      getdetails (package:mahadalzahra/main.dart:96:20)
E/flutter ( 9032): <asynchronous suspension>
E/flutter ( 9032): #2      MyApp.build (package:mahadalzahra/main.dart:104:5)
E/flutter ( 9032): #3      StatelessElement.build (package:flutter/src/widgets/framework.dart:4576:28)
E/flutter ( 9032): #4      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4502:15)
E/flutter ( 9032): #5      Element.rebuild (package:flutter/src/widgets/framework.dart:4218:5)
E/flutter ( 9032): #6      BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2627:33)
E/flutter ( 9032): #7      WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:883:20)
E/flutter ( 9032): #8      RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:284:5)
E/flutter ( 9032): #9      SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1113:15)
E/flutter ( 9032): #10     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1052:9)
E/flutter ( 9032): #11     SchedulerBinding.scheduleWarmUpFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:861:7)
E/flutter ( 9032): #12     Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:23:15)
E/flutter ( 9032): #13     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:398:19)
E/flutter ( 9032): #14     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:429:5)
E/flutter ( 9032): #15     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)
@TahaTesser
Copy link

Hi @momo1606
Can you please provide your flutter doctor -v, your flutter run --verbose and a minimal complete reproducible code sample.
Please refrain from screenshots if not needed
Thank you"

@TahaTesser TahaTesser added the blocked: customer-response Waiting for customer response, e.g. more information was requested. label Jun 2, 2020
@momo1606
Copy link
Author

momo1606 commented Jun 2, 2020

Hi @momo1606
Can you please provide your flutter doctor -v, your flutter run --verbose and a minimal complete reproducible code sample.
Please refrain from screenshots if not needed
Thank you"

C:\Users\G5\AndroidStudioProjects\akhul_qurani>flutter doctor -v
[√] Flutter (Channel stable, v1.17.1, on Microsoft Windows [Version 10.0.17763.1217], locale en-US)
• Flutter version 1.17.1 at C:\flutter\flutter
• Framework revision f7a6a7906b (3 weeks ago), 2020-05-12 18:39:00 -0700
• Engine revision 6bc433c6b6
• Dart version 2.8.2

C:\Users\G5\AndroidStudioProjects\akhul_qurani>
C:\Users\G5\AndroidStudioProjects\akhul_qurani>flutter doctor -v
[√] Flutter (Channel stable, v1.17.1, on Microsoft Windows [Version 10.0.17763.1217], locale en-US)
• Flutter version 1.17.1 at C:\flutter\flutter
• Framework revision f7a6a7906b (3 weeks ago), 2020-05-12 18:39:00 -0700
• Engine revision 6bc433c6b6
• Dart version 2.8.2

[!] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
• Android SDK at C:\Users\G5\AppData\Local\Android\sdk
• Platform android-29, build-tools 29.0.3
• Java binary at: D:\Android Studio\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)
! Some Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses

[√] Android Studio (version 3.6)
• Android Studio at D:\Android Studio
• Flutter plugin version 44.0.2
• Dart plugin version 192.7761
• Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)

[!] IntelliJ IDEA Ultimate Edition (version 2018.3)
• IntelliJ at C:\JetBrains\IntelliJ IDEA 2018.3.2
X Flutter plugin not installed; this adds Flutter specific functionality.
X Dart plugin not installed; this adds Dart specific functionality.
• For information about installing plugins, see
https://flutter.dev/intellij-setup/#installing-the-plugins

[√] VS Code (version 1.45.1)
• VS Code at C:\Users\G5\AppData\Local\Programs\Microsoft VS Code
• Flutter extension version 3.11.0

[!] Connected device
! No devices available

! Doctor found issues in 3 categories.

flutter run --verbose
verbose.txt

@momo1606
Copy link
Author

momo1606 commented Jun 2, 2020

Hi @momo1606
Can you please provide your flutter doctor -v, your flutter run --verbose and a minimal complete reproducible code sample.
Please refrain from screenshots if not needed
Thank you"

Code- this is what i executed for test purpose

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
      .then((_) {
    runApp(new MyApp());
  });
}

Future getdetails() async {
  DateTime dt=DateTime.now().toUtc();
  QuerySnapshot r = await Firestore.instance.collection('maqarat_male').
  where('date',isEqualTo: Timestamp.fromDate(DateTime(dt.year,dt.month,dt.day))).getDocuments();

  print(r.documents[0].documentID);
  return r;

}
class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context){
    getdetails();
    return MaterialApp(
      title: 'title',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        scaffoldBackgroundColor: Color(0xFFFFFFFF),
        primaryColor: Color(0xFF003CE7),
      ),
      home: SplashScreen(),
    );

  }

}

@TahaTesser TahaTesser added plugin: cloud_firestore type: bug Something isn't working and removed blocked: customer-response Waiting for customer response, e.g. more information was requested. labels Jun 3, 2020
@TahaTesser
Copy link

flutter doctor -v
[✓] Flutter (Channel dev, 1.19.0-3.0.pre, on Mac OS X 10.15.5 19F96, locale
    en-GB)
    • Flutter version 1.19.0-3.0.pre at /Users/tahatesser/Code/flutter_dev
    • Framework revision 6135091de9 (31 hours ago), 2020-06-01 17:17:03 -0700
    • Engine revision e39301f23f
    • Dart version 2.9.0 (build 2.9.0-11.0.dev 6489a0c68d)

 
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
    • Android SDK at /Users/tahatesser/Code/sdk
    • Platform android-29, build-tools 29.0.3
    • 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.5)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.5, Build version 11E608c
    • 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 45.1.2
    • Dart plugin version 193.7361
    • Java version OpenJDK Runtime Environment (build
      1.8.0_242-release-1644-b3-6222593)

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

[✓] Connected device (4 available)
    • iPhone 11  • 0EC80516-8F99-4A0C-89ED-2273579862F9 • ios            •
      com.apple.CoreSimulator.SimRuntime.iOS-13-5 (simulator)
    • macOS      • macOS                                • darwin-x64     • Mac
      OS X 10.15.5 19F96
    • Web Server • web-server                           • web-javascript •
      Flutter Tools
    • Chrome     • chrome                               • web-javascript •
      Google Chrome 83.0.4103.61

• No issues found!

@greghesp
Copy link
Contributor

greghesp commented Jul 13, 2020

@momo1606

Ive tested this with the rework that is currently in progress

When you pass isEqualTo: Timestamp.fromDate(DateTime(dt.year,dt.month,dt.day))), it creates a timestamp at the start of that day such as 2020-07-13 00:00:00.000.

As you're using "isEqualTo" it's looking for that exact timestamp.

If you want to get all documents for the whole day, you need to use multiple where statements like so

QuerySnapshot querySnapshot = await Firestore.instance.collection('myCollection')
    .where('createdAt', isGreaterThanOrEqualTo: Timestamp.fromDate(DateTime(dt.year, dt.month, dt.day)))
    .where('createdAt', isLessThanOrEqualTo: Timestamp.fromDate(DateTime(dt.year, dt.month, dt.day + 1)))
    .getDocuments();

Can you confirm this works please, and if you still experience the issue, report back,

@Ehesp Ehesp closed this as completed Jul 13, 2020
@firebase firebase locked and limited conversation to collaborators Aug 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants