【问题标题】:Flutter: Is there a better way to handle calling setState() for each condition in an if/else statement?Flutter:有没有更好的方法来处理 if/else 语句中每个条件的调用 setState()?
【发布时间】:2023-03-27 22:38:01
【问题描述】:

我有我认为未优化的代码。基本上,如果发生 FirebaseAuthException,我想更改 errorString 的值。我是否使用 ChangeNotifier 代替?

Future<void> _signInWithEmailAndPassword(BuildContext context,AsyncSnapshot<String> userSnapshot,AsyncSnapshot<String> passwordSnapshot) async {
    try {
      final auth = Provider.of<AuthService>(context, listen: false);
      await auth.signInWithEmailAndPassword(
          userSnapshot.data.trim(), passwordSnapshot.data.trim());
          setState(() => errorString = 'Success');
    } on FirebaseAuthException catch (e) {
      // I don't want to call setState() in each condition
      if (e.code == 'invalid-credential') {
        setState(() {
          errorString = "Email address appears to be malformed/expired";
        });
      } else if (e.code == 'wrong-password') {
        setState(() {
          errorString = "Password associated with this email is wrong";
        });
      } else if (e.code == 'user-not-found') {
        setState(() {
          errorString = "Email has not been registered, please sign up :)";
        });
      } else if (e.code == 'user-disabled') {
        setState(() {
          errorString = "User with this email has been disabled :(";
        });
      } else if (e.code == 'too-many-requests') {
        setState(() {
          errorString = "Too many requests, please try again later.";
        });
      } else if (e.code == 'operation-not-allowed') {
        setState(() {
          errorString = "Signing in with email and password is not enabled";
        });
      } else if (e.code == 'account-exists-with-different-credential') {
        setState(() {
          errorString =
              "Email has already been registered. Reset your password.";
        });
      }
    } 
  }

【问题讨论】:

    标签: flutter setstate state-management flutter-change-notifier


    【解决方案1】:

    我会做这样的事情:

    const errMap = {
        'invalid-credential': "Email address appears to be malformed/expired",
        'wrong-password': "Password associated with this email is wrong",
        'user-not-found': "Email has not been registered, please sign up :)",
        'user-disabled': "User with this email has been disabled :(",
        'too-many-requests': "Too many requests, please try again later.",
        'operation-not-allowed': "Signing in with email and password is not enabled",
        'account-exists-with-different-credential': "Email has already been registered. Reset your password."
    };
    
    setState(() {
        errorString = errMap[e.code];
    });
    

    【讨论】:

      【解决方案2】:

      只需将setState 放在所有赞的末尾

       Future<void> _signInWithEmailAndPassword(BuildContext context,AsyncSnapshot<String> userSnapshot,AsyncSnapshot<String> passwordSnapshot) async {
          try {
            final auth = Provider.of<AuthService>(context, listen: false);
            await auth.signInWithEmailAndPassword(
                userSnapshot.data.trim(), passwordSnapshot.data.trim());
            setState(() => errorString = 'Success');
          } on FirebaseAuthException catch (e) {
            // I don't want to call setState() in each condition
            if (e.code == 'invalid-credential') {
             
                errorString = "Email address appears to be malformed/expired";
             
            } else if (e.code == 'wrong-password') {
            
                errorString = "Password associated with this email is wrong";
              
            } else if (e.code == 'user-not-found') {
              
                errorString = "Email has not been registered, please sign up :)";
             
            } else if (e.code == 'user-disabled') {
              
                errorString = "User with this email has been disabled :(";
              
            } else if (e.code == 'too-many-requests') {
               
                errorString = "Too many requests, please try again later.";
             
            } else if (e.code == 'operation-not-allowed') {
              
                errorString = "Signing in with email and password is not enabled";
              
            } else if (e.code == 'account-exists-with-different-credential') {
           
                errorString =
                "Email has already been registered. Reset your password.";
              
            }
            setState(() {})
          }
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-01
        • 1970-01-01
        相关资源
        最近更新 更多