How to integrate flutter app with node.js

不羁的心 提交于 2020-12-01 12:09:37

问题


I am trying to develop a flutter app which is integrated with node.js . But I don't know how to implement it anyone can help me with this


回答1:


If you create a RESTful api server, you can write it in any language you want, and your Flutter app can use it to retrieve and post data. So simply create a Node.js server and make requests to it via Flutter over http.

Here is an example of how to create an HTTP client in Flutter and use it to connect to a server endpoint and get a response:

//Import dart library
import 'dart:io';

_getUserApi() async {
  var httpClient = new HttpClient();
  var uri = new Uri.https('yourserverurl.com', '/your/endpoint/whatever');
  var request = await httpClient.getUrl(uri);
  var response = await request.close();
  var responseBody = await response.transform(UTF8.decoder).join();
  return responseBody;
} 

If you configure your server to return data in JSON format (as is most common with Node.js), you will need to parse the JSON response and convert it to a typed form to be used by your application. You can do this either by writing the constructors yourself, or by using a Dart library like json_serializable or built_value.

Here is a very good article about using each of these methods.

Once you have deserialized your JSON, you can use the data in your Flutter widget.



来源:https://stackoverflow.com/questions/49914136/how-to-integrate-flutter-app-with-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!