0

I'm really struggling with flutter to get the data from Firebase. I have a collection 'vardata' , and I want to retrieve some data, I have some fields like

levelG1
"1.74"
(string)
levelG8
"1.39"
levelS1
"1.16"
levelS8
"0.93"

So I created a class:

import 'package:cloud_firestore/cloud_firestore.dart';

class GetData {
  readDatabase() {
    return Firestore.instance
        .collection('vardata')
        .where('hourRate', isEqualTo: '4.43')
        .getDocuments();
  }
}

On the main screen I call this before build the UI with the widgets obviously

var dbvalue;

  @override
  void initState() {
    super.initState();
    GetData().readDatabase().then((QuerySnapshot docs) {
      if (docs.documents.isNotEmpty) {
        dbvalue = docs.documents;
        print(dbvalue['levelD1']);
      }
    });
  }

and the input I get in my console is

Performing hot restart...
Syncing files to device iPhone 8...
Restarted application in 1,963ms.
    [C10.1 CCEC0E54-0E6F-4882-B61F-1EAB7196F2F3 10.10.114.127:65261<->216.58.207.202:443]
    Connected Path: satisfied (Path is satisfied), interface: en0
    Duration: 0.399s, DNS @0.001s took 0.015s, TCP @0.017s took 0.044s, TLS took 0.080s
    bytes in/out: 5454/1258, packets in/out: 9/11, rtt: 0.041s, retransmitted packets: 0, out-of-order packets: 0
    [C11.1 40207A2B-FD04-4340-92C4-57D44DD55DB6 10.10.114.127:65264<->172.217.21.138:443]
    Connected Path: satisfied (Path is satisfied), interface: en0
    Duration: 0.360s, DNS @0.000s took 0.010s, TCP @0.011s took 0.041s, TLS took 0.080s
    bytes in/out: 4836/2067, packets in/out: 9/10, rtt: 0.039s, retransmitted packets: 0, out-of-order packets: 0
flutter: admin@admin.com
flutter: admin123
flutter: 0.0
[VERBOSE-2:ui_dart_state.cc(157)] Unhandled Exception: Bad state: Future already completed
#0      _AsyncCompleter.complete (dart:async/future_impl.dart:41:31)
#1      Route.didComplete (package:flutter/src/widgets/navigator.dart:208:19)
#2      NavigatorState.pushReplacement.<anonymous closure> (package:flutter/src/widgets/navigator.dart:1890:13)
#3      TickerFuture.whenCompleteOrCancel.thunk (package:flutter/src/scheduler/ticker.dart:398:15)
#4      _rootRunUnary (dart:async/zone.dart:1134:38)
#5      _CustomZone.runUnary (dart:async/zone.dart:1031:19)
#6      _FutureListener.handleValue (dart:async/future_impl.dart:139:18)
#7      Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:680:45)
#8      Future._propagateToListeners (dart:async/future_impl.dart:709:32)
#9      Future._completeWithValue (dart:async/future_impl.dart:524:5)
#10     Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:554:7)
#11     _rootRun (dart:async/zone.dart:1126:13)
#12     <…>
[VERBOSE-2:ui_dart_state.cc(157)] Unhandled Exception: PlatformException(Error 7, FIRFirestoreErrorDomain, Missing or insufficient permissions.)
#0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
#1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:321:33)
<asynchronous suspension>
#2      MethodChannel.invokeMapMethod (package:flutter/src/services/platform_channel.dart:349:48)
#3      MethodChannelQuery.getDocuments (package:cloud_firestore_platform_interface/src/method_channel/method_channel_query.dart:87:46)
#4      Query.getDocuments (package:cloud_firestore/src/query.dart:37:34)
#5      GetData.readDatabase (package:playtech/services/readDataBase.dart:8:10)
#6      _HomeScreenState.initState (package:playtech/home_screen.dart:98:15)
#7      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4355:58)
#8      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
#9      Elem<…>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    Do you use other Firebase related code? It seems that there's an exception related to the permission to you Firebase account. – Unicone Jun 21 '20 at 01:39
  • i have only a login page , so authentication –  Swift newbie Jun 21 '20 at 01:57
  • 1
    The exception means that your user doesn't have permission to read the data that they're trying to access according to the [security rules of your database](https://firebase.google.com/docs/firestore/security/get-started). I also added some questions about the same topic, with various approaches to address it. – Frank van Puffelen Jun 21 '20 at 02:11
  • rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { match /{document=**} { allow read, write: if true; } } } –  Swift newbie Jun 21 '20 at 02:12
  • Did you copy the generated `google-services.json` file from your Firebase project? – Unicone Jun 21 '20 at 02:22

0 Answers0