Flutter应用移除后台后,已登录用户无法返回对应页面问题排查
问题排查与修复方案
核心问题分析
你的代码存在三个关键问题,导致APP重启后无法回到用户上次的页面:
- 强制重置登录状态:
check方法开头直接执行sharedPref.setBool(OnboardState.KEYLOGIN, false),每次启动都会把登录状态强制改为false,永远触发未登录分支跳转注册页。 - 错误获取Provider实例:
whereToGo里手动新建LogicContainer,这不是Widget树中由Provider管理的实例;且APP重启后Provider的内存状态会丢失,无法拿到用户之前选择的容器索引。 - 未持久化容器索引:没有将用户选择的
tappedContainerIndex保存到SharedPreferences,重启后无法恢复该值。
修复后的代码
import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:go_router/go_router.dart'; import 'package:google_fonts/google_fonts.dart'; import 'package:provider/provider.dart'; import 'package:shared_preferences/shared_preferences.dart'; import '../Provider/LogicForTappedContainer.dart'; class Onboard extends StatefulWidget { const Onboard({super.key}); @override State<Onboard> createState() => OnboardState(); } class OnboardState extends State<Onboard> { // SharedPreferences 键名 static const KEYLOGIN = "login"; static const KEY_CONTAINER_INDEX = "container_index"; double opacity1 = 0.0; double opacity2 = 0.0; // 检查登录状态与容器索引 Future<void> check() async { var sharedPref = await SharedPreferences.getInstance(); // 直接获取保存的登录状态,不再强制重置 bool? isLoggedIn = sharedPref.getBool(KEYLOGIN); // 从SharedPreferences获取保存的容器索引,默认值设为0 int savedIndex = sharedPref.getInt(KEY_CONTAINER_INDEX) ?? 0; print('isLoggedIn: $isLoggedIn'); print('saved container index: $savedIndex'); if (isLoggedIn == true) { // 根据保存的索引跳转对应页面 if (savedIndex == 0) { context.go('/userMainScreen'); } else if (savedIndex == 1) { context.go('/panditchat'); } } else { context.go('/signup'); } } @override void initState() { whereToGo(); Future.delayed(const Duration(milliseconds: 500), () { setState(() { opacity1 = 1.0; }); }); Future.delayed(const Duration(milliseconds: 1000), () { setState(() { opacity2 = 1.0; }); }); super.initState(); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.white, body: Consumer<LogicContainer>(builder: (context, provider, child) { return Stack(children: [ Image.asset( "assets/logos/Temple.jpg", fit: BoxFit.fitHeight, height: double.infinity, ), Positioned( top: MediaQuery.of(context).size.height * 0.83, left: 80.w, child: Column( children: [ AnimatedOpacity( opacity: opacity1, duration: const Duration(milliseconds: 500), child: Text( " Welcome", style: GoogleFonts.signikaNegative( fontSize: 40, color: Colors.white, fontWeight: FontWeight.bold, ), ), ), const SizedBox(height: 5), AnimatedOpacity( opacity: opacity2, duration: const Duration(milliseconds: 500), child: Text( " Worshipper", style: GoogleFonts.signikaNegative( fontSize: 40, color: Colors.white, fontWeight: FontWeight.bold, ), ), ), ], ), ), ]); })); } void whereToGo() { Timer(const Duration(seconds: 3), () async { // 直接调用check方法,不需要手动创建Provider实例 await check(); }); } }
额外补充步骤
在用户登录成功的代码位置,除了设置KEYLOGIN为true,还要保存当前选择的容器索引:
// 登录成功时执行 var sharedPref = await SharedPreferences.getInstance(); sharedPref.setBool(OnboardState.KEYLOGIN, true); // 保存用户选择的容器索引,这里假设provider.tappedContainerIndex是当前选中的索引 sharedPref.setInt(OnboardState.KEY_CONTAINER_INDEX, provider.tappedContainerIndex);
内容的提问来源于stack exchange,提问作者Sachin Kumar
相关产品推荐
相关产品推荐

