[flutter] #3 firebase - Logout

giyeon·2021년 5월 4일
0

flutter-firebase

목록 보기
3/10
post-thumbnail

#이 포스팅은 플린이의 입장에서 쓰여진 글입니다. 코드 지적은 언제나 환영입니다. 🙆🏻‍♂️

이 프로젝트는 'Youtube The net ninja'의 flutter firebase tutorial 강의를 참고했습니다.


Logout

firebase를 이용한 logout구현은 비교적 간단해요.
signout() method를 사용하기만 하면 돼요!

AuthService class

class 내부에 signOut() method를 만들어볼게요.
혹시모를 error 처리를 위해 try-catch문으로 감싸줘요.

 Future signOut() async {
    try {
      print('sign out complete');
      return await _auth.signOut();
    } catch (e) {
      print('sign out failed');
      print(e.toString());
      return null;
    }
  }

만든 method를 HomeScreen의 Logout Button을 누르면 트리거가 되도록 해요.
AuthService 인스턴스화 하고,
appbar에서 아이콘 버튼을 하나 만들어줘요.
onPressed에 넣어주면 끝!

HomeScreen.dart

final AuthService _auth = AuthService();

Scaffold(
      backgroundColor: Colors.brown[50],
      appBar: AppBar(
        title: Text('Home'),
        backgroundColor: Colors.brown[400],
        elevation: 0,
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.clear),
            onPressed: () async {
              await _auth.signOut();
            },
          ),
        ],
      ),
      body: Container(
        child: Text('home'),
      ),
    )
profile
Web , App developer wannabe 🧑🏻‍💻

0개의 댓글