【问题标题】:How to update password in flutter using Firestore如何使用 Firestore 在 Flutter 中更新密码
【发布时间】:2021-09-10 13:36:55
【问题描述】:

大家好,我是 Flutter 的新手,我正在尝试根据存储在 Firestore 中的用户数据以及用户当前登录系统的情况来更新密码。任何人都知道如何根据登录系统的特定 ID 更新密码并在用户输入重复密码时对其进行验证?请帮忙。

代码

import 'package:flutter/material.dart';
import 'package:monger_app/localization/localization_constants.dart';
import 'package:monger_app/page/setting.dart';
import 'package:monger_app/theme/colors.dart';


class BackupSettings extends StatefulWidget {
  @override
  _BackupSettingsState createState() => _BackupSettingsState();
}

class _BackupSettingsState extends State<BackupSettings> {
  final _newpasswordController = TextEditingController();
  final _repeatpasswordController = TextEditingController();
  var _formKey = GlobalKey<FormState>();
  
  

  bool checkCurrentPasswordValid = true;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(getTranslated(context, 'backup_text'),),
        elevation: 0,
        brightness: Brightness.light,
        backgroundColor: primary,
        leading: IconButton(
          onPressed: (){
            Navigator.pop(context, MaterialPageRoute(builder: (context) => Settings()));
          },
          icon: Icon(Icons.arrow_back_ios,
            size: 20,
            color: Colors.black,),
        ),
      ),
      body: SingleChildScrollView(
        child: Padding(
          padding: EdgeInsets.all(20.0),
          child: Column(
              key: _formKey,
                children: <Widget>[
                  Container(
                    child: TextFormField(
                        validator: (input) {
                          if (input.length < 8)
                            return 'Please Provide Minimum 8 Character';
                        },
                        decoration: InputDecoration(
                          labelText: getTranslated(context, 'new_password'),
                          labelStyle: TextStyle(color: Colors.grey),
                          prefixIcon: Icon(
                            Icons.lock,
                            color: secondary,
                          ),
                          focusedBorder: UnderlineInputBorder(
                            borderSide: BorderSide(color: secondary),
                          ),
                        ),
                        obscureText: true,
                        controller: _newpasswordController
                        ),
                  ),
                  Container(
                    child: TextFormField(
                      decoration: InputDecoration(
                        labelText: getTranslated(context, 'repeat_password'),
                        labelStyle: TextStyle(color: Colors.grey),
                        prefixIcon: Icon(
                          Icons.lock,
                          color: secondary,
                        ),
                        focusedBorder: UnderlineInputBorder(
                          borderSide: BorderSide(color: secondary),
                        ),
                      ),
                      obscureText: true,
                        controller: _repeatpasswordController,
                      validator: (value) {
                        return _newpasswordController.text == value
                            ? null
                            : "Please validate your entered password";
                      },
                    ),
                  ),
                  SizedBox(height: 20),
                  RaisedButton(
                    padding: EdgeInsets.fromLTRB(70, 10, 70, 10),
                    onPressed: (){
                      
                    },

                    child: Text(getTranslated(context, 'save_button'),
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: 20.0,
                            fontWeight: FontWeight.bold)),
                    color: primary,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(20.0),
                    ),
                  )
                ],
              ),
            ),
          ),
          );
  }
}

【问题讨论】:

  • 嗨,我可以知道怎么做吗?
  • 嗨,我可以知道怎么做吗?
  • 您是否在使用 Firebase 身份验证并仍在 Firestore 中存储密码?
  • 是的,我正在使用 firebase 身份验证并存储到 firestore @Dharmaraj

标签: firebase flutter google-cloud-firestore firebase-authentication


【解决方案1】:

使用这个功能,

void _changePassword(String yourPassword) async{
    FirebaseUser user = await FirebaseAuth.instance.currentUser();
    user.updatePassword(yourPassword).then((_){
Firestore.instance.collection("users").document(user.uid).updateData(
         {
          "password" : _newpasswordController.text,
         }).then((_){
          print("Successfully changed password");
         });
    }).catchError((error){
      print("Error " + error.toString());
    });
  }

按下时,

    onPressed: (){
if(!_formKey.currentState.validate()){
return;
}
_formKey.currentState.save();
             _changePassword(_newpasswordController.text);      
        },

【讨论】:

  • 我应该在按下按钮时输入什么?
  • 检查已编辑的答案。让我知道它是否有效。
  • 我检查了 Firestore 中的密码,它没有更改,但在运行中它显示 D/FirebaseAuth(27924): Notifying id token listeners about user ( PyDw717LcSTofi7dB4d3kV9QhIY2 )。 W/System (27924):忽略标头 X-Firebase-Locale,因为它的值为 null。 I/flutter (27924):成功更改密码。
  • 但在运行中显示成功更改密码,但实际上并没有更改密码(在firestore中)
  • 它不会更改 firestore 数据库中的密码。它将在身份验证表中更改。对于 db 中的更改,您必须更新该特定用户数据。检查更新的答案。
猜你喜欢
  • 1970-01-01
  • 2021-01-18
  • 2020-10-25
  • 1970-01-01
  • 2021-02-10
  • 2021-02-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多