# Flutter + Firebase環境建置 問題: 跟著忍者教學系列做到第5集 [Flutter & Firebase App Tutorial #5 - Anonymous Sign In](https://youtu.be/LBJoY4VjECo) 執行出現error firebase(default) doesn't exist google[(Stack Overflow)](https://stackoverflow.com/questions/70320263/the-term-flutterfire-is-not-recognized-as-the-name-of-a-cmdlet-function-scri)了一番之後 找到一篇官方寫的,使用firebase前的環境建置文章 https://firebase.google.com/docs/flutter/setup?hl=zh-tw&platform=android 還有[官方示範影片](https://www.youtube.com/watch?v=FkFvQ0SaT1I&ab_channel=Firebase) 是的,一切看起來那麼的容易和簡單 我卻怎麼用都無法執行flutterfire configure 步驟+克服重重困難的過程如下 ## 1. 安裝Firebase CLI 有2種方法 * ### 獨立的二進製文件(standalone binary) 一開始我下載這個但無法執行flutterfire configure - ### 使用npm 後來改成這個方法,先去下載node.js > 如果有下載 獨立的二進製文件(standalone binary)的話 > 先把那個檔案刪掉,不然兩個方法會衝突 你會得到一個node.js命令提示字元  執行下面的指令安裝firebase-tools ``` npm install -g firebase-tools ``` 執行這個啟動flutterfire_cli ``` dart pub global activate flutterfire_cli ``` 如果不能執行可能要改系統變數   在User變數中的Path 新增 flutter/bin 資料夾的路徑  記得改完系統環境變數之後需要重開機! ## 2. 在命令提示字元執行 firebase login 登入和firebase帳號相同的google帳戶 ## 3. 在命令提示字元執行 firebase projects:list 查看自己的firebase專案列表 (非必要步驟) ## 4. 在命令提示字元 你的程式資料夾 執行 flutterfire configure 我這裡一直執行錯誤 因為我是在vscode裡面的terminal執行的 就是一直出現 ```flutter= PS D:\work\flutter_vscode\brew_crew> flutterfire configure i Found 0 Firebase projects. COMMAND: firebase --version ERROR: The FlutterFire CLI currently requires the official Firebase CLI to also be installed, see https://firebase.google.com/docs/cli#install_the_firebase_cli for how to install it. ``` 叫我安裝Firebase CLI,但我明明已經裝了= = ### ==結果!最後我在node.js的在命令提示字元執行flutterfire configure== 就成功了! 後面也沒什麼問題 ## 5. 改程式碼 1. 在pubspec.yaml 中添加 ```flutter= dependencies: firebase_auth: ^4.6.2 cloud_firestore: ^4.8.0 firebase_core: ^2.13.1 ``` 2. 在lib/main.dart檔案import這兩個咚咚 如果成功執行 flutterfire configure lib資料夾應該會生成firebase_options.dart 這個檔案 ```flutter= import 'package:firebase_core/firebase_core.dart'; import 'firebase_options.dart'; ``` 3. 在lib/main.dart檔案加入初始化程式 > 就是這兩行 > WidgetsFlutterBinding.ensureInitialized(); > await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform); 完整的main檔 ```flutter= void main() async { // 記得awit要配上async WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform); runApp(const MyApp()); //MyApp = 你的APP名稱 } ``` 恭喜,可以執行程式了!
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up