检索不记名令牌邮递员扑
retrieve bearer token postman to flutter
我在我的 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 已经保存,它会转到 bottomnavbar 页面
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()));
}
您可以复制粘贴运行下面的完整代码
要在 JSON 中检索 apikey 的值,您可以执行 data['data']['apikey']
代码片段
String apiKey = data['data']['apikey'];
print("apiKey $apiKey");
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString('apiKey', apiKey);
String getedApiKey = await prefs.getString('apiKey');
print(getedApiKey);
输出
I/flutter (22942): apiKey ak5neGVDd3h4M0lVeVF2b2hXWjg3OEZMYUlvcWExTXRqQ21xSmJGWQ==
I/flutter (22942): ak5neGVDd3h4M0lVeVF2b2hXWjg3OEZMYUlvcWExTXRqQ21xSmJGWQ==
I/flutter (22942): login successfully
完整代码
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
login() async {
/*final response = await http.post(
"https://api.batulima.com//v1_ships/login_app",
body: {"email": email, "password": password},
);*/
String jsonString = '''
{
"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 "
}
''';
final response = http.Response(jsonString, 200);
final data = jsonDecode(response.body);
String status = data['status'];
String message = data['message'];
String apiKey = data['data']['apikey'];
print("apiKey $apiKey");
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString('apiKey', apiKey);
String getedApiKey = await prefs.getString('apiKey');
print(getedApiKey);
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);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: login,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
我在我的 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 已经保存,它会转到 bottomnavbar 页面
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()));
}
您可以复制粘贴运行下面的完整代码
要在 JSON 中检索 apikey 的值,您可以执行 data['data']['apikey']
代码片段
String apiKey = data['data']['apikey'];
print("apiKey $apiKey");
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString('apiKey', apiKey);
String getedApiKey = await prefs.getString('apiKey');
print(getedApiKey);
输出
I/flutter (22942): apiKey ak5neGVDd3h4M0lVeVF2b2hXWjg3OEZMYUlvcWExTXRqQ21xSmJGWQ==
I/flutter (22942): ak5neGVDd3h4M0lVeVF2b2hXWjg3OEZMYUlvcWExTXRqQ21xSmJGWQ==
I/flutter (22942): login successfully
完整代码
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
login() async {
/*final response = await http.post(
"https://api.batulima.com//v1_ships/login_app",
body: {"email": email, "password": password},
);*/
String jsonString = '''
{
"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 "
}
''';
final response = http.Response(jsonString, 200);
final data = jsonDecode(response.body);
String status = data['status'];
String message = data['message'];
String apiKey = data['data']['apikey'];
print("apiKey $apiKey");
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString('apiKey', apiKey);
String getedApiKey = await prefs.getString('apiKey');
print(getedApiKey);
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);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: login,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}