5

I am building a flutter web app which requires a url parameter in the start.

Currently it needs a parameter like http://localhost:49844/?id=105897

But after the app catches the url it disappears (like http://localhost:49844/#/) from the web address bar causing the application to fail on refresh of the site. Is there a way to keep the parameter once in the Url path?

In my code I use code below to get the parameter value from the Url

String appUrl = Uri.base.queryParameters["id"].toString();

Also, I use home: instead of routes as I only have a single page to be displayed.

2

2 Answers 2

1

I think you need to try initialRoute and onGenerateRoute instead of home

This is just sample code, you can check docs for a more precise way. And this answer.

  MaterialApp(
    initialRoute: '/?id=105897',
    onGenerateRoute: (settings) {
      Uri uri = Uri.parse(settings.name);
      int id = int.parse(uri.queryParameters['id']);
      return const Home(id);
    },
  );
Sign up to request clarification or add additional context in comments.

1 Comment

What to do for unnamed routes ?
0

Add flutter_web_plugins and execute flutter pub get command in the terminal.

// pubspec.yaml

dependencies:
  flutter:
    sdk: flutter
  flutter_web_plugins:
    sdk: flutter


  cupertino_icons: ^1.0.2
  timelines: ^0.1.0
  image: ^4.1.7
  touchable: ^1.0.2
  encrypt: ^5.0.1
  http: ^0.13.6
  ...

Next, call usePathUrlStrategy() inside main()

// main.dart

import 'package:flutter_web_plugins/flutter_web_plugins.dart';

void main() {
  usePathUrlStrategy();
  runApp(const MyApp());
}

Relaunch the project.

Comments

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.