启动画面几秒钟后无法导航到重定向页面

时间:2019-12-04 06:55:16

标签: flutter dart

我正在尝试使用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),
    );

  }
}
终端错误

Terminal

设备错误

enter image description here

2 个答案:

答案 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(),

工作演示

enter image description here

完整代码

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()));
      }