質問編集履歴

1

ソースを追加

2022/04/12 11:56

投稿

murapon
murapon

スコア22

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- **発生している問題**
1
+ ## 発生している問題
2
2
  FlutterでFirebase Authenticationを用いてTwitterログインを実装しています。
3
3
  「[FlutterでFirebaseのTwitterログインを実装する](https://qiita.com/0maru/items/a46f5e5b1a9644bb58af)」
4
4
  を参考に実装し、Twitterのログイン画面が表示されてるところまでは確認できたのですが、認証後にコールバックでアプリに戻った際に、アプリが再起動し、最初からプログラムが起動されてしまうので、
@@ -21,6 +21,151 @@
21
21
  ```
22
22
  以降の処理が動きません。
23
23
 
24
+ ## ソース
25
+ **login.dart**
26
+ ```dart
27
+ import 'package:flutter/material.dart';
28
+ import 'package:firebase_auth/firebase_auth.dart';
29
+ import 'package:google_sign_in/google_sign_in.dart';
30
+ import 'package:self_scoring_app/views/pages/top_page.dart';
31
+
32
+ import 'package:twitter_login/twitter_login.dart';
33
+
34
+ import '../../cache/CacheUpdateStatus.dart';
35
+ import '../../utility/Config.dart';
36
+ import '../../utility/Messages.dart';
37
+ import '../widgets/PageStyle.dart';
38
+
39
+ class LoginPage extends StatefulWidget {
40
+ const LoginPage({
41
+ Key key,
42
+ }) : super(key: key);
43
+
44
+ @override
45
+ _LoginPageState createState() => _LoginPageState();
46
+ }
47
+
48
+ class _LoginPageState extends State<LoginPage> {
49
+ final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
50
+ bool isUser = false;
51
+
52
+ @override
53
+ void initState() {
54
+ super.initState();
55
+ _load();
56
+ }
57
+
58
+ @override
59
+ Widget build(BuildContext context) {
60
+ bool _return = false;
61
+ if (this.isUser) {
62
+ _return = true;
63
+ }
64
+ return Scaffold(
65
+ appBar: AppBar(
66
+ backgroundColor: PageStyle.headerColor,
67
+ automaticallyImplyLeading: _return,
68
+ title: Padding(
69
+ padding: EdgeInsets.only(left: 5),
70
+ child: Text(Messages.pageLoginTitle, style: PageStyle.defaultTextStyle),
71
+ ),
72
+ titleSpacing: 0,
73
+ ),
74
+ body: Container(
75
+ child: Column(
76
+ children: <Widget>[
77
+ Form(
78
+ key: _formKey,
79
+ child: Center(
80
+ child: Padding(
81
+ padding: const EdgeInsets.symmetric(horizontal: 24),
82
+ child: Column(
83
+ mainAxisAlignment: MainAxisAlignment.center,
84
+ mainAxisSize: MainAxisSize.min,
85
+ children: _getButtons(),
86
+ ),
87
+ ),
88
+ ),
89
+ )
90
+ ],
91
+ ),
92
+ ),
93
+ );
94
+ }
95
+
96
+ void _load() {
97
+ final FirebaseAuth firebaseAuth = FirebaseAuth.instance;
98
+ if (firebaseAuth.currentUser != null) {
99
+ this.isUser = true;
100
+ }
101
+ }
102
+
103
+ List<Widget> _getButtons() {
104
+ List<Widget> buttons = [];
105
+
106
+ buttons.add(
107
+ SizedBox(
108
+ child: ElevatedButton(
109
+ style: ElevatedButton.styleFrom(
110
+ primary: Colors.blue,
111
+ onPrimary: Colors.white,
112
+ ),
113
+ onPressed: () => _onSignInWithTwitter(),
114
+ child: Text(Messages.buttonTwitterLogin, style: PageStyle.buttonTextStyle),
115
+ ),
116
+ ),
117
+ );
118
+ return buttons;
119
+ }
120
+
121
+ Future _onSignInWithTwitter() async {
122
+ print(Config.twitterConsumerKey);
123
+ print(Config.twitterSecretKey);
124
+ try {
125
+ final twitterLogin = TwitterLogin(
126
+ apiKey: Config.twitterConsumerKey,
127
+ apiSecretKey: Config.twitterSecretKey,
128
+ redirectURI: 'test-log://',
129
+ );
130
+ print("test1--------1");
131
+ final authResult = await twitterLogin.loginV2();
132
+ print("test1--------2");
133
+ print(authResult.status);
134
+ switch (authResult.status) {
135
+ case TwitterLoginStatus.loggedIn:
136
+ // success
137
+ print('====login success====');
138
+ final credential = TwitterAuthProvider.credential(
139
+ accessToken: authResult.authToken,
140
+ secret: authResult.authTokenSecret,
141
+ );
142
+ await FirebaseAuth.instance.signInWithCredential(credential);
143
+ break;
144
+ case TwitterLoginStatus.cancelledByUser:
145
+ // cancel
146
+ print('====login cancel====');
147
+ break;
148
+ case TwitterLoginStatus.error:
149
+ // error
150
+ print('====login error====');
151
+ break;
152
+ }
153
+ } catch (e) {
154
+ await showDialog(
155
+ context: context,
156
+ builder: (context) {
157
+ return AlertDialog(
158
+ title: Text(Messages.errorCommon, style: PageStyle.defaultTextStyle),
159
+ content: Text(e.toString(), style: PageStyle.defaultTextStyle),
160
+ );
161
+ },
162
+ );
163
+ }
164
+ }
165
+ }
166
+
167
+ ```
168
+
24
169
  **AndroidManifest.xml**
25
170
  ```xml
26
171
  <application
@@ -38,7 +183,7 @@
38
183
  </application>
39
184
  ```
40
185
 
41
- **試したこと**
186
+ ## 試したこと
42
187
  - AndroidManifest.xmlの、intent-filterの値を変えてみました。
43
188
  - 「flutter_twitter」などの他のpackageが使えないか試したのですが、「The plugin `flutter_twitter` uses a deprecated version of the Android embedding.」というエラーが出て、インストール自体できませんでした。
44
189