5

I was able to navigate to other pages by changing the url of my flutter web hosted on netlify(manually deployed) by typing the url in search bar as shown below:- enter image description here

So if i change /home to /about then it leads me perfectly to about page. But to remove this '#' from my url i added the following code in my main.dart My Code:-

import 'package:url_strategy/url_strategy.dart';

void main()async {
  await Firebase.initializeApp();
  setPathUrlStrategy();//this removes the '#' from my url
  FluroRouting.setupRouter();
  runApp(MyApp());
}
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'My Website',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      initialRoute: '/home',
      onGenerateRoute: FluroRouting.router.generator,
    );
  }
}

and code for routing is:-

class FluroRouting {
  static final router = FluroRouter();
  static Handler _aboutUsHandler = Handler(
      handlerFunc: (BuildContext context, Map<String, dynamic> params) =>
          AboutUs());
  static Handler _homeHandler = Handler(
      handlerFunc: (BuildContext context, Map<String, dynamic> params) =>
          HomePage());
  static void setupRouter() {
    router.define('/home', handler: _homeHandler,);
    router.define('/about', handler: _aboutUsHandler,);
  }
  static void navigateToPage({String routeName,BuildContext context}) {
    router.navigateTo(context, routeName, transition: TransitionType.none);
  }
  static void pushAndClearStackToPage({String routeName,BuildContext context}) {
    router.navigateTo(context, routeName, clearStack: true,transition: TransitionType.none);
  }
}

After doing so the '#' was successfully removed but when i change /home to /about then it shows page not found error. enter image description here

Deepak Lohmod
  • 2,072
  • 2
  • 8
  • 18

4 Answers4

0

I found the solution for this problem,in my case at least, my best guess is that the way the path domain is configured in free deploy for testing websites like surge, netlify and even Firebase hosting is breaking the functionality of setPathURl(). I used a domain that I've bought some time prior and it worked like a charm, but if I try to access the same website by using the firebase hosting domain it didn't worked.

Use a real domain to test functionalities of usePathUrlStrategy

Pen Drive
  • 95
  • 7
0

The path URL strategy should be called separately and assigned as given in the official document for web projects.

https://flutter.dev/docs/development/ui/navigation/url-strategies

I set the path URL strategy in my project as given below,

void main() {
  configureApp();
  runApp(MyApp());
}

void configureApp() {
  setUrlStrategy(PathUrlStrategy());
}

FYI, I have also hosted it for production and it works fine.

Can you also post your code on how you assign routes?

AmateurCoder
  • 153
  • 10
0

I had the same problem and fixed it by adding an .htaccess file to the hosting site, next to index.html, with the following configuration:

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.html?path=$1 [NC,L,QSA]
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
DYY
  • 71
  • 7
0

I created a file next to where the index.html is in the web folder with

/*    /index.html    200

the file call "_redirects" and work to me

Juan Labrador
  • 1,204
  • 10
  • 14