質問編集履歴

1

情報が不足していました。

2020/04/08 08:17

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- FlutterでFirebase Authenticationを使ってID、PWによるログイン処理を実装しています。
1
+ FlutterでFirebaseを使ってID、PWによるログイン処理を実装しています。
2
2
 
3
3
  そこで、ログインはうまくいくのですが、ログアウトしてログインページに戻ったあとに再ログインできないという事象に悩まされています。
4
4
 
@@ -6,11 +6,11 @@
6
6
 
7
7
 
8
8
 
9
- 基本的には、[こちら](Flutter Firebase Authentication Tutorial — AndroidVille | Ayusch https://ayusch.com/flutter-firebase-authentication-tutorial/)のページ則って実装しています。
9
+ 基本的には、[こちら](https://ayusch.com/flutter-firebase-authentication-tutorial/)を参考に実装しています。
10
-
11
-
12
-
10
+
11
+
12
+
13
- 以下、エラーを記します。
13
+ エラーす。
14
14
 
15
15
  ```
16
16
 
@@ -72,23 +72,15 @@
72
72
 
73
73
 
74
74
 
75
- 以下、ソースをす。
75
+ ソースです。★つけた箇所がデバッグた結果、怪しいと感じた箇所です。
76
-
77
-
78
-
76
+
79
- ```main.dart
77
+ ### main.dart
80
-
81
- import 'package:flutter/material.dart';
78
+
82
-
83
- import 'authentication.dart';
84
-
85
- import 'root_page.dart';
79
+ ```
86
-
87
-
88
80
 
89
81
  void main() {
90
82
 
91
- runApp(new MyApp());
83
+ // 省略
92
84
 
93
85
  }
94
86
 
@@ -96,155 +88,559 @@
96
88
 
97
89
  class MyApp extends StatelessWidget {
98
90
 
91
+ Widget build(BuildContext context) {
92
+
93
+ return MaterialApp(
94
+
95
+ // 省略,
96
+
97
+ home: RootPage(auth: Auth()));
98
+
99
+ }
100
+
101
+ }
102
+
103
+ ```
104
+
105
+
106
+
107
+ ### root_page.dart
108
+
109
+ ```
110
+
111
+ class RootPage extends StatefulWidget {
112
+
113
+ RootPage({this.auth});
114
+
115
+
116
+
117
+  //★authインスタンスは重複していないか
118
+
119
+ final BaseAuth auth;
120
+
121
+ State<StatefulWidget> createState() => _RootPageState();
122
+
123
+ }
124
+
125
+
126
+
127
+ class _RootPageState extends State<RootPage> {
128
+
129
+ AuthStatus authStatus = AuthStatus.NOT_DETERMINED;
130
+
131
+ String _userId = "";
132
+
133
+
134
+
135
+ void initState() {
136
+
137
+ super.initState();
138
+
139
+ widget.auth.getCurrentUser().then((user) {
140
+
141
+ setState(() {
142
+
143
+ if (user != null) {
144
+
145
+ _userId = user?.uid;
146
+
147
+ }
148
+
149
+ authStatus =
150
+
151
+ user?.uid == null ? AuthStatus.NOT_LOGGED_IN : AuthStatus.LOGGED_IN;
152
+
153
+ });
154
+
155
+ });
156
+
157
+ }
158
+
159
+
160
+
161
+ void loginCallback() {
162
+
163
+   // 省略
164
+
165
+ }
166
+
167
+
168
+
169
+  //★本処理は適切に呼ばれているか
170
+
171
+ void logoutCallback() {
172
+
173
+ setState(() {
174
+
175
+ authStatus = AuthStatus.NOT_LOGGED_IN;
176
+
177
+ _userId = "";
178
+
179
+ });
180
+
181
+ }
182
+
183
+
184
+
185
+ Widget buildWaitingScreen() {
186
+
187
+ return Scaffold(
188
+
189
+ // 省略
190
+
191
+ );
192
+
193
+ }
194
+
195
+
196
+
197
+ Widget build(BuildContext context) {
198
+
199
+ switch (authStatus) {
200
+
201
+ case AuthStatus.NOT_DETERMINED:
202
+
203
+ return buildWaitingScreen();
204
+
205
+ break;
206
+
207
+ case AuthStatus.NOT_LOGGED_IN:
208
+
209
+ return LoginSignupPage(
210
+
211
+ auth: widget.auth,
212
+
213
+ loginCallback: loginCallback,
214
+
215
+ );
216
+
217
+ break;
218
+
219
+ case AuthStatus.LOGGED_IN:
220
+
221
+ if (_userId.length > 0 && _userId != null) {
222
+
223
+ return HomePage(
224
+
225
+ userId: _userId,
226
+
227
+ auth: widget.auth,
228
+
229
+ logoutCallback: logoutCallback,
230
+
231
+ );
232
+
233
+ } else
234
+
235
+ return buildWaitingScreen();
236
+
237
+ break;
238
+
239
+ default:
240
+
241
+ return buildWaitingScreen();
242
+
243
+ }
244
+
245
+ }
246
+
247
+ }
248
+
249
+ ```
250
+
251
+
252
+
253
+ ### login_signup_page.dart
254
+
255
+ ```
256
+
257
+ class LoginSignupPage extends StatefulWidget {
258
+
259
+ LoginSignupPage({this.auth, this.loginCallback});
260
+
261
+
262
+
263
+ final BaseAuth auth;
264
+
265
+ final VoidCallback loginCallback;
266
+
267
+ State<StatefulWidget> createState() => _LoginSignupPageState();
268
+
269
+ }
270
+
271
+
272
+
273
+ class _LoginSignupPageState extends State<LoginSignupPage> {
274
+
275
+ final _formKey = GlobalKey<FormState>();
276
+
277
+ String _groupId;
278
+
279
+ String _displayName;
280
+
281
+ String _email;
282
+
283
+ String _password;
284
+
285
+ String _errorMessage;
286
+
287
+ bool _isLoginForm;
288
+
289
+ bool _isLoading;
290
+
291
+
292
+
293
+ bool validateAndSave() {
294
+
295
+ final form = _formKey.currentState;
296
+
297
+ if (form.validate()) {
298
+
299
+ form.save();
300
+
301
+ return true;
302
+
303
+ }
304
+
305
+ return false;
306
+
307
+ }
308
+
309
+
310
+
311
+ void validateAndSubmit() async {
312
+
313
+ setState(() {
314
+
315
+ _errorMessage = "";
316
+
317
+ _isLoading = true;
318
+
319
+ });
320
+
321
+ if (validateAndSave()) {
322
+
323
+ String userId = "";
324
+
325
+ try {
326
+
327
+ if (_isLoginForm) {
328
+
329
+      //★このtry文でエラーが発生しているように見える
330
+
331
+ userId = await widget.auth.signIn(_email, _password);
332
+
333
+ } else {
334
+
335
+ userId = await widget.auth.signUp(_email, _password);
336
+
337
+ Firestore.instance.collection('users').document(userId).setData({
338
+
339
+ // 省略
340
+
341
+ });
342
+
343
+ widget.auth.sendEmailVerification();
344
+
345
+ _showVerifyEmailSentDialog();
346
+
347
+ }
348
+
349
+ setState(() {
350
+
351
+ _isLoading = false;
352
+
353
+ });
354
+
355
+
356
+
357
+ if (userId.length > 0 && userId != null && _isLoginForm) {
358
+
359
+ widget.loginCallback();
360
+
361
+ }
362
+
363
+ } catch (e) {
364
+
365
+ //★ここでeがnullになっていることが原因?
366
+
367
+ setState(() {
368
+
369
+ _isLoading = false;
370
+
371
+ _errorMessage = e.message;
372
+
373
+ _formKey.currentState.reset();
374
+
375
+ });
376
+
377
+ }
378
+
379
+ }
380
+
381
+ }
382
+
383
+
384
+
99
385
  @override
100
386
 
387
+ void initState() {
388
+
389
+ _errorMessage = "";
390
+
391
+ _isLoading = false;
392
+
393
+ _isLoginForm = true;
394
+
395
+ super.initState();
396
+
397
+ }
398
+
399
+ void resetForm() {
400
+
401
+ // 省略
402
+
403
+ }
404
+
405
+ void toggleFormMode() {
406
+
407
+ // 省略
408
+
409
+ }
410
+
411
+
412
+
101
413
  Widget build(BuildContext context) {
102
414
 
103
- return new MaterialApp(
415
+ return Scaffold(
104
-
416
+
105
- title: 'アプリ名',
417
+ // 省略
106
-
107
- debugShowCheckedModeBanner: false,
418
+
108
-
109
- theme: ThemeData(
110
-
111
- primarySwatch: Colors.blue,
112
-
113
- ),
419
+ );
114
-
115
- home: RootPage(auth: Auth()));
420
+
116
-
117
- }
421
+ }
422
+
423
+
424
+
118
-
425
+ // 省略
426
+
119
- }
427
+ }
120
-
428
+
121
- ```
429
+ ```
122
-
123
-
124
-
430
+
431
+
432
+
125
- ```root_page.dart
433
+ ### homepage.dart
126
-
127
- import 'package:flutter/material.dart';
434
+
128
-
129
- import 'authentication.dart';
130
-
131
- import 'homepage.dart';
132
-
133
- import 'login_signup_page.dart';
134
-
135
-
136
-
137
- enum AuthStatus {
138
-
139
- NOT_DETERMINED,
140
-
141
- NOT_LOGGED_IN,
142
-
143
- LOGGED_IN,
144
-
145
- }
435
+ ```
146
-
147
-
148
-
436
+
149
- class RootPage extends StatefulWidget {
437
+ class HomePage extends StatelessWidget {
438
+
150
-
439
+ HomePage(
440
+
441
+ {Key key, this.auth, this.onSignedOut, this.userId, this.logoutCallback})
442
+
151
- RootPage({this.auth});
443
+ : super(key: key);
152
444
 
153
445
 
154
446
 
155
447
  final BaseAuth auth;
156
448
 
157
-
449
+ final VoidCallback onSignedOut;
450
+
158
-
451
+ final VoidCallback logoutCallback;
452
+
453
+ final String userId;
454
+
455
+
456
+
457
+ Widget build(BuildContext context) {
458
+
459
+ //★TODO: 認証情報等をすべて引数で下位Widgetに渡している箇所はProviderでリファクタリングする
460
+
461
+ return MaterialApp(
462
+
159
- @override
463
+ home: MyHomePage(
464
+
160
-
465
+ title: 'title',
466
+
467
+ auth: this.auth,
468
+
469
+ onSignedOut: this.onSignedOut,
470
+
471
+ logoutCallback: this.logoutCallback,
472
+
473
+ userId: this.userId),
474
+
475
+ routes: <String, WidgetBuilder>{
476
+
161
- State<StatefulWidget> createState() => _RootPageState();
477
+ '/login_signup': (BuildContext context) => LoginSignupPage(),
478
+
162
-
479
+ });
480
+
163
- }
481
+ }
482
+
164
-
483
+ }
484
+
485
+
486
+
165
-
487
+ class MyHomePage extends StatefulWidget {
488
+
166
-
489
+ MyHomePage(
490
+
491
+ {Key key,
492
+
493
+ this.title,
494
+
495
+ this.auth,
496
+
497
+ this.onSignedOut,
498
+
499
+ this.logoutCallback,
500
+
501
+ this.userId})
502
+
503
+ : super(key: key);
504
+
505
+
506
+
507
+ final String title;
508
+
509
+ final BaseAuth auth;
510
+
511
+ final VoidCallback logoutCallback;
512
+
513
+ final String userId;
514
+
515
+ final VoidCallback onSignedOut;
516
+
517
+
518
+
519
+ _MyHomePageState createState() => _MyHomePageState(
520
+
521
+ auth: this.auth, onSignedOut: this.onSignedOut, userId: this.userId);
522
+
523
+ }
524
+
525
+
526
+
167
- class _RootPageState extends State<RootPage> {
527
+ class _MyHomePageState extends State<MyHomePage> {
168
-
528
+
169
- AuthStatus authStatus = AuthStatus.NOT_DETERMINED;
529
+ FirebaseUser user;
170
-
530
+
171
- String _userId = "";
531
+ final BaseAuth auth;
532
+
172
-
533
+ final logoutCallback;
534
+
173
-
535
+ final VoidCallback onSignedOut;
174
-
536
+
537
+
538
+
175
- @override
539
+ _MyHomePageState(
540
+
541
+ {this.auth, this.onSignedOut, this.logoutCallback, this.userId});
542
+
543
+
176
544
 
177
545
  void initState() {
178
546
 
179
547
  super.initState();
180
548
 
549
+
550
+
181
- widget.auth.getCurrentUser().then((user) {
551
+ initUser() async {
182
-
552
+
183
- setState(() {
553
+ setState(() {});
184
-
185
- if (user != null) {
554
+
186
-
187
- _userId = user?.uid;
188
-
189
- }
555
+ }
556
+
557
+
558
+
190
-
559
+ final String userId;
560
+
561
+ String displayName = '名無し';
562
+
563
+
564
+
565
+ Widget build(BuildContext context) {
566
+
567
+ Firestore.instance
568
+
569
+ .collection('users')
570
+
571
+ .document(userId)
572
+
573
+ .get()
574
+
191
- authStatus =
575
+ .then((onValue) {
192
-
576
+
193
- user?.uid == null ? AuthStatus.NOT_LOGGED_IN : AuthStatus.LOGGED_IN;
577
+ displayName = onValue.data['displayName'].toString();
194
-
195
- });
196
578
 
197
579
  });
198
580
 
199
- }
581
+
200
-
201
-
202
-
203
- void loginCallback() {
204
-
205
- widget.auth.getCurrentUser().then((user) {
206
-
207
- setState(() {
208
-
209
- _userId = user.uid.toString();
210
-
211
- });
212
-
213
- });
214
-
215
- setState(() {
216
-
217
- authStatus = AuthStatus.LOGGED_IN;
218
-
219
- });
220
-
221
- }
222
-
223
-
224
-
225
- void logoutCallback() {
226
-
227
- setState(() {
228
-
229
- authStatus = AuthStatus.NOT_LOGGED_IN;
230
-
231
- _userId = "";
232
-
233
- });
234
-
235
- }
236
-
237
-
238
-
239
- Widget buildWaitingScreen() {
240
582
 
241
583
  return Scaffold(
242
584
 
585
+ appBar: Header(),
586
+
243
- body: Container(
587
+ body: Column(
244
-
588
+
245
- alignment: Alignment.center,
589
+ children: <Widget>[
590
+
246
-
591
+ Expanded(
592
+
593
+ flex: 0,
594
+
595
+ child: Row(
596
+
247
- child: CircularProgressIndicator(),
597
+ children: <Widget>[
598
+
599
+ // 省略
600
+
601
+ Expanded(
602
+
603
+ flex: 1,
604
+
605
+ child: FlatButton(
606
+
607
+ // 省略
608
+
609
+ onPressed: () async {
610
+
611
+ try {
612
+
613
+ //★logoutCallbackが呼べていない?
614
+
615
+ await widget.auth.signOut();
616
+
617
+ widget.logoutCallback;
618
+
619
+ } catch (e) {
620
+
621
+ }
622
+
623
+ //★ここも怪しい
624
+
625
+ Navigator.of(context)
626
+
627
+ .pushReplacementNamed('/login_signup');
628
+
629
+ },
630
+
631
+ ),
632
+
633
+ )
634
+
635
+ ],
636
+
637
+ ),
638
+
639
+ ),
640
+
641
+ // 省略
642
+
643
+ ],
248
644
 
249
645
  ),
250
646
 
@@ -252,60 +648,6 @@
252
648
 
253
649
  }
254
650
 
255
-
256
-
257
- @override
258
-
259
- Widget build(BuildContext context) {
260
-
261
- switch (authStatus) {
262
-
263
- case AuthStatus.NOT_DETERMINED:
264
-
265
- return buildWaitingScreen();
266
-
267
- break;
268
-
269
- case AuthStatus.NOT_LOGGED_IN:
270
-
271
- return LoginSignupPage(
272
-
273
- auth: widget.auth,
274
-
275
- loginCallback: loginCallback,
276
-
277
- );
278
-
279
- break;
280
-
281
- case AuthStatus.LOGGED_IN:
282
-
283
- if (_userId.length > 0 && _userId != null) {
284
-
285
- return HomePage(
286
-
287
- userId: _userId,
288
-
289
- auth: widget.auth,
290
-
291
- logoutCallback: logoutCallback,
292
-
293
- );
294
-
295
- } else
296
-
297
- return buildWaitingScreen();
298
-
299
- break;
300
-
301
- default:
302
-
303
- return buildWaitingScreen();
304
-
305
- }
651
+ }
306
-
307
- }
652
+
308
-
309
- }
310
-
311
- ```
653
+ ```