2022年1月31日月曜日

Avoid using web-only libraries outside Flutter web plugin packages.と怒られる

 Flutter web でブラウザをリロードされた際に、その時表示されていたページによって処理を分岐したくて、dart:htmlをimportしてwindow.location.hrefでURLを取得していたら、Avoid using web-only libraries outside Flutter web plugin packages.と怒られるのだが、じゃあ、どうすればいいんだよ!としばらく放置していた。

Uri.base を使えばいいそうだ。

The natural base URI for the current platform.

When running in a browser this is the current URL of the current page (from window.location.href).

When not running in a browser this is the file URI referencing the current working directory.

 

2022年1月17日月曜日

Flutter 開発メモ

 開発中に使用したが、結局使わなくなったのでソースから削除するけど、後で使うかもしれない機能メモ

指定した型の一番近い祖先Widgetを返す

MyApp? myApp = context.findAncestorWidgetOfExactType<MyApp>();

2022年1月13日木曜日

Flutter web でmanifest.jsonが401エラーになる

開発中のFlutter webアプリをサーバにデプロイしてChromeでアクセスしたら、DevToolsのコンソールで、manifest.jsonが 401エラーになっているのがわかった。

なんだよ気持ち悪いなぁと調べていたら、まだ開発中ということでデプロイ先のフォルダにBasic認証をかけているのが原因らしい。

index.html内の

<link rel="manifest" href="manifest.json">

を、以下のように変更したら解消した。

<link rel="manifest" href="manifest.json" crossorigin="use-credentials">

参考 MDN



Flutter web をドキュメントルート以外に設置する

 Flutter web をドキュメントルート以外に設置する方法は、Flutterのドキュメント(Configuring the URL strategy on the web)のHosting a Flutter app at a non-root locationに以下のように書かれている。

Update the <base href="/"> tag in web/index.html to the path where your app is hosted. For example, to host your Flutter app at myapp.dev/flutter_app, change this tag to <base href="/flutter_app/">.

これを受けて、私もbuild/web/index.htmlを直接書き換えていたが、忘れてそのまま設置してしまう可能性もあり、何とかならないのかと思って、build下ではなくプロジェクト直下のweb/index.htmlをみると、以下のように変数指定されている。

<base href="$FLUTTER_BASE_HREF">

これは、どこかで設定しておけるはずだと思い、いろいろ探してみたがそれらしい場所がない。

ターミナルでhelpすると、

> flutter help build web

Usage: flutter build web [arguments]

--base-href                        Overrides the href attribute of the <base> tag in web/index.html. No change is done to web/index.html file

なるほど、上記のドキュメント例なら、以下のようにbuildすればいいということか。

> flutter build web --base-href "/flutter_app/"

ドキュメントに一言、書いておいてください。