Firebase 无错误重定向-displayName

Firebase 无错误重定向-displayName,firebase,flutter,dart,firebase-authentication,Firebase,Flutter,Dart,Firebase Authentication,在使用firebase身份验证时,我遇到了一个奇怪的错误,没有任何注释 这一次,在我按下设置名称按钮后,应用程序停止。在VScode中,我立即被重定向到此页面: 正如我所说,调试控制台中没有错误,没有注释。没有期待看到 我想设置displayName可能有问题,但不清楚是什么 这是该类的完整代码: class Verified extends StatefulWidget { @override _VerifiedState createState() => _VerifiedS

在使用firebase身份验证时,我遇到了一个奇怪的错误,没有任何注释

这一次,在我按下设置名称按钮后,应用程序停止。在VScode中,我立即被重定向到此页面:

正如我所说,调试控制台中没有错误,没有注释。没有期待看到

我想设置displayName可能有问题,但不清楚是什么

这是该类的完整代码:

class Verified extends StatefulWidget {
  @override
  _VerifiedState createState() => _VerifiedState();
}

class _VerifiedState extends State<Verified> {
  final formKey = GlobalKey<FormState>();
  final nameController = TextEditingController();
  final _auth = FirebaseAuth.instance;

  validate(displayName) {
    if (formKey.currentState.validate()) {
      setName(displayName);
    }
  }

  setName(displayName) async {
    try {
      await _auth.currentUser.updateProfile(displayName: displayName);
    } catch (e) {
      log(e.code.toString());
    }
    log(_auth.currentUser.displayName.toString());
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      child: Padding(
        padding: const EdgeInsets.all(100.0),
        child: Column(
          children: [
            Text('choose your username'),
            Form(
              key: formKey,
              child: TextFormField(
                controller: nameController,
                decoration: InputDecoration(hintText: 'name'),
              ),
            ),
            RaisedButton(
                child: Text('set name'),
                onPressed: () => validate(nameController))
          ],
        ),
      ),
    );
  }
}
类验证扩展了StatefulWidget{
@凌驾
_VerifiedState createState()=>\u VerifiedState();
}
类_验证状态扩展状态{
final formKey=GlobalKey();
最终名称控制器=TextEditingController();
final _auth=FirebaseAuth.instance;
验证(显示名称){
if(formKey.currentState.validate()){
设置名称(显示名称);
}
}
setName(displayName)异步{
试一试{
wait_auth.currentUser.updateProfile(displayName:displayName);
}捕获(e){
日志(例如,code.toString());
}
日志(_auth.currentUser.displayName.toString());
}
@凌驾
小部件构建(构建上下文){
退货(
孩子:填充(
填充:常数边集全部(100.0),
子:列(
儿童:[
Text('选择您的用户名'),
形式(
key:formKey,
子项:TextFormField(
控制器:名称控制器,
装饰:输入装饰(hintText:“name”),
),
),
升起的按钮(
子项:文本('set name'),
按下:()=>验证(名称控制器))
],
),
),
);
}
}

提前感谢您

解决方案

当我使用
\u auth.currentUser
从函数操作中删除所有操作时,我还将此函数移动到用户登录/注册的位置,并且它也可以工作。 因此,我认为错误是因为firebase没有看到任何用户,解决方案是在这些操作之后将
.currentUser
与注册/登录或保存用户在同一类/函数中使用