0%

Flutter 023 - Domain Error Handle (part7)

前言

Hi, 今天要把偵錯的功能加上去,教學內容只會擷取片段程式碼,建議大家搭配完整程式碼來練習。

完整程式碼

需準備的知識

[Day15] Flutter - 大海撈針不是辦法 ( Dartz )

安裝

1
dartz: ^0.9.2

Domain:Google Failure

我創建了一個錯誤訊息的類別,如果Google登入出錯時會出現GoogleAuthServerFailure,如果你還有FB、Github等的登入你可以往裡面一直新增,FirebaseAuthFailure則是處理登入獲取使用者資料的錯誤訊息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import 'package:equatable/equatable.dart';

abstract class AuthFailure extends Equatable {
final String message;

AuthFailure({required this.message});

@override
List<Object> get props => [message];
}

class GoogleAuthServerFailure extends AuthFailure {
GoogleAuthServerFailure({required String message}) : super(message: "");
}

class FirebaseAuthFailure extends AuthFailure {
FirebaseAuthFailure({required String message}) : super(message: "");
}

修改 Infrastructure Auth

將所有功能都加上AuthFailure,我多新增了一個getUser,這樣一來登入完成後可以直接獲取使用者資訊。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import 'package:dartz/dartz.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:stunning_tribble/domain/auth/auth_failure.dart';

abstract class AuthRepositoryImpl {
/// Used when the google login button is triggered.
Future<Either<AuthFailure, Unit>> signInWithGoogle();

/// Check if you are logged in.
Future<Either<AuthFailure, Unit>> isSignedIn();

/// Log out of device.
Future<Either<AuthFailure, Unit>> signOut();

/// Get current user info.
Future<Either<AuthFailure, User>> getUser();
}

class AuthRepository implements AuthRepositoryImpl {
final FirebaseAuth _firebaseAuth;
final GoogleSignIn _googleSignIn;

AuthRepository()
: _firebaseAuth = FirebaseAuth.instance,
_googleSignIn = GoogleSignIn();

@override
Future<Either<AuthFailure, Unit>> signInWithGoogle() async {
try {
final GoogleSignInAccount? googleUser = await _googleSignIn.signIn();
if (googleUser == null) {
return left(GoogleAuthServerFailure(message: "Cache User Failure"));
}
final GoogleSignInAuthentication googleAuth =
await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
await _firebaseAuth.signInWithCredential(credential);

return right(unit);
} catch (e) {
return left(GoogleAuthServerFailure(message: "$e"));
}
}

@override
Future<Either<AuthFailure, Unit>> isSignedIn() async {
try {
final User? currentUser = _firebaseAuth.currentUser;
if (currentUser != null) {
return right(unit);
} else {
return left(FirebaseAuthFailure(message: "Not Logged In"));
}
} catch (e) {
return left(FirebaseAuthFailure(message: "$e"));
}
}

Future<Either<AuthFailure, User>> getUser() async {
try {
return right(_firebaseAuth.currentUser);
} catch (_) {
return left(FirebaseAuthFailure(message: "Get Current User Failure"));
}
}

@override
Future<Either<AuthFailure, Unit>> signOut() async {
try {
Future.wait([
_firebaseAuth.signOut(),
_googleSignIn.signOut(),
]);
return right(unit);
} catch (e) {
return left(FirebaseAuthFailure(message: "$e"));
}
}
}