Tag: Flutter

  • 【Flutter】primarySwatch: Colors.blue,はもう古い。これからは〇〇の時代

    Flutterで

    final ThemeData darkTheme = ThemeData(
      fontFamily: 'NotoSansJP',
      primarySwatch: Colors.blue,
    );

    のようにprimarySwatch は使えなくなっています。

    どうすれば良いか?

    現在(25年5月)全体の色変更するには

    final ThemeData darkTheme = ThemeData(
      fontFamily: 'NotoSansJP',
      colorScheme: ColorScheme.fromSeed (
        seedColor: Colors.blue, 
        brightness: Brightness.dark, 
        dynamicSchemeVariant: DynamicSchemeVariant.tonalSpot
      ),
    );

    のように colorScheme を使用する必要があります。

    • 全体の所謂メインカラーを調整するには seedColor を使用してください。
    • ダークモードの設定はColorScheme内でbrightness: Brightness.darkのように指定すればOK
    • トーン(色合い)を変更するにはdynamicSchemeVariant を設定します。

    各プロパティの値については公式ドキュメントを参考にするのが一番です!

    デフォルトの状態

    各色の設定については公式ドキュメントの中断あたりにある例がめちゃくちゃわかりやすいです。ぜひ参考に

    参考

    https://api.flutter.dev/flutter/material/ColorScheme-class.html

    https://api.flutter.dev/flutter/material/MaterialColor-class.html

  • Flutter iOSでビルドできない No such module ‘Flutter’ Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

    Flutter iOSでビルドできない No such module ‘Flutter’ Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

    Flutter で iOS用にbuild するも下記のエラーが AppDelegate.swift で起こってしまい、実行できません。

    No such module ‘Flutter’

    Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

    この記事はこれの解決方法です。

    解決方法

    Flutter プロジェクトのキャッシュを削除します。依存関係の問題を解消するために行います。

    flutter clean

    pubspec.yaml に従ってパッケージのダウンロードや更新、依存関係のセットアップを行います。

    flutter pud get

    ios ディレクトリに移動ます。

    cd ios

    ios/Podfile に基づいて Pods/ フォルダを作成します。また、ios/Podfile.lockを更新し、依存関係のバージョンを固定します。

    pod install

    xcode を書きコマンドで開きます。 Runner.xcodeproj ではないので注意⚠️

    open Runner.xcworkspace

    これで解決しない場合

    flutter をアップグレードしてみましょう。(私はこれで解決しました……。トホホ)アップグレードしたらまた、clean, pub get, pod install の手順を踏んでください。

    flutter upgrade

  • XCode 環境変数追加 Edit Scheme…

    XCode 環境変数追加 Edit Scheme…

    Flutter のプロジェクトで

    import Flutter
    import UIKit
    import GoogleMaps
    
    @main
    @objc class AppDelegate: FlutterAppDelegate {
      override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
      ) -> Bool {
        GMSServices.provideAPIKey(ProcessInfo.processInfo.environment["MY_API_KEY"]!)
        GeneratedPluginRegistrant.register(with: self)
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
      }
    }
    Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

    のエラーが出ました。

    この際直打ちのAPIキーはやめて、環境変数に設置することにしました。

    やり方

    XCodeでプロジェクト名のところをクリックすると Edit Scheme… という項目があるのでクリックします。

    Environment Variables のところに + ボタンを押して、追加します。

    GOOGLE_MAPS_API_KEY
    
    hogehogehogehogehogehogehogeho

    みたいな感じで Name, Value に設定しました。

    その後、AppDelegate .swiftを以下のように書き換えました。

    import Flutter
    import UIKit
    import GoogleMaps
    
    @main
    @objc class AppDelegate: FlutterAppDelegate {
      override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
      ) -> Bool {
        
          // Xcode の環境変数から APIキー を取得
          if let apiKey = ProcessInfo.processInfo.environment["GOOGLE_MAPS_API_KEY"], !apiKey.isEmpty {
              GMSServices.provideAPIKey(apiKey)
          } else {
              fatalError("Google Maps API Key is missing. Please set GOOGLE_MAPS_API_KEY in Xcode's Environment Variables.")
          }
    
        GeneratedPluginRegistrant.register(with: self)
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
      }
    }
    

    これで動作しました!やったー

  • Flutter build iOS error ビルドできない

    Flutter build iOS error ビルドできない

    flutter build ios

    としたところ、以下のようなエラーが出てきた。

    ══════════ No valid code signing certificates were found You can connect to your Apple Developer account by signing in with your Apple ID in Xcode and create an iOS Development Certificate as well as a Provisioning Profile for your project by:   1- Open the Flutter project's Xcode target with        open ios/Runner.xcworkspace   2- Select the 'Runner' project in the navigator then the 'Runner'   target      in the project settings   3- Make sure a 'Development Team' is selected under Signing &   Capabilities > Team.      You may need to:          - Log in with your Apple ID in Xcode first          - Ensure you have a valid unique Bundle ID          - Register your device with your Apple Developer Account          - Let Xcode automatically provision a profile for your app   4- Build or run your project again   5- Trust your newly created Development Certificate on your iOS   device      via Settings > General > Device Management > [your new      certificate] > Trust  For more information, please visit:   https://developer.apple.com/library/content/documentation/IDEs/Conce   ptual/   AppDistributionGuide/MaintainingCertificates/MaintainingCertificates   .html  Or run on an iOS simulator without code signing ══════════════════════════════════════════════════════════════════════ ══════════ No development certificates available to code sign app for device deployment
    ══════════
    No valid code signing certificates were found
    You can connect to your Apple Developer account by signing in with
    your Apple ID
    in Xcode and create an iOS Development Certificate as well as a
    Provisioning
    Profile for your project by:
      1- Open the Flutter project's Xcode target with
           open ios/Runner.xcworkspace
      2- Select the 'Runner' project in the navigator then the 'Runner'
      target
         in the project settings
      3- Make sure a 'Development Team' is selected under Signing &
      Capabilities > Team.
         You may need to:
             - Log in with your Apple ID in Xcode first
             - Ensure you have a valid unique Bundle ID
             - Register your device with your Apple Developer Account
             - Let Xcode automatically provision a profile for your app
      4- Build or run your project again
      5- Trust your newly created Development Certificate on your iOS
      device
         via Settings > General > Device Management > [your new
         certificate] > Trust
    
    For more information, please visit:
      https://developer.apple.com/library/content/documentation/IDEs/Conce
      ptual/
      AppDistributionGuide/MaintainingCertificates/MaintainingCertificates
      .html
    
    Or run on an iOS simulator without code signing
    ══════════════════════════════════════════════════════════════════════
    ══════════
    No development certificates available to code sign app for device
    deployment
    

    解決法

    terminal で ios/Runner.xcworkspace ファイルを開く

    open ios/Runner.xcworkspace
    XCode
    こんな感じで xcode が立ち上がる。

    Xcodeで「Runner」プロジェクトを選択

    「Signing & Capabilities」タブを開く

    私の場合、久しぶりのストアアップロードで規約に同意していないのが原因でした。

    Unable to process request – PLA Update available
    You currently don’t have access to this membership resource. To resolve this issue, agree to the latest Program License Agreement in your developer account.

    StoreConnectから同意したらできました。

    “Automatically manage signing”(自動管理) をチェック

    「Team」のプルダウンから Apple Developer アカウントを選択

    「Provisioning Profile」が作成されるのを確認

    以上です。

    再度以下のコマンドでビルドしなおしましょう。

    flutter build iOS

  • Flutter で Firebase を使うには?導入までのコマンド紹介

    これは何?

    FlutterでFirebaseを使う場合のコマンドの紹介です。

    環境

    • AppleSilicon M1 MacBookAir
    • macOS
    • firebase cli version 13.23.0 
    • Flutter version 3.24.4

    やり方

    1 プロジェクトの作成

    firebase のページにアクセスして、プロジェクトを作成しましょう。

    https://console.firebase.google.com

    firebase

    2 Firebase Cli の設定

    環境ごとに異なると思いますので、各々公式のガイドを参考に進めてください。

    3 CLIの初期操作

    firebase login

    このコマンドでブラウザが立ち上がり、Googleのアカウントの選択と権限の確認が出てきます。

    firebase projects:list

    ログインの確認も兼ねて上記コマンドを実行してみてください。
    ステップ1で作ったプロジェクト名と projectID などが出てくると思います。ProjectID はこの後使うのでコピーしておきましょう。

    4 Flutterへの設定

    まずFlutterプロジェクトのあるディレクトリへ移動しましょう。

    cd あなたのFlutterプロジェクトのディレクトリ

    次に下記コマンドを実行してパッケージを追加します。

    flutter pub add firebase_core

    つづて下記コマンドを実行します。

    dart pub global activate flutterfire_cli

    このコマンドを実行した後、パスを通す必要があります。
    AppleSilicon mac をお使いの方はターミナルはzshだと思いますので下のコマンドを実行します。

    echo 'export PATH="$PATH:$HOME/.pub-cache/bin"' >> ~/.zshrc

    できたら、保存したzshの設定をターミナルに読み込ませるのに下記コマンドを実行します。

    source ~/.zshrc

    最後に以下コマンドを実行します。[さっきコピーしたID]のところは自分のIDで入れ替えてください。

    flutterfire configure --project=さっきコピーしたID

    実行すると

    ✔ Which platforms should your configuration support

    これはどのプラットフォームで使うのか聞かれています。
    スペースを押すとチェックを外せます。
    全部のプラットフォームで使うならこのままEnterを押して進みます。

    次に

    ? Which Android application id (or package name) do you want to use for this configuration, e.g. ‘com.example.app’?

    と聞かれます。これはAndroidのビルドIDなのですが、com.自分の名前.アプリ名 と言った感じでOKです。

    以上でFlutterで使えるようになります。
    お疲れ様でした!