質問編集履歴
1
コードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -25,3 +25,153 @@
|
|
25
25
|
|
26
26
|
|
27
27
|
他にも投稿を紐付けていきます。
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
追記
|
34
|
+
|
35
|
+
このコードで行う場合はどのようにuidを取得していけばいいでしょうか?
|
36
|
+
|
37
|
+
```ここに言語を入力
|
38
|
+
|
39
|
+
class MyHomePage extends StatefulWidget {
|
40
|
+
|
41
|
+
MyHomePage({Key key, this.title}) : super(key: key);
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
final String title;
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
@override
|
50
|
+
|
51
|
+
_MyHomePageState createState() => _MyHomePageState();
|
52
|
+
|
53
|
+
}
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
class _MyHomePageState extends State<MyHomePage> {
|
58
|
+
|
59
|
+
final GoogleSignIn _googleSignIn = GoogleSignIn();
|
60
|
+
|
61
|
+
final FirebaseAuth _auth = FirebaseAuth.instance;
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
Future<FirebaseUser> _handleSignIn() async {
|
66
|
+
|
67
|
+
GoogleSignInAccount googleCurrentUser = _googleSignIn.currentUser;
|
68
|
+
|
69
|
+
try {
|
70
|
+
|
71
|
+
if (googleCurrentUser == null)
|
72
|
+
|
73
|
+
googleCurrentUser = await _googleSignIn.signInSilently();
|
74
|
+
|
75
|
+
if (googleCurrentUser == null)
|
76
|
+
|
77
|
+
googleCurrentUser = await _googleSignIn.signIn();
|
78
|
+
|
79
|
+
if (googleCurrentUser == null) return null;
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
GoogleSignInAuthentication googleAuth =
|
84
|
+
|
85
|
+
await googleCurrentUser.authentication;
|
86
|
+
|
87
|
+
final AuthCredential credential = GoogleAuthProvider.getCredential(
|
88
|
+
|
89
|
+
accessToken: googleAuth.accessToken,
|
90
|
+
|
91
|
+
idToken: googleAuth.idToken,
|
92
|
+
|
93
|
+
);
|
94
|
+
|
95
|
+
final FirebaseUser user =
|
96
|
+
|
97
|
+
(await _auth.signInWithCredential(credential)).user;
|
98
|
+
|
99
|
+
print("signed in " + user.displayName);
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
return user;
|
104
|
+
|
105
|
+
} catch (e) {
|
106
|
+
|
107
|
+
print(e);
|
108
|
+
|
109
|
+
return null;
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
void transitionNextPage(FirebaseUser user) {
|
118
|
+
|
119
|
+
if (user == null) return;
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
Navigator.push(context,
|
124
|
+
|
125
|
+
MaterialPageRoute(builder: (context) => NextPage(userData: user)));
|
126
|
+
|
127
|
+
}
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
@override
|
132
|
+
|
133
|
+
Widget build(BuildContext context) {
|
134
|
+
|
135
|
+
return Scaffold(
|
136
|
+
|
137
|
+
appBar: AppBar(
|
138
|
+
|
139
|
+
title: Text(widget.title),
|
140
|
+
|
141
|
+
),
|
142
|
+
|
143
|
+
body: Center(
|
144
|
+
|
145
|
+
child: Column(
|
146
|
+
|
147
|
+
mainAxisAlignment: MainAxisAlignment.center,
|
148
|
+
|
149
|
+
children: <Widget>[
|
150
|
+
|
151
|
+
RaisedButton(
|
152
|
+
|
153
|
+
child: Text('Sign in Google'),
|
154
|
+
|
155
|
+
onPressed: () {
|
156
|
+
|
157
|
+
_handleSignIn()
|
158
|
+
|
159
|
+
.then((FirebaseUser user) => transitionNextPage(user))
|
160
|
+
|
161
|
+
.catchError((e) => print(e));
|
162
|
+
|
163
|
+
},
|
164
|
+
|
165
|
+
),
|
166
|
+
|
167
|
+
]),
|
168
|
+
|
169
|
+
),
|
170
|
+
|
171
|
+
);
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
```
|