実現したいこと
Flutter Firebase.initializeApp()の呼び出し時のエラー解消し、新たにFirebaseのパッケージを使用したい。
Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
)
前提
以下のサイトに載っているチュートリアルを進めています。
現在の現象が起きるまでは特に問題なく稼働していました。
サイトではWebで起動する様になっていますが、今回はIosで起動する様にしています。
https://www.flutter-study.dev/firebase-app/authentication
元々はFirebaseを利用した以下のログイン機能の箇所で落ちていた。
try {
final FirebaseAuth auth = FirebaseAuth.instance;
・・・・
}
この箇所を解消するためにFirebase.initializeApp()にたどり着いた。
発生している問題・エラーメッセージ
以下の箇所で例外が発生しています。 final FirebaseAuth auth = FirebaseAuth.instance; 以下、エラー内容 ([core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp())
該当のソースコード
main.dart
1import 'package:flutter/material.dart'; 2import 'package:firebase_core/firebase_core.dart'; 3import 'package:firebase_auth/firebase_auth.dart'; 4// import 'firebase_options.dart'; 5 6void main() async { 7 //初期化処理を追加 8 WidgetsFlutterBinding.ensureInitialized(); 9 await Firebase.initializeApp( 10 // options: DefaultFirebaseOptions.currentPlatform, 11 ); 12 13 runApp(const ChatApp()); 14} 15 16class ChatApp extends StatelessWidget { 17 const ChatApp({super.key}); 18 19 @override 20 Widget build(BuildContext context) { 21 return MaterialApp( 22 title: 'ChatApp', 23 theme: ThemeData( 24 primarySwatch: Colors.red, 25 ), 26 home: LoginPage(), 27 ); 28 } 29} 30 31//ログイン画面用Widget 32class LoginPage extends StatefulWidget { 33 @override 34 _LoginPageState createState() => _LoginPageState(); 35} 36 37class _LoginPageState extends State<LoginPage> { 38 //メッセージ表示用 39 String infoText = ''; 40 //入力したメールアドレス・パスワード 41 String email = ''; 42 String password = ''; 43 44 @override 45 Widget build(BuildContext context) { 46 return Scaffold( 47 body: Center( 48 child: Container( 49 padding: EdgeInsets.all(24), 50 child: Column( 51 mainAxisAlignment: MainAxisAlignment.center, 52 children: <Widget>[ 53 // メールアドレス入力 54 TextFormField( 55 decoration: InputDecoration(labelText: 'メールアドレス'), 56 onChanged: (String value) { 57 setState(() { 58 email = value; 59 }); 60 }, 61 ), 62 // パスワード入力 63 TextFormField( 64 decoration: InputDecoration(labelText: 'パスワード'), 65 obscureText: true, 66 onChanged: (String value) { 67 setState(() { 68 password = value; 69 }); 70 }, 71 ), 72 Container( 73 padding: EdgeInsets.all(8), 74 //メッセージ表示 75 child: Text(infoText), 76 ), 77 Container( 78 width: double.infinity, 79 //ユーザー登録ボタン 80 child: ElevatedButton( 81 child: Text('ユーザー登録'), 82 onPressed: () async { 83 try { 84 //メールアドレスでユーザー登録 85 final FirebaseAuth auth = FirebaseAuth.instance; 86 await auth.createUserWithEmailAndPassword( 87 email: email, 88 password: password, 89 ); 90 //ユーザー登録に成功した場合 91 //チャット画面に遷移+ログイン画面を破棄 92 await Navigator.of(context).pushReplacement( 93 MaterialPageRoute(builder: (context) { 94 return ChatPage(); 95 }), 96 ); 97 } on FirebaseAuthException catch (e) { 98 //ユーザー登録に失敗した場合 99 setState(() { 100 /// パスワードが弱い場合 101 if (e.code == 'weak-password') { 102 print('パスワードが弱いです'); 103 104 /// メールアドレスが既に使用中の場合 105 } else if (e.code == 'email-already-in-use') { 106 print('すでに使用されているメールアドレスです'); 107 } 108 109 /// その他エラー 110 else { 111 print('アカウント作成エラー'); 112 } 113 infoText = "登録に失敗しました:${e.toString()}"; 114 }); 115 } 116 }, 117 ), 118 ) 119 ], 120 ), 121 ), 122 ), 123 ); 124 } 125} 126 127//チャット画面用Widget 128class ChatPage extends StatelessWidget { 129 @override 130 Widget build(BuildContext context) { 131 return Scaffold( 132 appBar: AppBar( 133 title: Text('チャット'), 134 actions: <Widget>[ 135 IconButton( 136 icon: Icon(Icons.close), 137 onPressed: () async { 138 //ログイン画面に遷移+チャット画面を破棄 139 await Navigator.of(context).pushReplacement( 140 MaterialPageRoute(builder: (context) { 141 return LoginPage(); 142 }), 143 ); 144 }, 145 ), 146 ], 147 ), 148 floatingActionButton: FloatingActionButton( 149 child: Icon(Icons.add), 150 onPressed: () async { 151 //投稿画面に遷移 152 await Navigator.of(context).push( 153 MaterialPageRoute(builder: (context) { 154 return AddPostPage(); 155 }), 156 ); 157 }, 158 ), 159 ); 160 } 161} 162 163// 投稿画面用Widget 164class AddPostPage extends StatelessWidget { 165 @override 166 Widget build(BuildContext context) { 167 return Scaffold( 168 appBar: AppBar( 169 title: Text('チャット投稿'), 170 ), 171 body: Center( 172 child: ElevatedButton( 173 child: Text('戻る'), 174 onPressed: () { 175 //一つ前の画面に戻る 176 Navigator.of(context).pop(); 177 }, 178 ), 179 ), 180 ); 181 } 182} 183
pubspec.yaml
1name: mychatapp 2description: A new Flutter project. 3 4publish_to: "none" # Remove this line if you wish to publish to pub.dev 5 6https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html 7 8version: 1.0.0+1 9 10environment: 11 sdk: ">=2.19.0-146.2.beta <3.0.0" 12 13dependencies: 14 flutter: 15 sdk: flutter 16 17 cupertino_icons: ^1.0.2 18 firebase_core: ^2.10.0 19 cloud_firestore: ^4.5.2 20 firebase_auth: ^4.4.2 21 22dev_dependencies: 23 flutter_test: 24 sdk: flutter 25 26 flutter_lints: ^2.0.0 27 28flutter: 29 uses-material-design: true 30
試したこと
runAppを実行する前に以下を実行。
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_auth/firebase_auth.dart';
// import 'firebase_options.dart';
void main() async {
//初期化処理を追加
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
// options: DefaultFirebaseOptions.currentPlatform,
);
以下コメントにしている箇所はエラーが解消されなくてコメントにしています。
import 'firebase_options.dart';
options: DefaultFirebaseOptions.currentPlatform,
補足情報(FW/ツールのバージョンなど)
cupertino_icons: ^1.0.2
firebase_core: ^2.10.0
cloud_firestore: ^4.5.2
firebase_auth: ^4.4.2
[✓] Flutter (Channel dev, 3.4.0-17.2.pre, on macOS 12.6.5 21G531 darwin-x64, locale ja-JP)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
[✓] Xcode - develop for iOS and macOS (Xcode 14.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.2)
[✓] Android Studio (version 4.0)
[✓] Android Studio (version 4.0)
[✓] IntelliJ IDEA Community Edition (version 2020.1.2)
[✓] IntelliJ IDEA Community Edition (version 2020.1.3)
[✓] IntelliJ IDEA Ultimate Edition (version EAP IC-202.5958.24)
[✓] IntelliJ IDEA Ultimate Edition (version EAP IC-202.6397.20)
[✓] VS Code (version 1.77.3)
[✓] Connected device (3 available)
[✓] HTTP Host Availability
0 コメント