如何将flutter移动应用程序连接到heroku中托管的node js中的rest API?

时间:2020-07-25 08:16:16

标签: flutter heroku

我已经在Heroku中部署了一个Rest API(nodejs),但我想知道如何将Flutter移动应用程序与这些端点连接起来。我一直在寻找解决方案,但对此一无所知。在我的移动设备上。希望任何人都能提供一个好的解决方案。

1 个答案:

答案 0 :(得分:1)

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

首次导入http package确实在pubspec,yaml中提到了这一点 然后 如果要处理jsondata,请导入转换包

import 'dart:convert';

现在您将创建将来的功能 获取请求:

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