0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

FlutterでFirebase Authを利用したログイン処理

0
Last updated at Posted at 2020-03-14

メアド、パスワードを画面から入力

メアドとパスワードの入力欄と、ログインボタンを配置します。

入力時にstateを更新しています。

login.dart
class LoginPage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Consumer <LoginStore>(
        builder: (context, loginStore, _) {
          return Scaffold(
              body: Container(margin: EdgeInsets.symmetric(
                  vertical: 80.0, horizontal: 30.0),
                  child: Column(children: <Widget>[
                    TextFormField(
                      decoration: InputDecoration(
                        hintText: "メアド",
                      ),
                      onChanged: (text) => loginStore.changeEmail(text),
                    ),
                    TextFormField(
                      decoration: InputDecoration(
                        hintText: "パスワード",
                      ),
                      onChanged: (text) =>
                          loginStore.changePassword(text),
                    ),
                    RaisedButton(
                        child: Text("Login")
                        ,
                        onPressed: () {
                          loginStore.tapLoginButton();
                        }),
                  ],)
              ));
        });
  }
}

認証処理

signInWithEmailAndPassword で認証を行います。

login.dart
class LoginStore with ChangeNotifier {
  var email = "";
  var password = "";

  void changeEmail(String text) {
    email = text;
  }

  void changePassword(String text) {
    password = text;
  }

  void tapLoginButton() async {
    AuthResult result = await _signIn(email, password);
  }

  Future<AuthResult> _signIn(String email, String password) async {
    try {
      final AuthResult result = await FirebaseAuth.instance
          .signInWithEmailAndPassword(
          email: email, password: password);
      return result;
    } catch (e) {
      return null;
    }
  }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?