,

Using Node JS for REST API to fetch data and create a backend for Flutter | Step-by-step guide for fetching data using API in Flutter with Node JS Backend

Posted by

Node JS GET REST API | Flutter Node JS Backend | Fetch Data using API | Flutter Tutorial

body {
font-family: Arial, sans-serif;
padding: 20px;
background-color: #f2f2f2;
}

h1, h2, p {
margin-bottom: 20px;
}

code {
background-color: #f2f2f2;
padding: 5px;
}

.code-block {
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

Node JS GET REST API | Flutter Node JS Backend | Fetch Data using API | Flutter Tutorial

Introduction

In this tutorial, we will learn how to create a REST API using Node.js and how to fetch data from this API in a Flutter app. We will create a simple Node.js backend that exposes a GET endpoint and then build a Flutter app that consumes this API to fetch and display data.

Creating a Node.js REST API

First, we need to create a Node.js backend that will serve as our API. We can use the express framework to create a simple REST API. Make sure to have Node.js and npm installed on your system.


const express = require('express');
const app = express();

app.get('/api/data', (req, res) => {
const data = {
id: 1,
name: 'John Doe',
email: 'john.doe@example.com'
};
res.json(data);
});

app.listen(3000, () => {
console.log('Server is running on port 3000');
});

Fetching Data in a Flutter App

Now that we have our Node.js backend set up, we can move on to building a Flutter app that will consume the API and display the fetched data. We can use the http package in Flutter to make network requests to our Node.js server.


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

Future<Map> fetchData() async {
final response = await http.get('http://localhost:3000/api/data');
if (response.statusCode == 200) {
return json.decode(response.body);
} else {
throw Exception('Failed to load data');
}
}

Conclusion

In this tutorial, we learned how to create a simple Node.js backend that serves a REST API and how to fetch data from this API in a Flutter app. We used the express framework to create the backend and the http package in Flutter to make network requests. This is just a basic example, but it should give you a good starting point for building more complex applications.

0 0 votes
Article Rating
9 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@fatiqhussnain8399
6 months ago

well explained brother keep it up.❤❤

@iamshaheenmirza
6 months ago

flutter: {pname: 333333333, pprice: 33333333, pdesc: 33333333}
flutter: ClientException with SocketException: The remote computer refused the network connection.
flutter: (OS Error: The remote computer refused the network connection.
flutter: , errno = 1225), address = localhost, port = 64025, uri=http://localhost/api/add_product

i get this problem, the request isn't posted in my humble opinion. could ya plz tell ?

@iamshyley
6 months ago

I if run the code after doing everything, i get an error of "type null is not a subtype of a type bool"

@zoey11th64
6 months ago

If you really want to learn using NodeJS with Flutter you should avoid this channel.

@Sachinkumar-qf6ws
6 months ago

my fetch data screen keeps on loading continously but fetch api is working fine in thunderclient what should i do now ?

@Jose-jq9ps
6 months ago

🔥🔥

@aitchikh276
6 months ago

Plz can i have source code

@sammyjokes9613
6 months ago

Please continue this tutorial.. It's splendid

@ManuMoneyMind
6 months ago

bhai is code ka source code de do pta nhi kha error aa rha hai (iska nhi ) >>>> iska https://www.youtube.com/watch?v=AOpsCgjOIW8 Flutter fetch data from API | Flutter HTTP Get Request | Flutter API call | Flutter Rest API iska source code de do please