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 4 : 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 : /// If validation could be performed synchronously consider return result with 36 : /// [SynchronousFuture]. 37 : /// 38 : final Future<bool> Function(Destination destination)? validator; 39 : 40 : /// Validates the destination. 41 : /// 42 : /// [TheseusRouterDelegate] uses this method to check if it is needed to redirect to 43 : /// another destination. 44 : /// 45 4 : Future<bool> validate(Destination destination) async { 46 12 : final result = await validator?.call(destination) ?? false; 47 12 : Log.d(runtimeType, 'validate(): $result'); 48 : return result; 49 : } 50 : }