I am trying to log a user in with the boolean value assigned to the 'isVerified' field in the users firestore document. Logic: If 'isVerified' is true then continue, else return to verify page.
I put in debugPrint statements to help me catch the error and it appears that the StreamBuilder for the document reference is being skipped over without displaying an error.
I copy pasted the collection reference from another file and changed it to be a document reference, I think I messed up some of the code. I have read other documentation to regarding stream builders but I can't find where I'm going wrong, please let me know if there's anything I can clarify. Thank you
My Code:
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
late int currentScreen;
// THESE SCREENS ARE FOUND IN THE NET FOLDER
List<Widget> screens = <Widget>[
const Login(),
const Verify(),
const Reset(),
const UserPage(),
];
@override
Widget build(BuildContext context) {
User? user = auth.currentUser;
currentScreen = 0;
// A: CHECK LOGIN STATUS OF USER AND DECIDE WHICH SCREEN TO SHOW USER
// ignore: unnecessary_null_comparison
if (user == null) {
debugPrint('>> Home: No user recognized');
// A: USER IS NOT LOGGED IN, SHOW THE SIGN IN SCREEN
currentScreen = 0;
} else {
debugPrint('>> Home: User recognized');
// A: Check user data for verification
final DocumentReference<Map<String, dynamic>> userReference =
FirebaseFirestore.instance.collection('users').doc(user.uid);
// A: Use this streambuilder to check if the users account has been veriified or not
// A: Check users document for 'isVerified'
debugPrint('>> Home: Before Stream Builder');
// THIS IS THE PART THAT SEEMINGLY GETS SKIPPED <----------------------------------------
StreamBuilder(
stream: userReference.snapshots(),
builder: (BuildContext context, AsyncSnapshot<dynamic> userSnapshot) {
if (!userSnapshot.hasData) {
currentScreen = 0;
debugPrint('>> test3');
return const Text('No data');
}
if (userSnapshot.hasError) {
currentScreen = 0;
debugPrint('>> test4');
return const Text('Snapshot error.');
}
final userData = userSnapshot.requireData;
return ListView.builder(
// itemCount: userData.size,
itemBuilder: (context, index) {
// ignore: unused_local_variable
bool isVerified = userData[index]['isVerified'];
// Send user to homepage
if (isVerified == true) {
currentScreen = 3;
}
// Send user to verify page
if (isVerified != true) {
currentScreen = 1;
}
// A: Show error and redisplay the sign in page
else {
currentScreen = 0;
}
return const Text('');
// return Scaffold(
// // BODY OF CONTENT
// body: Center(
// child: screens.elementAt(currentScreen),
// ),
// );
});
// ToDo: A: Maybe return the current screen scaffold here, see if it that's needed or not
// return const Text('');
},
);
}
// currentScreen = 0;
return Scaffold(
// BODY OF CONTENT
body: Center(
child: screens.elementAt(currentScreen),
),
);
}
}
CodePudding user response:
FirebaseFirestore.instance.collection('users').doc(user.uid).where('your filed', isEqualTo: 1).get();