Line data Source code
1 : import 'destination.dart'; 2 : import 'utils/log/log.dart'; 3 : 4 : /// Defines a redirection. 5 : /// 6 : /// Uses [validator] function to determine if it is allowed to navigate to the 7 : /// given destination. 8 : /// If it is not, user will be redirected to a provided new [destination] 9 : /// on navigation to given destination. 10 : /// 11 : /// You can extend this class and override its [validate()] method to implement 12 : /// more complex logic of validation. 13 : /// 14 : /// See also: 15 : /// - [TheseusRouterDelegate] 16 : /// - [Destination] 17 : /// 18 : class Redirection { 19 : /// Creates a redirection. 20 : /// 21 3 : const Redirection({ 22 : required this.destination, 23 : this.validator, 24 : }); 25 : 26 : /// Destination to redirect. 27 : /// 28 : final Destination destination; 29 : 30 : /// Implements a logic to validate a destination. 31 : /// 32 : /// Must return true if it is allowed to navigate to the destination. 33 : /// Otherwise returns false. 34 : /// 35 : final Future<bool> Function(Destination destination)? validator; 36 : 37 : /// Validates the destination. 38 : /// 39 : /// [TheseusRouterDelegate] uses this method to check if it is needed to redirect to 40 : /// another destination. 41 : /// 42 3 : Future<bool> validate(Destination destination) async { 43 9 : final result = await validator?.call(destination) ?? false; 44 9 : Log.d(runtimeType, 'validate(): $result'); 45 : return result; 46 : } 47 : }