Search code examples
urldart

How do I get query parameters in the URL


In Dart I am working on a web game. In this I would need a function where I can get data from the URL, in the same way that you would get it in PHP. How would I do this?

Say I, for example, append the following to my URL when I load my web game: ?id=15&randomNumber=3.14. How can I get them in Dart either as a raw string (preferred) or in some other format?


Solution

  • You can use the Uri class from dart:core
    https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart-core.Uri

    ie:

    main() {
      print(Uri.base.toString()); // http://localhost:8082/game.html?id=15&randomNumber=3.14
      print(Uri.base.query);  // id=15&randomNumber=3.14
      print(Uri.base.queryParameters['randomNumber']); // 3.14
    }