【发布时间】:2021-03-31 14:15:45
【问题描述】:
我对@987654321@ 和flutter 有点陌生。
我想使用用户名和密码登录并获取令牌响应。
如何从authorization 的文本字段中获取用户名和密码?
我也不确定验证码是否正确?
imports
class HomePage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _HomePageState();
}
}
class _HomePageState extends State<HomePage> {
final usernameController = TextEditingController();
final passwordController = TextEditingController();
Future<String> authorization() async {
var request = http.MultipartRequest(
'POST', Uri.parse('url'));
request.fields.addAll({
'grant_type': 'info',
'client_id': 'info',
'username': username,
'password': password,
});
http.StreamedResponse response = await request.send();
if (response.statusCode == 200) {
Map<String, dynamic> auth =
jsonDecode(await response.stream.bytesToString());
return auth['access_token'];
} else {
return "";
}
}
void _setText() {
setState(() {
username = usernameController.text;
password = passwordController.text;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Login'),
backgroundColor: Colors.green,
),
body: Column(
children: [
Padding(
child: TextField(
decoration: InputDecoration(labelText: 'username'),
controller: usernameController,
),
),
Padding(
child: TextField(
decoration: InputDecoration(labelText: 'password'),
controller: passwordController,
),
),
RaisedButton(
onPressed: _setText,
child: Text('Login'),
),
],
),
);
}
}
【问题讨论】:
-
先用
usernameController.text.trim()也为passwordController,然后用http包发送请求。
标签: flutter dart authorization token rest