0

With all the hype of Getx, I choose to use it in my first ever project, unfortunately, there aren't many tutorials about getx compared to others. This is my first build, how can I create a user id as a document id, I am following MVC pattern and this is the controller of that build.

Here is my code.

import 'package:firebase_auth/firebase_auth.dart';
import 'package:get/get.dart';

import 'package:wlykmain/views/homepage.dart';
import 'package:wlykmain/views/login.dart';

class FirebaseController extends GetxController {
  bool isHidden = true;

  void toggleBetween() {
    isHidden = !isHidden;
    update();
  }

  FirebaseAuth _auth = FirebaseAuth.instance;

  Rx<User> _firebaseUser = Rx<User>();

  String get user => _firebaseUser.value?.email;

  @override
  // ignore: must_call_super
  void onInit() {
    _firebaseUser.bindStream(_auth.authStateChanges());
  }

  void signUp(
    String firstname,
    String lastname,
    String email,
    String password,
  ) async {
    CollectionReference reference =
        FirebaseFirestore.instance.collection("Users");
    String uid = _auth.currentUser.uid;
    reference.doc(uid).set({});
    Map<String, dynamic> userdata = {
      "First Name": firstname,
      "Last Name": lastname,
      "Email": email,
      "uid": uid
    };

    await _auth
        .createUserWithEmailAndPassword(email: email, password: password)
        .then((value) {
      reference.add(userdata).then((value) => (Get.offAll(LoginPage())));
    }).catchError(
      (onError) => Get.snackbar(
        'Error creating account',
        onError.message,
        snackPosition: SnackPosition.TOP,
      ),
    );
  }

  void login(String email, String password) async {
    CollectionReference reference =
        FirebaseFirestore.instance.collection("Users");
    String uid = _auth.currentUser.uid;
    reference.doc(uid).set({"uid": uid});

    return await _auth
        .signInWithEmailAndPassword(email: email, password: password)
        // ignore: unnecessary_statements
        .then((value) {
      (Get.offAll(MyHome()));
    }).catchError(
      (onError) => Get.snackbar('Error while logging in', onError.message),
    );
  }

  void signOut() async {
    await _auth.signOut().then((value) => Get.offAll(LoginPage()));
  }
}















halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

2

You should replace add() with doc(uid).set() in your signUp() method:

void signUp(
    String firstname,
    String lastname,
    String email,
    String password,
  ) async {
    // ... 

    await _auth
        .createUserWithEmailAndPassword(email: email, password: password)
        .then((value) {
      reference.doc(uid).set(userdata).then((value) => (Get.offAll(LoginPage())));
    }).catchError(
      (onError) => Get.snackbar(
        'Error creating account',
        onError.message,
        snackPosition: SnackPosition.TOP,
      ),
    );
  }

add() generates a new key for that Cloud Firestore document, while doc() lets you specify a key.

halfer
  • 19,824
  • 17
  • 99
  • 186