openLaunchLink static method

Future<String?> openLaunchLink({
  1. required Future<String?> link(),
  2. required Future<void> open(
    1. String route
    ),
  3. String filesRoute = '/open',
})

Opens whatever route this launch was handed a link to, and answers with it.

The mobile half of start. A desktop launch carries its link as a command-line argument, which is why start takes arguments; Android and iOS carry it on the launch itself -- the Activity's intent on one, the two AppDelegate overrides dartvel build writes on the other -- and it is read back through a native binding rather than off argv.

Everything either side of this existed and nothing joined them up. The capture was written on both platforms, the binding that reads it was registered on both, and the launch path returned before reaching either because it starts on desktop. A home widget's tap therefore opened the application at whatever route it starts at, which is a shortcut rather than a widget, and looks enough like success that nobody reports it.

link is asked once and may fail: deepLinks.initial is registered on five platforms and this runs on every one, so a target that has no binding for it must come up rather than take the application down at startup.

Answers null when the launch carried nothing, which is the ordinary case -- somebody tapped the application's own icon, and navigating them somewhere would be worse than doing nothing.

Implementation

static Future<String?> openLaunchLink({
  required Future<String?> Function() link,
  required Future<void> Function(String route) open,
  String filesRoute = '/open',
}) async {
  String? value;
  try {
    value = await link();
  } on Object {
    return null;
  }
  final String text = value?.trim() ?? '';
  if (text.isEmpty) return null;
  final String? route = routeFor(text, filesRoute: filesRoute);
  if (route == null) return null;
  // What DV.Platform.deepLinks answers as well, so an application reading
  // the initial link for itself sees the one the router acted on rather
  // than nothing. Two answers to "what launched this" is a page opened
  // twice or opened and immediately left.
  _initialLink ??= text;
  const DVDeepLinks().dispatch(text);
  await open(route);
  return route;
}