App一般都会遇到这样的需求:没有登录则跳转登录页面,登录了则跳转至指定页面。
思路:
1.登录之后存储token。
2.路由拦截,没有登录则跳转至登录页面,登录了则跳转至指定页面。
步骤:
一:首先在pubspec.yaml中添加shared_preferences依赖库
shared_preferences: ^2.0.17
整个pubspec.yaml如下所示:
name: flutter_doctor_app
description: A new Flutter application.
# The following line prevents the package from being accidentally published to
# pub.dev using `pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1
environment:
sdk: ">=2.19.2 <3.0.0"
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS

最低0.47元/天 解锁文章
9743

被折叠的 条评论
为什么被折叠?



