如何获得带有用户名和密码文本字段的令牌响应? dart/flutter

How can I get token response with username and password textfields? dart/flutter

我对 rest apiflutter 有点陌生。 我想使用用户名和密码登录并获得令牌响应。 如何从 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'),
          ),
        ],
      ),
    );
  }
}

你应该稍微改变一下你的方法,为你的函数 authorization 提供参数,它将调用你的 API :

  // Add parameters
  Future<String> authorization(username,password) async {
      ...
  }

  void _setText() {
    // This will call your API with the form parameters
    result = await authorization(usernameController.text, passwordController.text);
    // result should have your token (= the response from the API)
  }
  • 由于您使用的是 TextEditingControllers,因此您不必使用字符串来保存用户名和密码的值。

像这样更改它们:

  TextEditingController usernameController = TextEditingController();
  TextEditingController  passwordController = TextEditingController();

然后,将您的 API post 更改为:

Future<String> authorization() async {
    var request = http.MultipartRequest(
        'POST', Uri.parse('url'));
    request.fields.addAll({
      'grant_type': 'info',
      'client_id': 'info',
      'username': usernameController.text, //notice you have to use .text
      'password': passwordController.text, //notice you have to use .text
    });
  • 你的onPressed像这样:

    RaisedButton(
    onPressed: () async {
    String token = await authorization();
    print(token); //This will be your token.
    },
    child: Text('Login'),),
    
  • 你不再需要这个了:

    //void _setText() {
    //setState(() {
    //username = usernameController.text;
    // password = passwordController.text;
    //});
    //}