【发布时间】:2021-02-05 00:42:51
【问题描述】:
我在我的 Flutter 项目中创建了一个简单的登录功能,它只需输入电子邮件和密码即可工作。 现在我想从邮递员添加令牌持有者功能,以便即使应用程序已关闭,用户仍然可以登录。 我想问的是如何将令牌承载值放入共享偏好函数中。
这是我的登录代码。
login() async {
final response = await http.post(
"https://api.batulima.com//v1_ships/login_app",
body: {"email": email, "password": password},
);
final data = jsonDecode(response.body);
String status = data['status'];
String message = data['message'];
if (status == "success") {
Navigator.of(context).pushReplacement(PageRouteBuilder(
pageBuilder: (_, __, ___) => new bottomNavBar(),
transitionDuration: Duration(milliseconds: 600),
transitionsBuilder:
(_, Animation<double> animation, __, Widget child) {
return Opacity(
opacity: animation.value,
child: child,
);
}));
print(message);
} else {
print(message);
}
}
这是我的邮递员 json 结构
{
"status": "success",
"data": {
"apikey": "ak5neGVDd3h4M0lVeVF2b2hXWjg3OEZMYUlvcWExTXRqQ21xSmJGWQ==",
"id_user": 49,
"id_role": "8",
"name_role": "Ship Owner",
"email": "afriansyahm86@gmail.com",
"phone": "082258785595",
"saldo": "0",
"photo": "https://batulimee.com/foto_user/avatar.png"
},
"message": "login successfully "
}
我应该添加什么才能从 apikey 检索值?
下面是我在 main.dart 中创建的 getpref。如果为空,则从初始屏幕开始到登录页面。如果 apikey 已保存,则进入底部导航栏页面
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
SharedPreferences prefs = await SharedPreferences.getInstance();
var apikey = prefs.getString('apikey');
print(apikey);
runApp(MaterialApp(
debugShowCheckedModeBanner: false,
home: apikey == null ? splash() : bottomNavBar()));
}
【问题讨论】:
标签: api flutter dart postman bearer-token