CodeTasy
CodeTasy

Reputation: 119

I want to check whether the document id specified by me exists or not

Hi I want to fetch the number first from firebase auth in a varibale name globalNumber which is set as document id in firebase and then use that to check whether the doc id exists or not. If exists then Navigate to some page else navigate to some other page as shown in my code. But its not working perfectly may the number is not being fetched correctly or there might be some other issue. I have checked some other similar question but its not working for me. Could anyone please let me know the correct code

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'dart:async';
import 'package:firebase_auth/firebase_auth.dart';

import 'package:loginpage/AccountDetialsPage.dart';

import 'package:loginpage/Pricepage.dart';



class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}


class _HomePageState extends State<HomePage> {

  var globalNumber;
  var firestore;
@override
  void initState() {
    // TODO: implement initState
    super.initState();
    getCurrentUser();
    firestore = Firestore.instance;

  }

  getCurrentUser() async{

    final FirebaseAuth _auth = FirebaseAuth.instance;
    final FirebaseUser user = await _auth.currentUser();
    globalNumber = user.phoneNumber;

}

  @override
  Widget build(BuildContext context) {
  print(globalNumber);
    return MaterialApp(
      home: SafeArea(
        child: Scaffold(
          body: Container(
            child: Center(child: (Text('Welcome '))),
          ),
          bottomNavigationBar: Container(
            height: 50,
            child: RaisedButton(
              color: Colors.red,
              child: (Text('Next',style: TextStyle(color: Colors.white),)),
              onPressed: () async{
                final snapShot = await Firestore.instance.collection('users').document(globalNumber).get();

                if (snapShot.exists){
                  Navigator.of(context).push(
                      MaterialPageRoute(
                          builder: (context) =>
                              Pricepage()
                      ));
                }
                else{
                  Navigator.of(context).push(
                      MaterialPageRoute(
                          builder: (context) =>
                              AccountDetialsPage()
                      ));
                }

              },
            ),
          ),
        ),
      ),
    );
  }
}

Question

How can I check whether the document id specified by me exists or not?

Upvotes: 1

Views: 74

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80914

Try the following:

 onPressed: () async{
    final FirebaseUser user = await _auth.currentUser();
    var globalNumb = user.phoneNumber;
    final snapShot = await Firestore.instance.collection('users').document(globalNumb).get();
    if (snapShot.exists){
            //..

inside onPressed retrieve the current user phoneNumber and then using exists check if the document exists or not.

Upvotes: 1

Related Questions