Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
291 views
in Technique[技术] by (71.8m points)

firebase - Redirecting without any error - displayName

While working with firebase authentication weird error without any notes happen to me.

This time application stops after I press set name button. Instantly in VScode I am redirected to this page:

enter image description here

As I said there is no error in debug console, no notes. No expections to see.

I guess there is something wrong with setting displayName but not clearly what.

This is full code of the class:

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))
          ],
        ),
      ),
    );
  }
}

Thank you in advance

question from:https://stackoverflow.com/questions/65940245/redirecting-without-any-error-displayname

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

SOLUTION

When I remove from function actions with _auth.currentUser everything works, I also moved this function to the place where the user was logged in/registered and it also worked. So as I think the error was because firebase saw no user and the solution is to use .currentUser in the same class/function as registering/logging in or saving user after those actions.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...