2

I am creating a simple web app for articles with flutter web where the user can navigate to a particular article using a link with parameters containing article id like so webpage.com/articles/123.so far I have read and tried out a bunch of stuff but nothing works. I am able to extract the Id from the URL. However no page is shown when the user tries to do this in the address bar and the user gets redirected to the homepage. How may I achieve this.

3 Answers 3

2

First of all, you need to use routed navigation and define them in the MaterialApp class. When done properly, your Flutter app will directly launch specific ArticlesScreens - you can then share the link (e.g. webpage.com/articles/123), paste it in your browser and then land on your app's ArticlesScreen displaying an article with id 123.

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      onGenerateRoute: (settings) {
        if (settings.name.contains("/articles/")) {
          //parse the URL and get the article ID here
          final String articleId = getArticleIdFromUrl(settings.name);

          return ArticleScreen(articleId: articleId);
        }

        switch (settings.name) {
          case "/":
            return MainScreen();
          default:
            return MainScreen();
        }
      },
      initialRoute: "/",
    );
  }
Sign up to request clarification or add additional context in comments.

Comments

2

I tried the package flutter_modular and it works great.

3 Comments

Hi I am using flutter_modular too. However, when I try to go directly to a url or reload a page it redirects back to my initial route. Did yo have to do anything special to be able to navigate directly to a url?
Nope, I followed the normal documentation on the page and it works.
@user1026498 Hi i also have the same issue after done the URl navigation using Navigator velocity_x library. did you resolved your issue? can you help me too? Thanks in advance
-1

You need to add routes in your Material app:

home: ProjectPage(),
routes: <String, WidgetBuilder>{
      '/home': (BuildContext context) => HomePage(),
      '/login': (BuildContext context) => LoginPage(),
},

2 Comments

you didn't address the url paremeter in your answer
Actually, since then, I only use Getx for route management and navigation and it handles URL parameters very nicely and easily.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.