10

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

2

1 Answer 1

15

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.

Sign up to request clarification or add additional context in comments.

8 Comments

If I would like to connect to a server running locally on my computer what should be the uri??I Tried my ipv4 it says FormatException, address should be alphabetic
It's just the uri of your rest endpoint defined by whatever you have programmed it to be in your api server. The server url part will be the hostname. If the server is running on your local machine, your server url will be 127.0.0.1, or localhost. And the uri is dependent on your server programming. For instance, I might have a RESTful api server on my local machine programmed to return a JSON list of clients for whenever I access http://[serverurl]/api/v1/clients. In this case, 127.0.0.1 is my server url and /api/v1/clients is my endpoint. Does that help?
I tried and I get a Socket EXCEPTION this is my code var httpClient = new HttpClient(); var request = await httpClient.get('127.0.0.1', 3000, "/api"); var response = await request.close(); error I get :SocketException: OS Error: Connection refused, errno = 111, address = 127.0.0.1, port = 39496
Aha, I see the problem. Apparently in Flutter, 127.0.0.1 is routed to your phone, and to get localhost, you need to use 10.0.2.2. Please try this for the server url: 10.0.2.2:<hostport>
I tried the specified url it just prints Instance of '_HttpClientResponse' when I do localhost:3000/api in my browser I get the json response from my mongo database but I am unable to fetch the same in the app can we continue this discussion somewhere else
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.