我已经在 Heroku 中部署了一个 Rest API (nodejs),但我想知道如何将我的 flutter 移动应用程序与这些端点连接。我已经寻找解决方案,但我还没有看到任何与此相关的内容。我的 flutter 应用程序正在运行在我的移动设备中。希望任何人都能给出一个好的解决方案。
最佳答案
import 'package:http/http.dart' as http;
首次导入http package请在 pubspec、yaml 中提及 然后 如果要处理jsondata则导入转换包
import 'dart:convert';
现在您将创建 future 的函数 对于获取请求:
Future<void> getGoodRequest1() async{
var url = 'your url';
http.Response response = await http.get(url);
String data = response.body;
print(jsonDecode(data));
}
用于发布请求
Future<void> postGoodRequest2() async{
String url = 'your url';
Map<String, String> headers = {"Content-type": "application/json"};
// if you want to pass json along with it
String json = '{"query": "example", "data": "example"}';
// make POST request
http.Response response = await http.post(url, headers: headers, body: json);
// check the status code for the result
print('posted');
}
更多信息可以引用这里Http flutter
关于flutter - 如何将flutter移动应用程序连接到heroku托管的node js中的rest API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63086026/