Line data Source code
1 : import 'dart:convert'; 2 : 3 : import 'package:flutter/foundation.dart'; 4 : import 'package:flutter/widgets.dart'; 5 : 6 : import 'beam_state.dart'; 7 : 8 : /// Converts [RouteInformation] to [BeamState] and vice-versa. 9 : class BeamerParser extends RouteInformationParser<BeamState> { 10 7 : BeamerParser({this.onParse = _identity}); 11 : 12 7 : static BeamState _identity(BeamState state) => state; 13 : 14 : /// A custom closure to execute after route information has been parsed 15 : /// into a [BeamState], but before returning it (i.e. before navigation happens). 16 : /// 17 : /// Can be used to inspect and modify the parsed route information. 18 : final BeamState Function(BeamState) onParse; 19 : 20 7 : @override 21 : SynchronousFuture<BeamState> parseRouteInformation( 22 : RouteInformation routeInformation) { 23 7 : final beamState = BeamState.fromUriString( 24 7 : routeInformation.location ?? '/', 25 7 : data: routeInformation.state == null 26 6 : ? {} 27 1 : : Map<String, String>.from( 28 2 : json.decode(routeInformation.state as String), 29 : ), 30 : ); 31 21 : return SynchronousFuture(onParse(beamState)); 32 : } 33 : 34 7 : @override 35 : RouteInformation restoreRouteInformation(BeamState beamState) { 36 7 : return RouteInformation( 37 14 : location: beamState.uri.toString(), 38 14 : state: json.encode(beamState.data), 39 : ); 40 : } 41 : }