[英]How to connect a flutter mobile app to a rest API in node js which is hosted in heroku?
I have deployed a rest API (nodejs) in Heroku but i want to know how to connect my flutter mobile app with these endpoints.I have looked for solution but i haven't seen anything regarding this.my flutter app is running in my mobile device.Hope any one can give a good solution. I have deployed a rest API (nodejs) in Heroku but i want to know how to connect my flutter mobile app with these endpoints.I have looked for solution but i haven't seen anything regarding this.my flutter app is running in my mobile设备。希望任何人都可以提供一个好的解决方案。
import 'package:http/http.dart' as http;
First import http package do mention that in pubspec,yaml then Import convert package if you want to deal with jsondata First import http package do mention that in pubspec,yaml then Import convert package if you want to deal with jsondata
import 'dart:convert';
Now you will be creating future functions for get request:现在您将为获取请求创建未来的函数:
Future<void> getGoodRequest1() async{
var url = 'your url';
http.Response response = await http.get(url);
String data = response.body;
print(jsonDecode(data));
}
for post request发帖请求
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');
}
For more you can refer here Http flutter有关更多信息,您可以参考这里Http flutter
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.