【问题标题】:Sign In two different types of users两种不同类型的用户
【发布时间】:2020-06-24 09:31:47
【问题描述】:

我正在使用 Flutter 和 Firebase 开发一个用于家庭学习的应用程序,但我的登录屏幕出现问题,因为我想将教师和学生发送到不同的个人资料屏幕,但我无法为此制定一个好的逻辑。我尝试分配一个字符串,该字符串将被比较以将每个用户发送到各自的屏幕,但它不起作用,因为它为我提供了 null。这是我的登录屏幕的代码。

import 'package:awesome_dialog/awesome_dialog.dart';
import 'package:etutor/Animation/FadeAnimation.dart';
import 'package:etutor/ReusableMaterial/BackArrow.dart';
import 'package:etutor/ReusableMaterial/Constants.dart';
import 'package:etutor/ReusableMaterial/Loading.dart';
import 'package:etutor/ReusableMaterial/ReusableButton.dart';
import 'package:etutor/ReusableMaterial/ReusableCard.dart';
import 'package:etutor/Screens/Index.dart';
import 'package:etutor/Screens/ProfileSelection.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

class SignIn extends StatefulWidget {
  @override
  _SignInState createState() => _SignInState();
}

class _SignInState extends State<SignIn> {
  final FirebaseAuth _auth = FirebaseAuth.instance;
  final Firestore _firestore = Firestore.instance;

  String email;
  String password;
  bool pass1 = true;
  String userType;
  String loggedInUserID;
  bool loading = false;

  void showErrorMessage(String title, String description) {
    AwesomeDialog(
      context: context,
      dialogType: DialogType.ERROR,
      animType: AnimType.BOTTOMSLIDE,
      tittle: title,
      desc: description,
    ).show();
  }

  Future getUserType() async {
    userType = (await _firestore
        .collection('userData')
        .document(loggedInUserID)
        .get()) as String;
    print(userType);
  }

  @override
  Widget build(BuildContext context) {
    return loading
        ? Loading()
        : Scaffold(
            backgroundColor: kBackgroundColor,
            body: SafeArea(
              child: ListView(
                children: <Widget>[
                  SizedBox(height: 35),
                  FadeAnimation(
                    0.5,
                    Center(
                      child: Text(
                        'Sign In',
                        style: kHeadingStyle,
                      ),
                    ),
                  ),
                  BackArrow(),
                  SizedBox(
                    height: 15,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      FadeAnimation(
                        0.5,
                        Text(
                          'Enter your details to access your Account.',
                          style: kTextLineStyle,
                        ),
                      ),
                    ],
                  ),
                  SizedBox(height: 45),
                  Padding(
                    padding: const EdgeInsets.only(left: 30, right: 30),
                    child: FadeAnimation(
                      0.5,
                      Container(
                        decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(10),
                            color: Colors.white,
                            boxShadow: [
                              BoxShadow(
                                color: Color.fromRGBO(84, 104, 255, .3),
                                blurRadius: 20,
                                offset: Offset(0, 10),
                              )
                            ]),
                        child: Column(
                          children: <Widget>[
                            ReusableCard(
                              onChanged: (value) {
                                email = value;
                              },
                              hintLabel: 'Email',
                            ),
                            ReusablePasswordCard(
                              onChanged: (value) {
                                password = value;
                              },
                              hintLabel: 'Password',
                              pass: pass1,
                              onPress: () {
                                setState(() {
                                  pass1 = !pass1;
                                });
                              },
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                  SizedBox(
                    height: 20,
                  ),
                  Row(
                    children: <Widget>[
                      Padding(
                        padding: EdgeInsets.only(left: 28),
                      ),
                      FadeAnimation(
                        0.5,
                        FlatButton(
                          child: Text(
                            'Forgot Password ?',
                            style: TextStyle(
                              fontFamily: 'Roboto',
                              fontSize: 15,
                              color: kBlueColor,
                            ),
                          ),
                          onPressed: () {},
                        ),
                      ),
                    ],
                  ),
                  SizedBox(
                    height: 30,
                  ),
                  FadeAnimation(
                    0.5,
                    Center(
                      child: Text(
                        'Don\'t have an Account ?',
                        style: TextStyle(
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                          color: Color(0xFF546270),
                        ),
                      ),
                    ),
                  ),
                  FadeAnimation(
                    0.5,
                    Center(
                      child: FlatButton(
                        onPressed: () {
                          Navigator.push(
                            context,
                            MaterialPageRoute(
                              builder: (context) => ProfileSelection(),
                            ),
                          );
                        },
                        child: Text(
                          'SIGN UP',
                          style: TextStyle(
                            color: kBlueColor,
                          ),
                        ),
                      ),
                    ),
                  ),
                  SizedBox(
                    height: 15,
                  ),
                  FadeAnimation(
                    0.5,
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        ReusableButton(
                          label: 'Continue',
                          onPress: () async {
                            if (!email.contains('@') ||
                                email == null ||
                                !email.contains('.com')) {
                              showErrorMessage('Invalid Email',
                                  'The email you entered is wrong, please check your email format and try again');
                            } else if (password == null) {
                              showErrorMessage('Invalid Password',
                                  'The password field is empty');
                            } else {
                              try {
                                setState(() {
                                  loading = true;
                                });

                                FirebaseUser signedInUser =
                                    (await _auth.signInWithEmailAndPassword(
                                            email: email, password: password))
                                        .user;
                                if (!signedInUser.isEmailVerified) {
                                  setState(() {
                                    loading = false;
                                  });
                                  AwesomeDialog(
                                          context: context,
                                          dialogType: DialogType.ERROR,
                                          animType: AnimType.BOTTOMSLIDE,
                                          tittle: 'Error!',
                                          desc:
                                              'Your email has not been verified please verify your email first')
                                      .show();
                                } else {
                                  if (signedInUser != null) {
                                    setState(() {
                                      loggedInUserID = signedInUser.uid;
//                                      print(loggedInUserID);
                                    });
                                  }

                                  while (loggedInUserID == null) {}
                                  if (loggedInUserID != null) {
                                    getUserType();
                                    setState(() {
                                      loading = false;
                                    });
                                    print(loggedInUserID);
                                    Navigator.push(
                                      context,
                                      MaterialPageRoute(
                                        builder: (context) =>
                                            Index(loggedInUserID),
                                      ),
                                    );
                                  }
                                }
                              } catch (e) {
                                print(e);
                                showErrorMessage('Error', e);
                              }
                            }
                          },
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          );
  }
}

【问题讨论】:

    标签: firebase flutter


    【解决方案1】:

    它返回 null 因为您的 getUserType() 函数是异步的,当您检查用户类型时,它不会等待您的函数完成并返回数据。

    您必须等待通过使用await 来检索数据

    if (loggedInUserID != null) {
        await getUserType();
        setState(() {
        loading = false;
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-09
      • 1970-01-01
      • 2019-08-11
      • 2018-01-16
      • 1970-01-01
      • 2020-05-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多