英文:
Flutter: do not redirect after authenticating
问题
我在Flutter中做了一个简单的检查,在认证后应该重定向到HomePage()。
我调试了一下,在认证后,authController.isAuth = true,但它没有引用到HomePage()。
class AuthOrHomePage extends StatelessWidget {
const AuthOrHomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
AuthController authController = Provider.of(context);
return FutureBuilder(
future: authController.tryAutoLogin(),
builder: (ctx, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
} else if (snapshot.error != null) {
return const Center(
child: Text('Ocorreu um erro!'),
);
} else {
if (authController.isAuth) {
return const HomePage();
} else {
return const InitPage();
}
}
},
);
}
}
有什么想法吗?
英文:
I'm doing a simple check in Flutter, where after authenticating, it should redirect to HomePage().
I debugged and after authentication authController.isAuth = true but it doesn't refer to HomePage().
class AuthOrHomePage extends StatelessWidget {
const AuthOrHomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
AuthController authController = Provider.of(context);
return FutureBuilder(
future: authController.tryAutoLogin(),
builder: (ctx, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
} else if (snapshot.error != null) {
return const Center(
child: Text('Ocorreu um erro!'),
);
} else {
if (authController.isAuth) {
return const HomePage();
} else {
return const InitPage();
}
}
},
);
}
}
Any idea what it could be?
答案1
得分: 0
在Flutter中,如果你想从一个屏幕导航到另一个屏幕,可以使用Navigator
。
class AuthOrHomePage extends StatelessWidget {
const AuthOrHomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
AuthController authController = Provider.of(context);
return FutureBuilder(
future: authController.tryAutoLogin(),
builder: (ctx, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
} else if (snapshot.error != null) {
return const Center(
child: Text('Ocorreu um erro!'),
);
} else {
if (authController.isAuth) {
Navigator.of(context).push(MaterialPageRoute(builder: (_) => const HomePage()));
} else {
Navigator.of(context).push(MaterialPageRoute(builder: (_) => const InitPage()));
}
return SizedBox();
}
},
);
}
}
英文:
In flutter, if you want to navigate from one screen to another you can use Navigator
.
class AuthOrHomePage extends StatelessWidget {
const AuthOrHomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
AuthController authController = Provider.of(context);
return FutureBuilder(
future: authController.tryAutoLogin(),
builder: (ctx, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
} else if (snapshot.error != null) {
return const Center(
child: Text('Ocorreu um erro!'),
);
} else {
if (authController.isAuth) {
Navigator.of(context).push(MaterialPageRoute(builder: (_) => const HomePage()));
} else {
Navigator.of(context).push(MaterialPageRoute(builder: (_) => const InitPage()));
}
return SizedBox();
}
},
);
}
}
答案2
得分: 0
尝试这种方式,
导入 'package:flutter/material.dart';
导入 'package:provider/provider.dart';
类 AuthOrHomePage 扩展 StatelessWidget {
const AuthOrHomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
AuthController authController = Provider.of(context);
返回 FutureBuilder(
未来: authController.tryAutoLogin(),
建造者: (ctx, 快照) {
如果 (快照.connectionState == ConnectionState.waiting) {
返回 const 居中(child: CircularProgressIndicator());
} else if (快照.error != null) {
返回 const 居中(
孩子: Text('Ocorreu um erro!'),
);
} else {
如果 (authController.isAuth) {
// 导航到 HomePage
WidgetsBinding.instance!.addPostFrameCallback((_) {
导航推Replacement(
上下文,
MaterialPageRoute(builder: (context) => const HomePage()),
);
});
} else {
返回 const InitPage();
}
}
返回 const SizedBox.shrink();
},
);
}
}
英文:
Try this way,
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class AuthOrHomePage extends StatelessWidget {
const AuthOrHomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
AuthController authController = Provider.of(context);
return FutureBuilder(
future: authController.tryAutoLogin(),
builder: (ctx, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
} else if (snapshot.error != null) {
return const Center(
child: Text('Ocorreu um erro!'),
);
} else {
if (authController.isAuth) {
// Navigate to the HomePage
WidgetsBinding.instance!.addPostFrameCallback((_) {
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => const HomePage()),
);
});
} else {
return const InitPage();
}
}
return const SizedBox.shrink();
},
);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论