如何使用 Dart http 向后端添加数据

How to add data to backend with Dart http

我正在尝试使用 http 包向 Dart 后端添加一些数据。我设法用 post 方法添加数据,但它只是替换了所有现有数据。我认为这不是它应该如何工作的。我使用 typicode json-server 作为虚拟后端。服务器行为是否正确?我做错了什么?

我当前的代码

import 'package:http/http.dart' as http;
import 'dart:convert';
...
String url = "localhost:3000/people/results";
var body = jsonEncode(
  {
    "results": [
      { "first": "John", "last": "Doe" }
    ]
  }
);
await http.post(url,
  body: body
);

预期的后端结果

"people": {
  "results": [
    ...some previous data here...
    { "first": "John", "last": "Doe" }
  ]
}

结果

"people": {
  "results": [
    // all previous data missing
    { "first": "John", "last": "Doe" }
  ]
}

这似乎是一个 typicode 服务器问题,因为它是一个虚拟服务器,它可能会丢弃旧数据,您需要创建自己的服务器来永久保存它。