The code I have written works with Firebase and Firestore and is for signing in a user and immediately checking Firestore to retrieve data regarding that user (the current user).
The results I have received indicate that the code for requesting and retrieving data from Firestore is flawed because instead of confirming known data, it retrieves or confirms null only.
I have edited my Firestore security rules from a position where the error message was that permission was not granted - so I suspect that the permissions situation is right now, leaving me to understand that it must be the code.
My Firestore is made up of 1 collection and around 10 sub-collections:
users (the main collection)
userA, userB (subcollections of the main collection)
The userA and userB subcollections consist of uniformly drafted subcollections featuring fields that confirm the current user's e-mail address.
At the time of the data-request to Firestore requesting data on the basis of the current user's e-mail address, the documents/details to be retrieved will be within subcollection "userA" or subcollection "userB". In each subcollection, the current user's email address is confirmed twice under two different field names. This could mean that the data request should retrieve two documents per call.
So, should the data-request-call to Firestore, looking within my users collection, yield data from either of its subclasses, or, should its response be null?
Is there a path statement for Flutter (when writing a data-request) that leads to a collection and its subclasses being searched in order to retrieve the data?
Generally, what should I do to change the result from "null" to values from within my Firestore, please?
My code and its result are as follows:
W/System (12345): Ignoring header X-Firebase-Locale because its value was null.
D/FirebaseAuth(32536): Notifying id token listeners about user ( ********************** ).
I/flutter (12345): []
And
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart' as auth;
import '../widgets/textfield_try.dart';
class LoginScreen extends StatelessWidget {
LoginScreen({super.key, this.onTap});
TextEditingController emailController = TextEditingController();
TextEditingController passwordController = TextEditingController();
auth.FirebaseAuth _auth = auth.FirebaseAuth.instance;
FirebaseFirestore _dB = FirebaseFirestore.instance;
String? numberX;
Function()? onTap;
static const source = Source.serverAndCache;
Future<void> signUserIn() async {
await auth.FirebaseAuth.instance.signInWithEmailAndPassword(
email: emailController.text, password: passwordController.text);
if (_auth != null) {
final docRef = await _dB.collection('users').where('${_auth.currentUser!.email}', isEqualTo: true).get(GetOptions(source: source)).then((value) {
if (value != null) {
final userData = value.docs.toList();
print(userData);
}
});
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
resizeToAvoidBottomInset: false,
backgroundColor: Colors.blue[900],
body: SafeArea(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(padding: EdgeInsets.only(top: 0, bottom: 37)),
TextField_Try(
hintText: 'Username',
obscureText: false,
controller: emailController),
Padding(padding: EdgeInsets.only(top: 0, bottom: 0)),
Padding(
padding: EdgeInsets.only(
top: 8.0, left: 8.0, right: 8.0, bottom: 28.0),
child: TextField_Try(
hintText: '**************',
obscureText: true,
controller: passwordController,
)),
Padding(padding: EdgeInsets.only(bottom: 10)),
ElevatedButton(onPressed: signUserIn, child: Text('Sign In')),
Padding(padding: EdgeInsets.only(bottom: 25)),
],
),
),
),
),
);
}
}
The photos confirm the structure of my Firestore - as explained above.
I have shown fields and details for collection userA. Collection userB contains exactly the same fields but userB is empty of data, so that there is data for one user only within the collection. This mirrors the real situation.
The query is meant to search "users" (which includes "userA" and "userB"), and by entering the user's email address, the expected result is for two documents to be retrieved - one where the e-mail field is called "loginEmail" and another where the field is called "eAddress". Both of the documents expected to be retrieved contain other data that I would like to later tap into - but as a first step, get those documents.
Thank you for considering my questions.