gpt4 book ai didi

firebase - Flutter和Firebase-在注册时收集其他数据

转载 作者:行者123 更新时间:2023-12-03 04:13:17 26 4
gpt4 key购买 nike

我正在使用方法createUserWithEmailAndPassword,但希望在用户注册时收集更多数据。除了电子邮件和密码,我还想收集:

  • 姓氏
  • 出生日期

  • 这是我的身份验证服务代码:
        //Email & Password Sign Up
    Future<String> createUserWithEmailAndPassword(
    String email, String password,
    ) async {
    final authResult = await _firebaseAuth.createUserWithEmailAndPassword(
    email: email,
    password: password,
    );

    // Update the username
    await updateUserName(email, authResult.user);
    return authResult.user.uid;

    }
    和我的注册页面:
    import 'package:easy_tiger/constants/appbar.dart';
    import 'package:easy_tiger/screens/account.dart';
    import 'package:easy_tiger/style.dart';
    import 'package:flutter/gestures.dart';
    import 'package:flutter/material.dart';
    import 'package:easy_tiger/services/auth_service.dart';

    enum AuthFormType { signIn, signUp }

    class SignUpPage extends StatefulWidget {
    final AuthFormType authFormType;

    SignUpPage({Key key, this.authFormType}) : super(key: key);

    @override
    _SignUpPageState createState() => _SignUpPageState(authFormType: this.authFormType);
    }

    class _SignUpPageState extends State<SignUpPage> {
    AuthFormType authFormType;


    _SignUpPageState({this.authFormType});

    final formKey = GlobalKey<FormState>();
    String _firstName,
    _lastName,
    _email,
    _confirmEmail,
    _password,
    _confirmPassword,
    _dateOfBirth;

    bool validate(){
    final form = formKey.currentState;
    form.save();
    if(form.validate()){
    form.save();
    return true;
    } else {
    return false;
    }
    }
    void switchFormState(String state) {
    formKey.currentState.reset();
    formKey.currentState.validate();
    if(state == 'signUp') {
    setState(() {
    authFormType = AuthFormType.signUp;
    });
    } else {
    setState(() {
    authFormType = AuthFormType.signIn;
    });
    }
    }

    void submit() async {
    if (validate()) {
    try {
    final auth = Provider
    .of(context)
    .auth;
    if (authFormType == AuthFormType.signIn) {
    String uid = await auth.signInWithEmailAndPassword(_email, _password);
    print("Signed In with ID $uid");
    Navigator.of(context).pushReplacementNamed('/home');
    } else {
    String uid = await auth.createUserWithEmailAndPassword(
    _email,
    _password,);
    print("Signed up with New ID $uid");
    Navigator.of(context).pushReplacementNamed('/home');
    }
    } catch (e) {
    print(e);
    }
    }
    }
    @override
    Widget build(BuildContext context) {
    return Scaffold(
    appBar: PreferredSize(
    preferredSize: const Size.fromHeight(80),
    child: MainAppBar(
    text: buildAppBarText(),
    )),
    body: SingleChildScrollView(
    child: Padding(
    padding: const EdgeInsets.all(12.0),
    child: Center(
    child: Container(
    child: Column(
    children: <Widget>[
    Align(
    child: Text(buildTitleText(), style: AppBarTextStyle),
    ),
    Container(
    padding: EdgeInsets.only(top: 10),
    ),
    Padding(
    padding: const EdgeInsets.all(20.0),
    child: Form(
    key: formKey,
    child: Column(
    children: buildInputs() + buildSwitchText(),
    )),
    ),
    ],
    ),
    ),
    ),
    ),
    ));
    }

    buildHeaderText() {
    String _headerText;
    if (authFormType == AuthFormType.signUp) {
    _headerText = "Don't have an account?";
    } else {
    _headerText = "Already have an account?";
    } return _headerText;
    }

    List<Widget> buildInputs() {
    List<Widget> textFields = [];

    if (authFormType == AuthFormType.signIn) {
    textFields.add(TextFormField(
    style: TextStyle(
    fontSize: 12.0,
    ),
    decoration: buildSignUpInputDecoration('Email'),
    validator: EmailValidator.validate,
    onSaved: (value) => _email = value
    ));
    textFields.add(SizedBox(
    height: 15,
    ));
    textFields.add(TextFormField(
    style: TextStyle(
    fontSize: 12.0,
    ),
    decoration: buildSignUpInputDecoration('Password'),
    obscureText: true,
    validator: PasswordValidator.validate,
    onSaved: (value) => _password = value,
    ),);
    }
    else {
    textFields.clear();
    //if we're in the sign up state, add name
    // add email & password
    textFields.add(TextFormField(
    style: TextStyle(
    fontSize: 12.0,
    ),
    decoration: buildSignUpInputDecoration('First Name'),
    onSaved: (value) => _firstName,
    ));
    textFields.add(SizedBox(
    height: 15,
    ));
    textFields.add(TextFormField(
    style: TextStyle(
    fontSize: 12.0,
    ),
    decoration: buildSignUpInputDecoration('Last Name'),
    onSaved: (value) => _lastName,
    ));
    textFields.add(SizedBox(
    height: 15,
    ));
    textFields.add(TextFormField(
    style: TextStyle(
    fontSize: 12.0,
    ),
    decoration: buildSignUpInputDecoration('Email Address'),
    onSaved: (value) => _email,
    ));
    textFields.add(SizedBox(
    height: 15,
    ));
    textFields.add(TextFormField(
    style: TextStyle(
    fontSize: 12.0,
    ),
    decoration: buildSignUpInputDecoration('Confirm Email Address'),
    onSaved: (value) => _confirmEmail,
    ));
    textFields.add(SizedBox(
    height: 15,
    ));
    textFields.add(TextFormField(
    style: TextStyle(
    fontSize: 12.0,
    ),
    decoration: buildSignUpInputDecoration('Password'),
    obscureText: true,
    onSaved: (value) => _password,
    ));
    textFields.add(SizedBox(
    height: 15,
    ));
    textFields.add(TextFormField(
    style: TextStyle(
    fontSize: 12.0,
    ),
    decoration: buildSignUpInputDecoration('Confirm Password'),
    onSaved: (value) => _confirmPassword,
    ));
    textFields.add(SizedBox(
    height: 15,
    ));
    textFields.add(TextFormField(
    style: TextStyle(
    fontSize: 12.0,
    ),
    decoration: buildSignUpInputDecoration('Date of Birth'),
    onSaved: (value) => _dateOfBirth,
    ));
    textFields.add(SizedBox(
    height: 15,
    ));
    }
    return textFields;
    }

    List<Widget> buildSwitchText() {
    String _switchButtonTextPart1, _switchButtonTextPart2,_newFormState;

    if(authFormType == AuthFormType.signIn) {
    _switchButtonTextPart1 = "Haven't got an account? ";
    _switchButtonTextPart2 = 'Sign Up';
    _newFormState = 'signUp';
    } else {
    _switchButtonTextPart1 = 'Already have an account? ';
    _switchButtonTextPart2 = 'Sign In';
    _newFormState = 'signIn';
    } return [
    SizedBox(height: 5.0),
    Container(
    width: MediaQuery.of(context).size.height * 0.7,
    child: RaisedButton(
    shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(5.0),),
    color: kPrimaryColor,
    textColor: Colors.black,
    child: Padding(
    padding: const EdgeInsets.all(8.0),
    child: Text(buildTitleText(),
    style: TextStyle(fontFamily: FontNameDefault,
    fontSize: 15.0),),
    ),
    onPressed: submit),
    ),
    SizedBox(height: 5.0,),
    RichText(
    text: TextSpan(
    style: TextStyle(
    fontFamily: FontNameDefault,
    color: Colors.black,
    fontSize: 12.0,
    ),
    children: <TextSpan>[
    TextSpan(
    text: _switchButtonTextPart1
    ),
    TextSpan(
    text: _switchButtonTextPart2,
    style: TextStyle(
    decoration: TextDecoration.underline,
    color: Colors.black,
    fontSize: 12.0),
    recognizer: TapGestureRecognizer()
    ..onTap = () {
    switchFormState(_newFormState);
    })
    ]),
    ),
    ];
    }

    String buildAppBarText() {
    String _switchAppBarHeader;

    if(authFormType == AuthFormType.signIn) {
    _switchAppBarHeader = "Already have an account?";
    } else {
    _switchAppBarHeader = "Don't have an account?";
    } return
    _switchAppBarHeader;
    }

    String buildTitleText() {
    String _switchTextHeader;

    if(authFormType == AuthFormType.signIn) {
    _switchTextHeader = "Sign In";
    } else {
    _switchTextHeader = "Sign Up";
    } return
    _switchTextHeader;
    }


    }
    class AccountController extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
    final AuthService auth = Provider.of(context).auth;
    return StreamBuilder(
    stream: auth.onAuthStateChanged,
    builder: (context, AsyncSnapshot<String> snapshot) {
    if (snapshot.connectionState == ConnectionState.active) {
    final bool signedIn = snapshot.hasData;
    return signedIn ? SignInPage() : SignUpPage();
    }
    return CircularProgressIndicator();
    });
    }
    }

    InputDecoration buildSignUpInputDecoration(String hint) {
    return InputDecoration(
    isDense: true,
    fillColor: Colors.white,
    hintText: hint,
    filled: true,
    enabledBorder: OutlineInputBorder(
    borderSide: BorderSide(width: 0.0),
    ),
    contentPadding: const EdgeInsets.only(left: 14.0, bottom: 10.0, top: 10.0),
    );
    }

    class Provider extends InheritedWidget {
    final AuthService auth;

    Provider({Key key, Widget child, this.auth}) : super(key: key, child: child);

    @override
    bool updateShouldNotify(InheritedWidget oldWidget) {
    return true;
    }

    static Provider of(BuildContext context) =>
    context.dependOnInheritedWidgetOfExactType<Provider>();
    }
    有人可以建议实现此感谢的最佳方法。

    最佳答案

    设法制定出解决方案...
    在我的注册屏幕中,我导入了cloud_firestore:

    import 'package:cloud_firestore/cloud_firestore.dart';
    然后,我更改了以下内容,以使用我们创建的uid创建文档并将字段设置为等于传递的值:
     if (validate()) {
    try {
    final auth = Provider
    .of(context)
    .auth;
    if (authFormType == AuthFormType.signIn) {
    String uid = await auth.signInWithEmailAndPassword(_email, _password);
    print("Signed In with ID $uid");
    Navigator.of(context).pushReplacementNamed('/home');
    } else {
    String uid = await auth.createUserWithEmailAndPassword(
    _email,
    _password,);
    userCollection.document(uid).setData({
    'First Name' : _firstName,
    'Last Name' : _lastName,
    'Date of Birth' : _dateOfBirth
    });
    print("Signed up with New ID $uid");
    Navigator.of(context).pushReplacementNamed('/home');
    }
    } catch (e) {
    print(e);
    }
    我必须删除我的验证方法,因为它会引起问题。运行新代码,新用户出现在我的数据库中。

    关于firebase - Flutter和Firebase-在注册时收集其他数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63341590/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com