URL strategy
- Flutter course
- Expert
- Lesson 28/39
- 4 min
Remove the URL '#' with setUrlStrategy.
URL strategy
setUrlStrategy controls the # sign in the web URL. The default HashUrlStrategy puts a # in the URL; PathUrlStrategy uses a clean path.
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
void main() {
setUrlStrategy(PathUrlStrategy());
runApp(MyApp());
}Hash vs Path
A hash URL (example.com/#/page) requires no server configuration. A path URL (example.com/page) looks clean but requires server-side routing; otherwise a refresh returns 404.
Practice Strategy variable
In main, assign 'PathUrlStrategy' to strategy, assign false to hasHash, and print strategy.
Starter code
void main() {
// strategy değişkenine PathUrlStrategy ata
// hasHash değişkenine false ata (hash kullanılmaz)
// strategy değişkenini yazdır
}Sample solution
void main() {
const strategy = 'PathUrlStrategy';
const hasHash = false;
print(strategy);
}Expected output
PathUrlStrategy
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does PathUrlStrategy do?
- Resets the URL
- Removes the '#' from the URL Correct answer
- Reloads the page
- Adds '#' to the URL
Explanation: PathUrlStrategy uses a clean path and removes the '#' from the URL; HashUrlStrategy adds it, and resetting or reloading is not the strategy's job.
2 Use PathUrlStrategy
setUrlStrategy(____());
setUrlStrategy(PathUrlStrategy());
Explanation: PathUrlStrategy uses a clean hash-less URL; HashUrlStrategy adds '#', UrlStrategy is the abstract parent, and CleanUrlStrategy does not exist in dart:ffi.
3 URL strategy matches
| Term | Meaning |
|---|---|
setUrlStrategy |
URL strategy setting |
path |
Clean URL path |
URL |
Web address |
PathUrlStrategy |
Hash-less strategy |