もっぺんプログラミング(´・ω・`)

もっぺん頑張って副業プログラマを目指してます。

100日後にFlutter生活:12日目:6章でとりあえず画面が出るまで

6章のサンプルコードで、とりあえずトップ画面が出るところまでたどり着けた…

僕の知識不足に、本の説明不足感もあいまって なかなかの難易度やった…

Flutter モバイルアプリ開発バイブル

やったこと。

Git clone でソースコードを取得

↓こんなん

> git clone https://github.com/AwaseFlutter/dart_ffi_example.git

GitHubリポジトリ GitHub - AwaseFlutter/dart_ffi_example: 『Flutter モバイルアプリ開発バイブル』の「付録 Flutter 1.9」における実装サンプルです。

flutter pod get で、必要なパッケージ取得

ソースを取得したら、Sampleフォルダに移動して、必要なパッケージを取得

> cd Sample
> flutter pub get
ソースコードの警告を修正

Eventクラスのコンストラクタで警告が出てたので修正。

date が必須になってるのに、代入文が書かれてなかった。
こ、このサンプルコード、動くのかな?ドキドキ…

↓この一番下の行を追記しました。

  Event({
    @required this.id,
    @required this.title,
    @required this.description,
    @required this.date,
    @required this.imageUrl,
  })  : assert(id != null),
        assert(title != null),
        assert(description != null),
        assert(date != null),
        assert(imageUrl != null);

  static Event fromJson(Map<String, dynamic> json) {
    return Event(
      id: json['id'],
      title: json['title'],
      date: json['date'],
iOSのビルドが終わらない問題

Xcodeで、ドメインをユニークなものに変更しないとダメだった。 忘れてました。

ので、Sample/ios 以下をXCodeで開いて、Runnerのドメイン名を変更

f:id:momoizo:20210125005315p:plain

Firebaseにプロジェクト登録

↓以下の手順に従って、プロジェクト登録
 初めての人は、Firebaseの利用登録からかな?

Flutter アプリに Firebase を追加する

手順中に、GoogleService-info.list をダウンロードする場面が出てくるので
ダウンロードして、XCodeを開いて、Runnerディレクトリの直下に追加。
XCodeで実施しないとダメな模様)

__FIRAPP_DEFAULT does not exist. にも対応

コンソールに以下のメッセージが出てて、気になったので調べて対応。

6.23.0 - [Firebase/Core][I-COR000004] App with name __FIRAPP_DEFAULT does not exist.
Configured the default Firebase app __FIRAPP_DEFAULT.

Objective-C でプロジェクトが生成されてたので以下を修正

※ファイルは、Sample/ios/Runner/Runner にある。

AppDelegate.h

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

@import Firebase;

@interface AppDelegate : FlutterAppDelegate

@end

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

参考: FlutterでFirebaseを利用するために[FIRApp configure];を追加 | by Kaoru Mori | Medium

Unhandled Exception: に対応

デバッグ起動すると、まだ以下が出るので調べて対応。

[VERBOSE-2:ui_dart_state.cc(177)] Unhandled Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.
If you're running an application and need to access the binary messenger before `runApp()` has been called (for example, during plugin initialization), then you need to explicitly call the `WidgetsFlutterBinding.ensureInitialized()` first.
If you're running a test, you can call the `TestWidgetsFlutterBinding.ensureInitialized()` as the first line in your test's `main()` method to initialize the binding.
#0      defaultBinaryMessenger.<anonymous closure>
package:flutter/…/services/binary_messenger.dart:92
#1      defaultBinaryMessenger
package:flutter/…/services/binary_messenger.dart:105
#2      MethodChannel.binaryMessenger
package:flutter/…/services/platform_channel.dart:143
#3      MethodChannel.setMethodCallHandler
package:flutter/…/services/platform_channel.dart:379
#4      new FirebaseAuth._ (package:firebase_auth/src/firebase_aut<…>

エラーメッセージに、main メソッドで初期化処理を呼ぶコードを追加っと

void main() {
  WidgetsFlutterBinding.ensureInitialized();

これで、一応、トップ画面が出るようになりました!

けど、ボタンを押したらアプリが落ちる… エラーメッセージを何も出さずに終わるから、ここからさらに調査かな〜

(´・ω・`)サンプルコードの難易度高すぎるぅ