Flutter 从颤振前端注册到渡槽后端

Flutter 从颤振前端注册到渡槽后端,flutter,dart,aqueduct,Flutter,Dart,Aqueduct,我在从颤振前端注册到渡槽后端时有点困难 这是我前端的代码: Future<void> signUp(String email, String password) async { final body = "username:$email,password:$password"; //<- return request entity could not be decoded //final body = {"username": email, "passwor

我在从颤振前端注册到渡槽后端时有点困难

这是我前端的代码:

  Future<void> signUp(String email, String password) async {
    final body = "username:$email,password:$password"; //<- return request entity could not be decoded
    //final body = {"username": email, "password": password}; //<- return bad state: Cannot set the body fields of Request with content-type "application/json"

    try {
      final http.Response response = await http.post(
          "http://localhost:8888/register",
          headers: {"Content-Type": "application/json"},
          body: body);
      final jsonResponse = json.decode(response.body);
      if (jsonResponse["error"] != null) {
        throw HttpException(jsonResponse["error"]);
      }
    } catch (error) {
      throw error;
    }
  }
未来注册(字符串电子邮件、字符串密码)异步{

final body=“username:$email,password:$password”;//下面是一个从颤振客户端连接到导水管服务器的示例。(但这并不是真正的服务器问题,因为客户端和服务器彼此独立。)

以下是注册的一个示例:

void _register(String email, String password) async {
  Map<String, String> headers = {"Content-type": "application/json"};
  final jsonString = '{"username":"$email", "password":"$password"}';
  Response response = await post(YOUR_URL_HERE, headers: headers, body: jsonString);
  print('${response.statusCode} ${response.body}');
}
void\u寄存器(字符串电子邮件、字符串密码)异步{
映射头={“内容类型”:“应用程序/json”};
final jsonString='{“用户名”:“$email”,“密码”:“$password”}';
Response-Response=wait-post(您的URL在这里,headers:headers,body:jsonString);
打印(“${response.statusCode}${response.body}”);
}
在您的示例中,您没有正确编码JSON

这里是另一个登录的例子。这个类是我提到的一个视图模型架构

导入'dart:convert';
进口“包装:颤振/基础.dart”;
将“package:http/http.dart”导入为http;
类LoginViewModel扩展了ChangeNotifier{
字符串_标记=“”;
boolu isLoggedIn=false;
bool get isLoggedIn=>\u isLoggedIn;
字符串get token=>\u token;
未来onLoginPressed(字符串用户名、字符串密码)异步{
if(username.isEmpty | | password.isEmpty){
返回;
}
_isLoggedIn=等待登录(用户名、密码);
notifyListeners();
}
未来登录(字符串用户名、字符串密码)异步{
var clientID='com.example.app';
var clientSecret='';
var body='username=$username&password=$password&grant_type=password';
var clientCredentials=Base64Encoder().convert(“$clientID:$clientSecret.codeUnits”);
映射头={
“内容类型”:“应用程序/x-www-form-urlencoded”,
“授权”:“基本$clientCredentials”
};
var response=wait http.post(您的URL在这里,headers:headers,body:body);
最终响应主体=响应主体;
如果(response.statusCode!=200){
返回false;
}
final map=json.decode(responseBody);
_令牌=映射['access_token'];
返回true;
}
}

我确实知道登录。我问的是注册……创建一个新用户。对不起,我也意识到了这一点。@delmin,我更新了我的答案,但我看不出你做错了什么。嗯……我也是。我的代码基本上与你的代码相同,但我仍然会遇到这些错误。我们可以用电子邮件格式保存用户名吗?这是唯一的区别在我的code@delmin是的,我想是的
import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;

class LoginViewModel extends ChangeNotifier {

  String _token = '';
  bool _isLoggedIn = false;
  bool get isLoggedIn => _isLoggedIn;
  String get token => _token;

  Future onLoginPressed(String username, String password) async {
    if (username.isEmpty || password.isEmpty) {
      return;
    }
    _isLoggedIn = await _login(username, password);
    notifyListeners();
  }

  Future<bool> _login(String username, String password) async {
    var clientID = 'com.example.app';
    var clientSecret = '';
    var body = 'username=$username&password=$password&grant_type=password';
    var clientCredentials = Base64Encoder().convert('$clientID:$clientSecret'.codeUnits);

    Map<String, String> headers = {
      'Content-type': 'application/x-www-form-urlencoded',
      'authorization': 'Basic $clientCredentials'
    };
    var response = await http.post(YOUR_URL_HERE, headers: headers, body: body);
    final responseBody = response.body;
    if (response.statusCode != 200) {
      return false;
    }

    final map = json.decode(responseBody);
    _token = map['access_token'];
    return true;
  }
}