Navigation 颤振-根据登录状态启动具有不同路由的应用程序
我正在寻找一种方法,根据登录状态在应用程序启动时显示不同的屏幕。例如,我定义了以下路线:Navigation 颤振-根据登录状态启动具有不同路由的应用程序,navigation,flutter,Navigation,Flutter,我正在寻找一种方法,根据登录状态在应用程序启动时显示不同的屏幕。例如,我定义了以下路线: /家 /登录 /背景 当然,我会检查用户是否已经在main()方法中登录,然后将my MaterialApp的initialRoute设置为/login或/home。成功登录后,我可以调用Navigator.pushReplacement导航到/home,登录屏幕将从堆栈中删除。不幸的是,我总是必须为/定义路由,或者设置MaterialApp的home属性。因此,如果我将/设置为空白的容器(),该容器将
- /家
- /登录
- /背景
main()
方法中登录,然后将my MaterialApp的initialRoute
设置为/login或/home。成功登录后,我可以调用Navigator.pushReplacement
导航到/home,登录屏幕将从堆栈中删除。不幸的是,我总是必须为/
定义路由,或者设置MaterialApp的home
属性。因此,如果我将/
设置为空白的容器()
,该容器将位于导航堆栈上,用户可以返回此空白屏幕
我提出的两个选择是:
- 将MaterialApp的home属性设置为HomeScreen或LoginScreen
- 如果用户尚未登录,则在主屏幕的
方法中返回LoginScreenbuild()
你知道什么是处理这种情况的正确方法吗?也许,你可以这样做。假设您有一个带有异步方法的类Auth
isloged
类身份验证{
final FirebaseAuth _FirebaseAuth=FirebaseAuth.instance;
Future isLogged()异步{
试一试{
final FirebaseUser user=wait_firebaseAuth.currentUser();
返回用户!=null;
}捕获(e){
返回false;
}
}
}
您可以使用myApp
构造函数传递initialRoute
,并根据登录状态决定初始路由。然后,您可以将myApp
的实例传递到runApp
:
警告:如果出现错误,请添加WidgetsFlutterBinding.ensureIninitialized()
就在void main()async{
之前。检查此问题
void main()异步{
WidgetsFlutterBinding.ensureInitialized();
最终认证_Auth=Auth();
final bool isLogged=等待_auth.isLogged();
最终MyApp MyApp=MyApp(
initialRoute:IsLoged?'/home':'/',
);
runApp(myApp);
}
之后,您必须修改myApp
类以通过构造函数中的初始路由:
类MyApp扩展了无状态小部件{
最终字符串初始路径;
MyApp({this.initialRoute});
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“动态路线演示”,
initialRoute:initialRoute,
路线:{
“/”:(上下文)=>LoginPage(),
“/home”:(上下文)=>HomePage(),
“/settings”:(上下文)=>SettingsPage(),
},
);
}
}
希望这有帮助。您应该做的是移动到路由/登录(或任何其他您喜欢的登录)并将其设置为initialRoute。在此基础上,您可以修改应用程序的onGenerateInitialRoutes属性,以确保这些路由实际上没有被推送到导航器历史记录中。这样,您就可以覆盖defaultGenerateInitialRoutes,将这些主状态推送到导航器上。请从Flatter的网站查看此代码片段。 也会复制到这里,以防万一将来链接不起作用 defaultGenerateInitialRoutes方法列表 defaultGenerateInitialRoutes(导航器状态导航器,字符串 initialRouteName)将路由名称转换为一组路由对象 这是onGenerateInitialRoutes的默认值,在以下情况下使用 initialRoute不为空 如果此字符串包含任何/个字符,则该字符串在 从字符串开始到结束的那些字符和子字符串 每一个这样的字符依次用作推送的路径 例如,如果使用route/stocks/HOOLI作为初始路由, 然后导航器将在启动时推送以下路由:/, /stocks,/stocks/HOOLI。这在允许 用于维护可预测路线历史记录的应用程序
静态列表defaultGenerateInitialRoutes(导航器状态导航器,字符串initialRouteName){
最终列表结果=[];
if(initialRouteName.startsWith('/')&&initialRouteName.length>1){
initialRouteName=initialRouteName.substring(1);//带前导'/'
断言(Navigator.defaultRouteName=='/');
列出调试路由名称;
断言(){
DebugRouteName=[Navigator.defaultRouteName];
返回true;
}());
add(navigator.\u routename(navigator.defaultRouteName,参数:null,allowNull:true));
最终列表routeParts=initialRouteName.split(“/”);
if(initialRouteName.isNotEmpty){
字符串routeName='';
用于(routeParts中的最终字符串部分){
routeName+='/$part';
断言(){
添加(路由名称);
返回true;
}());
add(navigator.\u routeName(routeName,参数:null,allowNull:true));
}
}
如果(result.last==null){
断言(){
错误。报告错误(
错误详细信息(
例外情况:
'无法导航到初始路由。\n'
'请求的路由名称为:“/$initialRouteName”\n'
'应用程序中没有相应的路由,因此指定的初始路由将为'
'已忽略,将改用“${Navigator.defaultRouteName}”
),
);
返回true;
}());
result.clear();
}
}else if(initialRouteName!=Navigator.defaultRouteName){
//如果initialRouteName不是“/”,那么我们尝试使用allowNull:true获取它,因此如果失败,
//我们回到“/”(不带allowNull:true,见下文)。
static List<Route<dynamic>> defaultGenerateInitialRoutes(NavigatorState navigator, String initialRouteName) {
final List<Route<dynamic>> result = <Route<dynamic>>[];
if (initialRouteName.startsWith('/') && initialRouteName.length > 1) {
initialRouteName = initialRouteName.substring(1); // strip leading '/'
assert(Navigator.defaultRouteName == '/');
List<String> debugRouteNames;
assert(() {
debugRouteNames = <String>[ Navigator.defaultRouteName ];
return true;
}());
result.add(navigator._routeNamed<dynamic>(Navigator.defaultRouteName, arguments: null, allowNull: true));
final List<String> routeParts = initialRouteName.split('/');
if (initialRouteName.isNotEmpty) {
String routeName = '';
for (final String part in routeParts) {
routeName += '/$part';
assert(() {
debugRouteNames.add(routeName);
return true;
}());
result.add(navigator._routeNamed<dynamic>(routeName, arguments: null, allowNull: true));
}
}
if (result.last == null) {
assert(() {
FlutterError.reportError(
FlutterErrorDetails(
exception:
'Could not navigate to initial route.\n'
'The requested route name was: "/$initialRouteName"\n'
'There was no corresponding route in the app, and therefore the initial route specified will be '
'ignored and "${Navigator.defaultRouteName}" will be used instead.'
),
);
return true;
}());
result.clear();
}
} else if (initialRouteName != Navigator.defaultRouteName) {
// If initialRouteName wasn't '/', then we try to get it with allowNull:true, so that if that fails,
// we fall back to '/' (without allowNull:true, see below).
result.add(navigator._routeNamed<dynamic>(initialRouteName, arguments: null, allowNull: true));
}
// Null route might be a result of gap in initialRouteName
//
// For example, routes = ['A', 'A/B/C'], and initialRouteName = 'A/B/C'
// This should result in result = ['A', null,'A/B/C'] where 'A/B' produces
// the null. In this case, we want to filter out the null and return
// result = ['A', 'A/B/C'].
result.removeWhere((Route<dynamic> route) => route == null);
if (result.isEmpty)
result.add(navigator._routeNamed<dynamic>(Navigator.defaultRouteName, arguments: null));
return result;
}