LoginSignup
7
7

More than 5 years have passed since last update.

FlutterFireを利用したFirestoreへの書込・読込をする簡易アプリを作成した過程の躓きと、その対応

Last updated at Posted at 2018-12-19

はじめに

@swchrmと申します。
本記事はFirebase Advent Calendar2018 19日目の記事です。

何が書いてあるのか

FlutterでFirebaseを扱うことのできるプラグインの集まりFlutterFireを利用して簡易投票アプリを作成した際に、Flutterはおろかクロスプラットフォーム開発の経験がなかった身には躓きポイントが幾つかありました。

その作成過程で躓いた箇所とその対応の内容が書かれています。

Flutterとはなにか、ふんわりと

Flutterは、クロスプラットフォーム開発で用いられるモバイルアプリケーションフレームワークです。
自分が比較対象としてよく耳にするのはReactNativeやXamarinでしょうか。
こちらの記事「Xamarin と React Native と Flutter の違いを正しく理解しよう」の比較がざっくり掴むのにちょうど良さそうです。

iOSとAndroidに加え、今後はWebも対応する予定です。

つくるものとゴール

今回はGoogle Codelabを題材にしています。
Firebase + Flutterの使い方の雰囲気くらいは掴める内容になっています。

こちらで掲載されている赤ちゃんの名前候補に投票できるアプリの完成を目指します。
基本的にはCodelabの案内に沿って進めていけばよいですが、本記事は躓いた際の補助資料的な位置づけになれば嬉しいです。

使った環境・必要なツール

  • macOS(CPU: 1.3GHz, Intel Core i5)
  • Dart 2.1.0
  • iPhone6s iOS12
  • AndroidStudio 3.2.1
  • VS Code 1.29.1(インストール前提)
  • homebrew 1.8.6(インストール前提)
  • Flutter 1.0.0
  • cloud_firestore 0.8.2

筆者の開発環境はmacOS、検証用デバイスはiPhone6Sを使用しています。
その他のOSの方はごめんなさい。未検証です。
また、デバイスはiOSかAndroidOSのものどちらかが必要です。

Flutter

Flutterの公式からSlutterのSDKをインストールし、プロジェクトを開始します。

DartSDKについては、The Dart SDK is bundled with Flutter; it is not necessary to install Dart separately. とあり、FlutterSDKに紐付けられているため、別々でインストールする必要がないとあります。
が、自分は結局インストールしました。1

zipを解凍し、PATHを通します。
flutter docter [-v]コマンドで、Flutterを使うにあたって現在の自分の環境の状態を確認します。

このあたりはCode labの説明どおり進めます。

以下はflutter docter [-v]時に出たメッセージ。

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.0.0, on Mac OS X 10.14.1 18B75, locale ja-JP)
[✗] Android toolchain - develop for Android devices
    ✗ Unable to locate Android SDK.
      Install Android Studio from: https://developer.android.com/studio/index.html
      On first launch it will assist you in installing the Android SDK components.
      (or visit https://flutter.io/setup/#android-setup for detailed instructions).
      If Android SDK has been installed to a custom location, set $ANDROID_HOME to that location.
      You may also want to add it to your PATH environment variable.

[✗] iOS toolchain - develop for iOS devices
    ✗ Xcode installation is incomplete; a full installation is necessary for iOS development.
      Download at: https://developer.apple.com/xcode/download/
      Or install Xcode via the App Store.
      Once installed, run:
        sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
    ✗ libimobiledevice and ideviceinstaller are not installed. To install with Brew, run:
        brew update
        brew install --HEAD usbmuxd
        brew link usbmuxd
        brew install --HEAD libimobiledevice
        brew install ideviceinstaller
    ✗ ios-deploy not installed. To install with Brew:
        brew install ios-deploy
    ✗ CocoaPods not installed.
        CocoaPods is used to retrieve the iOS platform side's plugin code that responds to your plugin usage on the Dart side.
        Without resolving iOS dependencies with CocoaPods, plugins will not work on iOS.
        For more info, see https://flutter.io/platform-plugins
      To install:
        brew install cocoapods
        pod setup
[!] Android Studio (not installed)
[!] VS Code (version 1.29.1)
[!] Connected device
    ! No devices available

! Doctor found issues in 5 categories.

[✗]は基本的には解決しなければならない問題で、[!]に関しては無視しても先に進める問題です。
今回はすべて[✓]になるまで解決します。

まずはiOS関連のErrorから解決を試みます。

iOS

下記に注意。

MacOSは、iOSとAndroid用のFlutterアプリの開発をサポートしています。最初のFlutterアプリケーションをビルドして実行するには、2つのプラットフォーム設定ステップの少なくとも1つを完了してください。
https://flutter.io/docs/get-started/install/macos#platform-setup

私はiOSの環境を整えました。
Apple Developer Agreementの処理を行います。
その後はXCodeをインストールしたあとhomebrewで下記をインストールします。

sudo xcodebuild -license accept  
brew update  
brew install --HEAD usbmuxd  
brew link usbmuxd  
brew install --HEAD libimobiledevice  
brew install ideviceinstaller

このあと続けて

brew install ios-deploy
brew install cocoapods

を実行。
CocoaPodsはiOSライブラリ管理ツールです。

iOS toolchainのError

Error: The `brew link` step did not complete successfullyと言われる場合、brew uninstallを行う必要があります。
私は下記を実行したらErrorが消えました。

brew update  
brew uninstall --ignore-dependencies libimobiledevice  
brew uninstall --ignore-dependencies usbmuxd  
brew install --HEAD usbmuxd  
brew unlink usbmuxd  
brew link usbmuxd  
brew install --HEAD libimobiledevice`  

flutter docter [-v]で確認したところ、iOS toolchainのエラーが消えたらOKです。

CocoaPods installed but not initialized.

pod setupで解決しました。

10分弱待たされる。
実行結果ログは下記の通り。

Setting up CocoaPods master repo
  $ /usr/bin/git clone https://github.com/CocoaPods/Specs.git master --progress
  Cloning into 'master'...
  remote: Enumerating objects: 316, done.
  remote: Counting objects: 100% (316/316), done.
  remote: Compressing objects: 100% (231/231), done.
  remote: Total 2668685 (delta 125), reused 153 (delta 72), pack-reused 2668369
  Receiving objects: 100% (2668685/2668685), 597.44 MiB | 3.89 MiB/s, done.
  Resolving deltas: 100% (1573495/1573495), done.
  Checking out files: 100% (289715/289715), done.

CocoaPods 1.6.0.beta.2 is available.
To update use: `gem install cocoapods --pre`
[!] This is a test version we'd love you to try.

For more information, see https://blog.cocoapods.org and the CHANGELOG for this version at https://github.com/CocoaPods/CocoaPods/releases/tag/1.6.0.beta.2

Setup completed

flutter docter [-v]でも確認。

[✓] iOS toolchain - develop for iOS devices (Xcode 10.1)
[!] Android Studio (not installed)
[!] VS Code (version 1.29.1)
[!] Connected device
    ! No devices available

ということで、iOSの設定はOK。

Android Studio

公式サイトからインストール。

別途AndroidSDKのインストールは必要ありません。
flutter docterコマンド実行結果のAndroidの欄からも案内が確認できます。(下記参照)

[✗] Android toolchain - develop for Android devices
    ✗ Unable to locate Android SDK.
      Install Android Studio from: https://developer.android.com/studio/index.html
      On first launch it will assist you in installing the Android SDK components.
      (or visit https://flutter.io/setup/#android-setup for detailed instructions).
      If Android SDK has been installed to a custom location, set $ANDROID_HOME to that location.
      You may also want to add it to your PATH environment variable.

Android Studioを起動して、SDKをインストールする案内があるので実行してください。
Android Studioをインストール&起動しただけではAndroid SDKはされず、✗は消えないので注意してください。

Android Licenceの承諾

Android SDKインストール後、flutter doctor [-v]を実行すると、下記のメッセージが出ます。

[!] Android toolchain - develop for Android devices (Android SDK 28.0.3)
    ✗ Android licenses not accepted.  To resolve this, run: flutter doctor --android-licenses

示されているコマンドflutter doctor --android-licensesを実行するとライセンスの承諾の処理が始まります。
私はここですべてyにしないと[!]が消えませんでした。
GoogleTV〜などとでてくるので、最初は必要ないと思い承諾しなかったのですが、最終的にはすべてyを選択しました。

ライセンス認証後の状態でまたflutter doctor [-v]を実行すると下記のとおりになりました。

[✓] Android Studio (version 3.2)
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.

この2つの問題はAndoroid StudioのPreferenceからプラグインをインストールすれば解決します。
Android Studio Preferences > Pluginsを選択し、
Install JetBrains plugin -> Dart
Browse Repositories -> Flutter
をインストールで対応できます。

その結果…[✘]はなくなりました。

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.0.0, on Mac OS X 10.14.1 18B75, locale ja-JP)
[✓] Android toolchain - develop for Android devices (Android SDK 28.0.3)
[✓] iOS toolchain - develop for iOS devices (Xcode 10.1)
[✓] Android Studio (version 3.2)
[!] VS Code (version 1.29.1)
[✓] Connected device (1 available)

! Doctor found issues in 1 category.

Android

AVDが起動画面から動かない

Androidも少しは試したのでかける箇所を共有。
起動はしたものの、そこから一向にAVDが動かなくなりました。

System imageをRecommendのものをそのまま使っていたましたが、x86のx86_64のものを使うことで解決しました。

https://akira-watson.com/android/avd-manager.html

これは私のマシンスペックがしょぼかったからの可能性もある…?

VS Code

VSCodeの[!]は、VSCodeにFlutterとDartのプラグインを追加すれば解決することが多いと思います。
もちろんエラーの内容はflutter doctor -vで確認してください。

ここでDartのプラグインを入れようとしますが、今度はVSCodeでDartのSDKがないと怒られが発生します。
これが、上で述べたDartのSDKを入れた理由です。
VS Codeの指示に従ってDart公式サイトのインストールページからインストールします。

サイトに飛ぶと、入れるDart SDKの候補が現在は3つ並んでいます。
私はWebを選択しましたが、問題なく動きました。

brew tap dart-lang/dartCould not link

下記のようなErrorが出る場合。

Error: Could not link:
/usr/local/etc/bash_completion.d/brew

Please delete these paths and run `brew update`.
Error: Could not link:
/usr/local/share/zsh/site-functions/_brew

Please delete these paths and run `brew update`.
Error: Could not link:
/usr/local/share/man/man1/brew.1

Please delete these paths and run `brew update`.
Error: Could not link:
/usr/local/share/doc/homebrew

Please delete these paths and run `brew update`.
==> Tapping dart-lang/dart
Cloning into '/usr/local/Homebrew/Library/Taps/dart-lang/homebrew-dart'...
remote: Enumerating objects: 7, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 7 (delta 1), reused 2 (delta 0), pack-reused 0
Unpacking objects: 100% (7/7), done.
Tapped 3 formulae (35 files, 33.5KB).

対応として下記を実行しました。

rm -rf /usr/local/share/doc/homebrew
rm -rf /usr/local/etc/bash_completion.d/brew
rm -rf /usr/local/share/zsh/site-functions/_brew
rm -rf /usr/local/share/man/man1/brew.1
brew update

Already up-to-date.が表示されました。
再度brew tap dart-lang/dartbrew install dartを実行した結果が以下。

==> Installing dart from dart-lang/dart
==> Downloading https://storage.googleapis.com/dart-archive/channels/stable/release/2.1.0/sdk/dartsdk-macos-x64-release.zip
######################################################################## 100.0%
==> Caveats
Please note the path to the Dart SDK:
  /usr/local/opt/dart/libexec
==> Summary
🍺  /usr/local/Cellar/dart/2.1.0: 339 files, 300.1MB, built in 30 seconds

成功したみたいですね。
dart --versionの結果も

Dart VM version: 2.1.0 (Tue Nov 13 18:22:02 2018 +0100) on "macos_x64"

で、無事インストールが確認できた。

その他flutter コマンド実行時

flutter create {任意のプロジェクト名}

"{作成したFlutterプロジェクト名}" is not a valid Dart package name.はパッケージ名の不正です。

"Flutterfire-Sample" is not a valid Dart package name.

From the [Pubspec format description](https://www.dartlang.org/tools/pub/pubspec.html):

**DO** use `lowercase_with_underscores` for package names.

Package names should be all lowercase, with underscores to separate words,
`just_like_this`.  Use only basic Latin letters and Arabic digits: [a-z0-9_].
Also, make sure the name is a valid Dart identifier -- that it doesn't start
with digits and isn't a reserved word.

ファイルの命名規則にひっかかってます。
この場合、just_like_this部のようにflutter create flutter_fire_sampleと名前を変えてプロジェクトを作成してみるとOKでした。

flutter createコマンドが成功した様子の見本(長いです)

Creating project flutter_fire_sample...
  flutter_fire_sample/ios/Runner.xcworkspace/contents.xcworkspacedata (created)
  flutter_fire_sample/ios/Runner/Info.plist (created)
  flutter_fire_sample/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@2x.png (created)
  flutter_fire_sample/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@3x.png (created)
  flutter_fire_sample/ios/Runner/Assets.xcassets/LaunchImage.imageset/README.md (created)
  flutter_fire_sample/ios/Runner/Assets.xcassets/LaunchImage.imageset/Contents.json (created)
  flutter_fire_sample/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage.png (created)
  flutter_fire_sample/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@2x.png (created)
  flutter_fire_sample/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@1x.png (created)
  flutter_fire_sample/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@1x.png (created)
  flutter_fire_sample/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@1x.png (created)
  flutter_fire_sample/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-1024x1024@1x.png (created)
  flutter_fire_sample/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-83.5x83.5@2x.png (created)
  flutter_fire_sample/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@3x.png (created)
  flutter_fire_sample/ios/Runner/Assets.xcassets/AppIcon.appiconset/Contents.json (created)
  flutter_fire_sample/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@2x.png (created)
  flutter_fire_sample/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@3x.png (created)
  flutter_fire_sample/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@2x.png (created)
  flutter_fire_sample/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@3x.png (created)
  flutter_fire_sample/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@2x.png (created)
  flutter_fire_sample/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@1x.png (created)
  flutter_fire_sample/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@3x.png (created)
  flutter_fire_sample/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@2x.png (created)
  flutter_fire_sample/ios/Runner/Base.lproj/LaunchScreen.storyboard (created)
  flutter_fire_sample/ios/Runner/Base.lproj/Main.storyboard (created)
  flutter_fire_sample/ios/Runner.xcodeproj/project.xcworkspace/contents.xcworkspacedata (created)
  flutter_fire_sample/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme (created)
  flutter_fire_sample/ios/Flutter/Debug.xcconfig (created)
  flutter_fire_sample/ios/Flutter/Release.xcconfig (created)
  flutter_fire_sample/ios/Flutter/AppFrameworkInfo.plist (created)
  flutter_fire_sample/test/widget_test.dart (created)
  flutter_fire_sample/flutter_fire_sample.iml (created)
  flutter_fire_sample/.gitignore (created)
  flutter_fire_sample/.metadata (created)
  flutter_fire_sample/ios/Runner/AppDelegate.h (created)
  flutter_fire_sample/ios/Runner/main.m (created)
  flutter_fire_sample/ios/Runner/AppDelegate.m (created)
  flutter_fire_sample/ios/Runner.xcodeproj/project.pbxproj (created)
  flutter_fire_sample/android/app/src/main/res/mipmap-mdpi/ic_launcher.png (created)
  flutter_fire_sample/android/app/src/main/res/mipmap-hdpi/ic_launcher.png (created)
  flutter_fire_sample/android/app/src/main/res/drawable/launch_background.xml (created)
  flutter_fire_sample/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png (created)
  flutter_fire_sample/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png (created)
  flutter_fire_sample/android/app/src/main/res/values/styles.xml (created)
  flutter_fire_sample/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png (created)
  flutter_fire_sample/android/app/src/main/AndroidManifest.xml (created)
  flutter_fire_sample/android/gradle/wrapper/gradle-wrapper.properties (created)
  flutter_fire_sample/android/gradle.properties (created)
  flutter_fire_sample/android/settings.gradle (created)
  flutter_fire_sample/pubspec.yaml (created)
  flutter_fire_sample/README.md (created)
  flutter_fire_sample/lib/main.dart (created)
  flutter_fire_sample/android/app/build.gradle (created)
  flutter_fire_sample/android/app/src/main/java/com/example/flutterfiresample/MainActivity.java (created)
  flutter_fire_sample/android/build.gradle (created)
  flutter_fire_sample/android/flutter_fire_sample_android.iml (created)
  flutter_fire_sample/.idea/runConfigurations/main_dart.xml (created)
  flutter_fire_sample/.idea/libraries/Flutter_for_Android.xml (created)
  flutter_fire_sample/.idea/libraries/Dart_SDK.xml (created)
  flutter_fire_sample/.idea/libraries/KotlinJavaRuntime.xml (created)
  flutter_fire_sample/.idea/modules.xml (created)
  flutter_fire_sample/.idea/workspace.xml (created)
Running "flutter packages get" in flutter_fire_sample...     5.5s
Wrote 64 files.

All done!
[✓] Flutter is fully installed. (Channel stable, v1.0.0, on Mac OS X 10.14.1 18B75, locale ja-JP)
[✓] Android toolchain - develop for Android devices is fully installed. (Android SDK 28.0.3)
[✓] iOS toolchain - develop for iOS devices is fully installed. (Xcode 10.1)
[✓] Android Studio is fully installed. (version 3.2)
[✓] VS Code is fully installed. (version 1.29.1)
[✓] Connected device is fully installed. (2 available)

In order to run your application, type:

  $ cd flutter_fire_sample
  $ flutter run

Your application code is in flutter_fire_sample/lib/main.dart.

flutter runをうつ。

More than one device connected; please specify a device with the '-d <deviceId>' flag, or use '-d all' to act on all devices.
{hogehoge} の iPhone • {xxxxxxxxxxxxxxxxxxxxxxxxxxxx} • ios • iOS 12.1

実行できるようになりました。

flutter run

note: Using new build systemnote: Planning buildnote: Using build description from memory

CodelabにはXcodeのBuild SystemはLegacyに変更する必要との記述があります
リンク先のTroubleshooting Xcode build fail:部が該当箇所です。

ほかの参照

https://github.com/flutter/flutter/issues/22123#issuecomment-423618945
https://github.com/flutter/flutter/issues/19241#issuecomment-404601754

iPhoneでのデバイスの認証

またiPhoneで、一般 -> デバイス管理 -> デベロッパでアプリを認証を許可していなくてもBuildはコケます。
そちらも許可する必要があります。

Error: No pubspec.yaml file found.

下記のエラーは、作成したプロジェクトディレクトリ直下でflutter runすればOKです。
pubspec.yamlは、雑に言えば依存関係を管理するファイルで、配置場所は作成したflutter runで作成したプロジェクトの直下です。

Error: No pubspec.yaml file found.
This command should be run from the root of your Flutter project.
Do not run this command from the root of your git clone of Flutter.

The default Firebase app has not yet been configured....

Launching lib/main.dart on {hogehoge} の iPhone in debug mode...
Automatically signing iOS for device deployment using specified development team in Xcode project: 8KYC4WC652
Running pod install...
Starting Xcode build...
Xcode build done.                                           627.3s
Installing and launching...
5.14.0 - [Firebase/Core][I-COR000003] The default Firebase app has not yet been configured. Add `[FIRApp configure];` (`FirebaseApp.configure()` in Swift) to your application initialization. Read more: https://goo.gl/ctyzm8.
5.14.0 - [Firebase/Core][I-COR000012] Could not locate configuration file: 'GoogleService-Info.plist'.
5.14.0 - [Firebase/Core][I-COR000005] No app has been configured yet.
5.14.0 - [Firebase/Core][I-COR000003] The default Firebase app has not yet been configured. Add `[FIRApp configure];` (`FirebaseApp.configure()` in Swift) to your application initialization. Read more: https://goo.gl/ctyzm8.
5.14.0 - [Firebase/Core][I-COR000012] Could not locate configuration file: 'GoogleService-Info.plist'.
5.14.0 - [Firebase/Core][I-COR000003] The default Firebase app has not yet been configured. Add `[FIRApp configure];` (`FirebaseApp.configure()` in Swift) to your application initialization. Read more: https://goo.gl/ctyzm8.
5.14.0 - [Firebase/Core][I-COR000012] Could not locate configuration file: 'GoogleService-Info.plist'.

FirebaseにiOSとAndroidで使う登録をしていないため、Errorです。
Firebase consoleにアクセスし、設定を行います。

XcodeからバンドルIDを設定し、アプリのニックネームをつける。
そのあと、GoogleService-Info.plistをダウンロードして、{任意のプロジェクトファイル名}/iOS/Runner/配下に置く。

CocoaPodsのPodfileがない場合は、{hoge}/ios/pod initする。
今回はすでにあるので、Podfileにpod 'Firebase/Core'を追加する。
Podfileの書き方に関してはこちらを参照

Podfileで開いた結果がこれ

  # Flutter Pods
  generated_xcode_build_settings = parse_KV_file('./Flutter/Generated.xcconfig')
  if generated_xcode_build_settings.empty?
    puts "Generated.xcconfig must exist. If you're running pod install manually, make sure flutter packages get is executed first."
  end
  generated_xcode_build_settings.map { |p|
    if p[:name] == 'FLUTTER_FRAMEWORK_DIR'
      symlink = File.join('.symlinks', 'flutter')
      File.symlink(File.dirname(p[:path]), symlink)
      pod 'Flutter', :path => File.join(symlink, File.basename(p[:path]))
    end
  }

  # Plugin Pods
  plugin_pods = parse_KV_file('../.flutter-plugins')
  plugin_pods.map { |p|
    symlink = File.join('.symlinks', 'plugins', p[:name])
    File.symlink(p[:path], symlink)
    pod p[:name], :path => File.join(symlink, 'ios')
  }
end

pod 'Firebase/Core'をどこに書けばいいか

自分はここでOKでした。

...(省略)...

    pod p[:name], :path => File.join(symlink, 'ios')
    pod 'Firebase/Core'
  }
end

flutter packages get

下記のエラーが出る場合は、追加した文字の書き方が悪いと出ます。
yamlファイルだからインデントを揃えればOKです。

[flutter_fire_sample] flutter packages get
Running "flutter packages get" in flutter_fire_sample...        
Error on line 17, column 5 of pubspec.yaml: A dependency may only have one source.
    sdk: flutter
    ^^^^^^^^^^^^^^

pub get failed (65)
exit code 65

正しいのはこちら。

dependencies:
  flutter:
    sdk: flutter
  cloud_firestore: ^0.8.2     # new

Firebase

Firebaseの登録やプロジェクト作成は公式をお読みください🙇‍♂️

Firebaseで「iOSアプリにFirebaseを追加」の手順4「初期コードの追加」はどこに行えばいいのか

Objective-Cの場合、ios/Runner/AppDelegate.hios/Runner/AppDelegate.mとに書けば解決します。

ios/Runner/AppDelegate.hの内容

#import <Flutter/Flutter.h>
#import <UIKit/UIKit.h>

@import Firebase;

@interface AppDelegate : FlutterAppDelegate

@end

ios/Runner/AppDelegate.mの内容


include "AppDelegate.h"
#include "GeneratedPluginRegistrant.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [FIRApp configure];

  [GeneratedPluginRegistrant registerWithRegistry:self];
  // Override point for customization after application launch.
  return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

@end

参照

https://github.com/flutter/flutter/issues/13534#issuecomment-427620251)

Homebrew使用時

複数ユーザーを使い分けている場合、権限の問題でDart SDKのインストール時にErrorになる場合があります。

MyMacBookAir:flutter_fire_sample swchrm$ brew tap dart-lang/dart  
touch: /usr/local/Homebrew/.git/FETCH_HEAD: Permission denied  
touch: /usr/local/Homebrew/Library/Taps/dart-lang/homebrew-dart/.git/FETCH_HEAD: Permission denied  
touch: /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/.git/FETCH_HEAD: Permission denied  
fatal: Unable to create '/usr/local/Homebrew/.git/index.lock': Permission denied  
fatal: Unable to create '/usr/local/Homebrew/.git/index.lock': Permission denied  
error: could not lock config file .git/config: Permission denied  

sudoでもだめ。

MyMacBookAir:traveling_jirorian_problem_flutter swchrm$ sudo brew tap dart-lang/dart
Password:
Error: Running Homebrew as root is extremely dangerous and no longer supported.
As Homebrew does not drop privileges on installation you would be giving all
build scripts full access to your system.

下記いずれも試したがだめだった。

https://qiita.com/masa_36/items/a811c9335e2e985f6adc

https://qiita.com/k-hotta/items/7236f68ef26f7771b02f

https://qiita.com/yshishido/items/ba5cd86afe217b221457

自分はこれで解決しました。

chmod -R g+w /usr/local/var
chgrp -R admin /usr/local/var

参照

https://gist.github.com/jaibeee/9a4ea6aa9d428bc77925#gistcomment-1943612

おわりに

Flutter+Firebaseかぶった(白目)

本アドベントカレンダーの7日目にFlutterFireの紹介記事が投稿されてしまったこともあり、本当はFirestoreに格納したデータをFlutterで取得してGoogleMap上に表示させる記事を書きたかったのですが、間に合わずでした。
FlutterFireに興味を持った方は上記記事の筆者の方が12/21に書籍を出版されるそうなので、そちらもあわせてどうぞ。僕も買いました。楽しみです。

内容に誤りがありましたら修正しますのでご指摘いただけると幸いです。


  1. VSCodeの設定参照。 

7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7