我正在尝试使用sharedpreferences创建带有会话的登录名,并将其与启动画面结合使用,但是效果不佳,请提供帮助。
这是我的代码,
class SplashPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
Future cekSession() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
bool session = (preferences.getBool("session") ?? false);
if (session == true) {
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (context) => MainPage()));
} else {
preferences.setBool("session", true);
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (context) => Login()));
}
}
return Scaffold(
body: new SplashScreen(
seconds: 3,
// I got error around here
navigateAfterSeconds: cekSession(),
title: new Text('Welcome !'),
image: new Image.asset("assets/image.png"),
backgroundColor: Colors.white,
styleTextUnderTheLoader: new TextStyle(),
photoSize: 100.0,
loaderColor: Colors.blue),
);
}
}
终端错误
设备错误
答案 0 :(得分:0)
您可以在下面复制粘贴运行完整代码
您可以在main
中查看会话
navigateAfterSeconds
需要小部件
代码段
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
SharedPreferences preferences = await SharedPreferences.getInstance();
bool session = (preferences.getBool("session") ?? false);
...
navigateAfterSeconds: initScreen == "Login" ? Login() : MainPage(),
工作演示
完整代码
import 'package:flutter/material.dart';
import 'package:splashscreen/splashscreen.dart';
import 'package:shared_preferences/shared_preferences.dart';
String initScreen;
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
SharedPreferences preferences = await SharedPreferences.getInstance();
bool session = (preferences.getBool("session") ?? false);
if (session == true) {
initScreen = "MainPage";
} else {
preferences.setBool("session", true);
initScreen = "Login";
}
runApp(MaterialApp(
home: MyApp(),
));
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: SplashScreen(
seconds: 3,
navigateAfterSeconds: initScreen == "Login" ? Login() : MainPage(),
title: Text(
'Welcome In SplashScreen',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20.0),
),
image: Image.network('https://i.imgur.com/TyCSG9A.png'),
backgroundColor: Colors.white,
styleTextUnderTheLoader: TextStyle(),
photoSize: 100.0,
onClick: () => print("Flutter Egypt"),
loaderColor: Colors.red),
);
}
}
class Login extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Text("Login");
}
}
class MainPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Text("Main Page");
}
}
答案 1 :(得分:0)
由于navigateAfterSeconds
未收到Future
,请尝试传递小部件。取StateFul Widget
。您需要声明布尔值isLoggiedIn
,然后才能在initState
中评估您的checkSession结果,然后再移至另一个屏幕。
bool isLoggiedIn;
@override
void initState() {
super.initState();
checkSession();
}
void checkSession(){
setState{(
isLoggiedIn= your value from sharedpref
)}
}
然后代表isLoggiedIn
值,您需要像这样传递小部件
if (session == true) {
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (context) => MainPage()));
} else {
preferences.setBool("session", true);
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (context) => Login()));
}