简体   繁体   English

如何使用 Firebase 主机在 Flutter 中设置 404 未找到页面

[英]how to setup 404 Not found page in Flutter using Firebase Hosting

I am trying to show a custom 404 not found page in my website with Flutter.我正在尝试在我的网站中显示自定义 404 未找到页面,页面编号为 Flutter。

I am using the following code:我正在使用以下代码:

MaterialApp(
        debugShowCheckedModeBanner: false,
        initialRoute: "/",
        routes: {
          "/": (context) => Scaffold(),
          "/test": (context) => TestPage()
        },
        onUnknownRoute: (settings) {
          return MaterialPageRoute(builder: (_) => Scaffold(
            body: Center(
              child: Text("Page not found !"),
            ),
          ));
        }, 

i have rebuild my app and deploy it with Firebase Hosting.我已经重建我的应用程序并使用 Firebase 托管进行部署。 However i am seeing this if i enter a wrong url:但是,如果我输入错误的 url,我会看到这个:

在此处输入图像描述

Since i am using flutter, i don't really want to add an.html file as it is said, even if it works.因为我使用的是 flutter,所以我真的不想添加 an.html 文件,即使它可以工作。
I would rather display a custom page built in dart.我宁愿显示一个内置于 dart 的自定义页面。

How can i display my custom "Page not found" in such case?在这种情况下如何显示我的自定义“找不到页面”?

EDIT :编辑

Here is my current firebase.json file:这是我当前的firebase.json文件:

{
  "hosting": {
    "cleanUrls": true,
    "public": "build/web",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }
}

What i have tried and what didn't work:我尝试了什么,什么没用:

  • Not having rewrites没有重写
  • Having rewrites in hope that flutter routing would take the lead重写希望flutter路由带头
  • Test the project in localhost在本地测试项目
  • Change the Flutter routing Strategy from URL to PATH according to the documentation根据文档将 Flutter 路由策略从 URL 更改为 PATH

In the end:到底:

  • My existing pages in the website are working fine我在网站上的现有页面运行良好
  • All wrong urls are redirected to /index.html所有错误的网址都重定向到 /index.html

The only thing that is working is:唯一有效的是:

  • Adding a 404.html file in my /build/web folder在我的 /build/web 文件夹中添加一个 404.html 文件
  • Remove rewrites in firebase.json删除firebase.json中的重写

However this is not what i want, i would like to display my not_found_page.dart instead.然而,这不是我想要的,我想改为显示我的 not_found_page.dart。

This might be a Flutter issue, i keep this open in the meantime but i will dig deeper on the Flutter side.这可能是一个 Flutter 问题,我在此期间保持开放状态,但我将在 Flutter 方面进行更深入的研究。

The problem was indeed on the Flutter side.问题确实出在Flutter这边。 I was using "/" as initialRoute and "/" as another route which was creating conflicts apparently and was actually useless in my code as the first page returned is defined under the MaterialApp 's child .我将“/”用作initialRoute ,将“/”用作另一条路线,这显然会造成冲突,但实际上在我的代码中毫无用处,因为返回的第一页是在MaterialAppchild页面下定义的。

Flutter was redirecting my routes to "/" instead of calling onUnknownRoute and there was a conflict with the rewrites in firebase.json Flutter 将我的路由重定向到"/"而不是调用onUnknownRoute ,并且与 firebase.json 中的重写发生冲突

So i have removed the "/": (context) => Scaffold(), from the routes map. I have also removed rewrites in firebase.json in order for this to work.所以我从routes map 中删除了"/": (context) => Scaffold(),我还删除了firebase.json中的rewrites ,以使其工作。

I have also set back the URL strategy, all the steps above were not working with the PATH strategy.我还设置了URL策略,上面的所有步骤都不适用于PATH策略。

EDIT : i have figured out that the PATH strategy can work only if there is no home property on the MaterialApp widget.编辑:我发现PATH策略只有在MaterialApp小部件上没有home属性时才能工作。
If so, all of the wrong routes will be redirected to the home and not call the onUnknownRoute如果是这样,所有错误的路线将被重定向home ,而不是调用onUnknownRoute

  • For the / route, the home property, if non-null, is used.对于 / 路由,使用 home 属性(如果非空)。
  • Otherwise, the routes table is used, if it has an entry for the route.否则,如果路由表有路由条目,则使用路由表。
  • Otherwise, onGenerateRoute is called, if provided.否则,如果提供,则调用 onGenerateRoute。 It should return a non-null value for any valid route not handled by home and routes.对于未由 home 和 routes 处理的任何有效路由,它应该返回一个非空值。
  • Finally if all else fails onUnknownRoute is called.最后,如果所有其他方法都失败,则调用 onUnknownRoute。

PS: There is no need to implement 404.html file PS:不需要执行404.html文件

just add a 404.html page in your root and redirect it your own custom flutter built page.只需在您的根目录中添加一个 404.html 页面并将其重定向到您自己的自定义 flutter 构建页面。 you can also learn to handle the你也可以学习处理

'Famous 404 page not found !' “找不到著名的 404 页面!” from here这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Firebase 为 Flutter Web 加载特定的 url 页面 - How can I load a specific url page using Firebase Hosting for Flutter Web Firebase 托管部署错误错误:错误:404,未找到请求的实体 - Firebase hosting deployment error Error: Error: 404, Requested entity was not found Flutter web 应用程序丢失 firebase.json 并在部署到 firebase 托管时显示“欢迎 Firebase 托管设置完成” - Flutter web app is missing firebase.json and shows "Welcome Firebase Hosting Setup Complete" when deployed to firebase hosting 如何在 Flutter 中设置 Firebase 连接的侦听器? - How to setup a listener for Firebase connectivity in Flutter? Firebase 托管 - 加载带有“www”的页面时“找不到站点”。 字首 - Firebase Hosting - "Site not Found" when loading page with "www." prefix 如何设置 Flutter Firebase 桌面应用程序 - how to setup Flutter Firebase desktop application 下一个动态路线 404 firebase 托管 - next dynamic routes 404 firebase hosting 有谁知道启动 firebase 应用程序时如何摆脱“Firebase Hosting Setup Complete”屏幕? - does anyone know how to get rid of "Firebase Hosting Setup Complete" screen when launching a firebase application? 如何使用firebase托管自由部署项目 - How to freely deploy project using firebase hosting Flutter web 应用程序在部署到 firebase 主机后返回空白页 - Flutter web app is returning blank page after being deployed to firebase hosting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM