Метод parseRouteInformation
Метод parseRouteInformation класса RouteInformationParser на вход приходит RouteInformation, на выходе - тип конфигурации delegate.
Синтаксис
@override
Future<AppConfig> parseRouteInformation(
RouteInformation routeInformation,
) async {
return AppConfig(routeInformation.uri.path);
}
Параметры
| Параметр | Описание |
|---|---|
routeInformation |
Текущий URI и state от Router или платформы.
|
Пример
Давайте распарсим query параметр:
import 'package:flutter/material.dart';
class QueryConfig
{
QueryConfig(this.tag);
final String tag;
}
class QueryParser extends RouteInformationParser<QueryConfig>
{
@override
Future<QueryConfig> parseRouteInformation(
RouteInformation routeInformation,
) async {
var tag = routeInformation.uri.queryParameters['tag'] ?? 'abcde';
return QueryConfig(tag);
}
@override
RouteInformation restoreRouteInformation(QueryConfig configuration) {
return RouteInformation(
uri: Uri.parse('/?tag=${configuration.tag}'),
);
}
}
void main()
{
var parser = QueryParser();
parser.parseRouteInformation(
RouteInformation(uri: Uri.parse('/?tag=12345')),
).then((cfg) {
runApp(
MaterialApp(
home: Scaffold(
body: Center(
child: Text(cfg.tag),
),
),
),
);
});
}
На экране по центру '12345' из query URI.
Пример
Давайте вернуть конфигурацию по умолчанию для пустого path:
import 'package:flutter/material.dart';
class QueryConfig
{
QueryConfig(this.tag);
final String tag;
}
class QueryParser extends RouteInformationParser<QueryConfig>
{
@override
Future<QueryConfig> parseRouteInformation(
RouteInformation routeInformation,
) async {
if (routeInformation.uri.path == '/') {
return QueryConfig('abcde');
}
return QueryConfig(routeInformation.uri.path);
}
@override
RouteInformation restoreRouteInformation(QueryConfig configuration) {
return RouteInformation(uri: Uri.parse('/'));
}
}
void main()
{
QueryParser().parseRouteInformation(
RouteInformation(uri: Uri.parse('/')),
).then((cfg) {
runApp(
MaterialApp(
home: Scaffold(
body: Center(child: Text(cfg.tag)),
),
),
);
});
}
На экране по центру 'abcde' для корневого URI.
Смотрите также
-
метод
restoreRouteInformation,
обратное преобразование -
класс
RouteInformation,
аргументparseRouteInformation -
метод
setNewRoutePath,
принимает результат парсинга -
класс
RouteInformationParser,
владелец метода