Change router examples to use SynchronousFuture (#796)

pull/801/head
Michael Goderbauer 4 years ago committed by GitHub
parent 3f5ab56485
commit 252c6fcb57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -5,6 +5,7 @@
/// Full sample that shows a custom RouteInformationParser and RouterDelegate /// Full sample that shows a custom RouteInformationParser and RouterDelegate
/// parsing named routes and declaratively building the stack of pages for the /// parsing named routes and declaratively building the stack of pages for the
/// [Navigator]. /// [Navigator].
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
void main() { void main() {
@ -26,7 +27,7 @@ class BooksApp extends StatefulWidget {
class _BooksAppState extends State<BooksApp> { class _BooksAppState extends State<BooksApp> {
BookRouterDelegate _routerDelegate = BookRouterDelegate(); BookRouterDelegate _routerDelegate = BookRouterDelegate();
BookRouteInformationParser _routeInformationParser = BookRouteInformationParser _routeInformationParser =
BookRouteInformationParser(); BookRouteInformationParser();
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -41,24 +42,25 @@ class _BooksAppState extends State<BooksApp> {
class BookRouteInformationParser extends RouteInformationParser<BookRoutePath> { class BookRouteInformationParser extends RouteInformationParser<BookRoutePath> {
@override @override
Future<BookRoutePath> parseRouteInformation( Future<BookRoutePath> parseRouteInformation(
RouteInformation routeInformation) async { RouteInformation routeInformation,
) {
final uri = Uri.parse(routeInformation.location); final uri = Uri.parse(routeInformation.location);
// Handle '/' // Handle '/'
if (uri.pathSegments.length == 0) { if (uri.pathSegments.length == 0) {
return BookRoutePath.home(); return SynchronousFuture(BookRoutePath.home());
} }
// Handle '/book/:id' // Handle '/book/:id'
if (uri.pathSegments.length == 2) { if (uri.pathSegments.length == 2 && uri.pathSegments[0] == 'book') {
if (uri.pathSegments[0] != 'book') return BookRoutePath.unknown();
var remaining = uri.pathSegments[1]; var remaining = uri.pathSegments[1];
var id = int.tryParse(remaining); var id = int.tryParse(remaining);
if (id == null) return BookRoutePath.unknown(); if (id != null) {
return BookRoutePath.details(id); return SynchronousFuture(BookRoutePath.details(id));
}
} }
// Handle unknown routes // Handle unknown routes
return BookRoutePath.unknown(); return SynchronousFuture(BookRoutePath.unknown());
} }
@override @override
@ -133,17 +135,17 @@ class BookRouterDelegate extends RouterDelegate<BookRoutePath>
} }
@override @override
Future<void> setNewRoutePath(BookRoutePath path) async { Future<void> setNewRoutePath(BookRoutePath path) {
if (path.isUnknown) { if (path.isUnknown) {
_selectedBook = null; _selectedBook = null;
show404 = true; show404 = true;
return; return SynchronousFuture<void>(null);
} }
if (path.isDetailsPage) { if (path.isDetailsPage) {
if (path.id < 0 || path.id > books.length - 1) { if (path.id < 0 || path.id > books.length - 1) {
show404 = true; show404 = true;
return; return SynchronousFuture<void>(null);
} }
_selectedBook = books[path.id]; _selectedBook = books[path.id];
@ -152,6 +154,7 @@ class BookRouterDelegate extends RouterDelegate<BookRoutePath>
} }
show404 = false; show404 = false;
return SynchronousFuture<void>(null);
} }
void _handleBookTapped(Book book) { void _handleBookTapped(Book book) {

@ -6,6 +6,7 @@
/// [BottomNavigationBar] can be used to select the route of the outer /// [BottomNavigationBar] can be used to select the route of the outer
/// RouterDelegate, and additional routes can be pushed onto the inner /// RouterDelegate, and additional routes can be pushed onto the inner
/// RouterDelegate / Navigator. /// RouterDelegate / Navigator.
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
void main() { void main() {
@ -90,18 +91,19 @@ class BooksAppState extends ChangeNotifier {
class BookRouteInformationParser extends RouteInformationParser<BookRoutePath> { class BookRouteInformationParser extends RouteInformationParser<BookRoutePath> {
@override @override
Future<BookRoutePath> parseRouteInformation( Future<BookRoutePath> parseRouteInformation(
RouteInformation routeInformation) async { RouteInformation routeInformation,
) {
final uri = Uri.parse(routeInformation.location); final uri = Uri.parse(routeInformation.location);
if (uri.pathSegments.isNotEmpty && uri.pathSegments.first == 'settings') { if (uri.pathSegments.isNotEmpty && uri.pathSegments.first == 'settings') {
return BooksSettingsPath(); return SynchronousFuture(BooksSettingsPath());
} else { } else {
if (uri.pathSegments.length >= 2) { if (uri.pathSegments.length >= 2 && uri.pathSegments[0] == 'book') {
if (uri.pathSegments[0] == 'book') { return SynchronousFuture(
return BooksDetailsPath(int.tryParse(uri.pathSegments[1])); BooksDetailsPath(int.tryParse(uri.pathSegments[1])),
} );
} }
return BooksListPath(); return SynchronousFuture(BooksListPath());
} }
} }
@ -166,7 +168,7 @@ class BookRouterDelegate extends RouterDelegate<BookRoutePath>
} }
@override @override
Future<void> setNewRoutePath(BookRoutePath path) async { Future<void> setNewRoutePath(BookRoutePath path) {
if (path is BooksListPath) { if (path is BooksListPath) {
appState.selectedIndex = 0; appState.selectedIndex = 0;
appState.selectedBook = null; appState.selectedBook = null;
@ -175,6 +177,7 @@ class BookRouterDelegate extends RouterDelegate<BookRoutePath>
} else if (path is BooksDetailsPath) { } else if (path is BooksDetailsPath) {
appState.setSelectedBookById(path.id); appState.setSelectedBookById(path.id);
} }
return SynchronousFuture<void>(null);
} }
} }
@ -304,10 +307,11 @@ class InnerRouterDelegate extends RouterDelegate<BookRoutePath>
} }
@override @override
Future<void> setNewRoutePath(BookRoutePath path) async { Future<void> setNewRoutePath(BookRoutePath path) {
// This is not required for inner router delegate because it does not // This is not required for inner router delegate because it does not
// parse route // parse route
assert(false); assert(false);
return SynchronousFuture<void>(null);
} }
void _handleBookTapped(Book book) { void _handleBookTapped(Book book) {

@ -6,6 +6,7 @@
/// transition animations are shown. (For example, [when two routes are popped /// transition animations are shown. (For example, [when two routes are popped
/// off the stack](https://github.com/flutter/flutter/issues/12146), however the /// off the stack](https://github.com/flutter/flutter/issues/12146), however the
/// default TransitionDelegate will handle this if you are using Router) /// default TransitionDelegate will handle this if you are using Router)
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
void main() { void main() {
@ -27,7 +28,7 @@ class BooksApp extends StatefulWidget {
class _BooksAppState extends State<BooksApp> { class _BooksAppState extends State<BooksApp> {
BookRouterDelegate _routerDelegate = BookRouterDelegate(); BookRouterDelegate _routerDelegate = BookRouterDelegate();
BookRouteInformationParser _routeInformationParser = BookRouteInformationParser _routeInformationParser =
BookRouteInformationParser(); BookRouteInformationParser();
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -42,14 +43,15 @@ class _BooksAppState extends State<BooksApp> {
class BookRouteInformationParser extends RouteInformationParser<BookRoutePath> { class BookRouteInformationParser extends RouteInformationParser<BookRoutePath> {
@override @override
Future<BookRoutePath> parseRouteInformation( Future<BookRoutePath> parseRouteInformation(
RouteInformation routeInformation) async { RouteInformation routeInformation,
) {
final uri = Uri.parse(routeInformation.location); final uri = Uri.parse(routeInformation.location);
if (uri.pathSegments.length >= 2) { if (uri.pathSegments.length >= 2) {
var remaining = uri.pathSegments[1]; var remaining = uri.pathSegments[1];
return BookRoutePath.details(int.tryParse(remaining)); return SynchronousFuture(BookRoutePath.details(int.tryParse(remaining)));
} else { } else {
return BookRoutePath.home(); return SynchronousFuture(BookRoutePath.home());
} }
} }
@ -113,10 +115,11 @@ class BookRouterDelegate extends RouterDelegate<BookRoutePath>
} }
@override @override
Future<void> setNewRoutePath(BookRoutePath path) async { Future<void> setNewRoutePath(BookRoutePath path) {
if (path.isDetailsPage) { if (path.isDetailsPage) {
_selectedBook = books[path.id]; _selectedBook = books[path.id];
} }
return SynchronousFuture<void>(null);
} }
void _handleBookTapped(Book book) { void _handleBookTapped(Book book) {
@ -213,9 +216,9 @@ class NoAnimationTransitionDelegate extends TransitionDelegate<void> {
Iterable<RouteTransitionRecord> resolve({ Iterable<RouteTransitionRecord> resolve({
List<RouteTransitionRecord> newPageRouteHistory, List<RouteTransitionRecord> newPageRouteHistory,
Map<RouteTransitionRecord, RouteTransitionRecord> Map<RouteTransitionRecord, RouteTransitionRecord>
locationToExitingPageRoute, locationToExitingPageRoute,
Map<RouteTransitionRecord, List<RouteTransitionRecord>> Map<RouteTransitionRecord, List<RouteTransitionRecord>>
pageRouteToPagelessRoutes, pageRouteToPagelessRoutes,
}) { }) {
final results = <RouteTransitionRecord>[]; final results = <RouteTransitionRecord>[];

@ -7,42 +7,42 @@ packages:
name: async name: async
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.5.0-nullsafety.1" version: "2.6.1"
boolean_selector: boolean_selector:
dependency: transitive dependency: transitive
description: description:
name: boolean_selector name: boolean_selector
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.1.0-nullsafety.1" version: "2.1.0"
characters: characters:
dependency: transitive dependency: transitive
description: description:
name: characters name: characters
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.1.0-nullsafety.3" version: "1.1.0"
charcode: charcode:
dependency: transitive dependency: transitive
description: description:
name: charcode name: charcode
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.2.0-nullsafety.1" version: "1.2.0"
clock: clock:
dependency: transitive dependency: transitive
description: description:
name: clock name: clock
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.1.0-nullsafety.1" version: "1.1.0"
collection: collection:
dependency: transitive dependency: transitive
description: description:
name: collection name: collection
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.15.0-nullsafety.3" version: "1.15.0"
cupertino_icons: cupertino_icons:
dependency: "direct main" dependency: "direct main"
description: description:
@ -56,7 +56,7 @@ packages:
name: fake_async name: fake_async
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.2.0-nullsafety.1" version: "1.2.0"
flutter: flutter:
dependency: "direct main" dependency: "direct main"
description: flutter description: flutter
@ -73,21 +73,21 @@ packages:
name: matcher name: matcher
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.12.10-nullsafety.1" version: "0.12.10"
meta: meta:
dependency: transitive dependency: transitive
description: description:
name: meta name: meta
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.3.0-nullsafety.3" version: "1.3.0"
path: path:
dependency: transitive dependency: transitive
description: description:
name: path name: path
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.8.0-nullsafety.1" version: "1.8.0"
sky_engine: sky_engine:
dependency: transitive dependency: transitive
description: flutter description: flutter
@ -99,55 +99,55 @@ packages:
name: source_span name: source_span
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.8.0-nullsafety.2" version: "1.8.1"
stack_trace: stack_trace:
dependency: transitive dependency: transitive
description: description:
name: stack_trace name: stack_trace
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.10.0-nullsafety.1" version: "1.10.0"
stream_channel: stream_channel:
dependency: transitive dependency: transitive
description: description:
name: stream_channel name: stream_channel
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.1.0-nullsafety.1" version: "2.1.0"
string_scanner: string_scanner:
dependency: transitive dependency: transitive
description: description:
name: string_scanner name: string_scanner
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.1.0-nullsafety.1" version: "1.1.0"
term_glyph: term_glyph:
dependency: transitive dependency: transitive
description: description:
name: term_glyph name: term_glyph
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.2.0-nullsafety.1" version: "1.2.0"
test_api: test_api:
dependency: transitive dependency: transitive
description: description:
name: test_api name: test_api
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.2.19-nullsafety.2" version: "0.3.0"
typed_data: typed_data:
dependency: transitive dependency: transitive
description: description:
name: typed_data name: typed_data
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.3.0-nullsafety.3" version: "1.3.0"
vector_math: vector_math:
dependency: transitive dependency: transitive
description: description:
name: vector_math name: vector_math
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.1.0-nullsafety.3" version: "2.1.0"
sdks: sdks:
dart: ">=2.10.0 <2.11.0" dart: ">=2.12.0 <3.0.0"

Loading…
Cancel
Save