3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Flutter WebのBase URLを変更する方法

Posted at

通常Flutter Webを起動するとアドレスの直下になってしまいます。
そのBaseの変更方法を紹介します。
既存のサーバーに相乗りする場合などに使います。

これをBaseというのかRootというのか。
Rootで検索すると本記事の3つ目が出てきました。
検索難民の助けになれば幸いです。

Flutter Web の base / を変更する方法

build/web/index.html の15行目あたりの base タグを編集します。

<base href="/">
↓↓↓
<base href="/test/">

これで 192.168.1.1/index.html だったのが 192.168.1.1/test/index.html で実行できます。
既存のサーバーに相乗りする場合に使います。

Flutter WebのURLの後ろに付く # を消す方法

main.dart を編集します。

import 'package:flutter_web_plugins/flutter_web_plugins.dart'; // 追加

void main() {
    setUrlStrategy(PathUrlStrategy()); // 追加
    runApp(MyApp());
}

これで消えます。URLの後ろに # が付くのが鬱陶しいと思っていました。

Flutter Webのページのルートを変更する方法

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      routes: {
        '/': (context) => MainScreen(),
        '/test': (context) => TestScreen(), // 最後にスラッシュは不要
      },

これでブラウザでURLから直接ページに移動できます。
例 192.168.1.1/#/test

注意:「後ろに付く # を消す」状態だとこの方法は無効です。やはり # は残しておいた方が良さげです。

3
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?