Flutter - Accessing REST API
Flutter - Accessing REST API
Flutter - Accessing REST API
Flutter provides http package to consume HTTP resources. http is a Future-based library and uses
await and async features. It provides many high level methods and simplifies the development of
REST based mobile applications.
Basic Concepts
http package provides a high level class and http to do web requests.
http methods accept a url, and additional information through Dart Map (post data, additional
headers, etc.,). It requests the server and collects the response back in async/await pattern.
For example, the below code reads the data from the specified url and print it in the console.
print(await http.read('https://flutter.dev/'));
read − Request the specified url through GET method and return back the response as
Future<String>
get − Request the specified url through GET method and return back the response as
Future<Response>. Response is a class holding the response information.
post − Request the specified url through POST method by posting the supplied data and return
back the response as Future<Response>
put − Request the specified url through PUT method and return back the response as Future
<Response>
head − Request the specified url through HEAD method and return back the response as
Future<Response>
delete − Request the specified url through DELETE method and return back the response as
Future<Response>
http also provides a more standard HTTP client class, client. client supports persistent connection. It
will be useful when a lot of request to be made to a particular server. It needs to be closed properly
using close method. Otherwise, it is similar to http class. The sample code is as follows −
https://www.tutorialspoint.com/flutter/flutter_accessing_rest_api.htm 1/13
3/20/23, 3:11 AM Flutter - Accessing REST API
Replace the default startup code (main.dart) with our product_nav_app code.
Copy the assets folder from product_nav_app to product_rest_app and add assets inside the
pubspec.yaml file.
flutter:
assets:
- assets/appimages/floppy.png
- assets/appimages/iphone.png
- assets/appimages/laptop.png
- assets/appimages/pendrive.png
- assets/appimages/pixel.png
- assets/appimages/tablet.png
dependencies:
http: ^0.12.0+2
Here, we will use the latest version of the http package. Android studio will send a package
alert that the pubspec.yaml is updated.
Click Get dependencies option. Android studio will get the package from Internet and properly
configure it for the application.
https://www.tutorialspoint.com/flutter/flutter_accessing_rest_api.htm 2/13
3/20/23, 3:11 AM Flutter - Accessing REST API
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
Create a new JSON file, products.json with product information as shown below −
[
{
"name": "iPhone",
"description": "iPhone is the stylist phone ever",
"price": 1000,
"image": "iphone.png"
},
{
"name": "Pixel",
"description": "Pixel is the most feature phone ever",
"price": 800,
"image": "pixel.png"
},
{
"name": "Laptop",
"description": "Laptop is most productive development tool",
"price": 2000,
"image": "laptop.png"
},
{
"name": "Tablet",
"description": "Tablet is the most useful device ever for meeting",
"price": 1500,
"image": "tablet.png"
},
{
"name": "Pendrive",
"description": "Pendrive is useful storage medium",
"price": 100,
"image": "pendrive.png"
},
{
"name": "Floppy Drive",
https://www.tutorialspoint.com/flutter/flutter_accessing_rest_api.htm 3/13
3/20/23, 3:11 AM Flutter - Accessing REST API
Create a new folder, JSONWebServer and place the JSON file, products.json.
Run any web server with JSONWebServer as its root directory and get its web path. For
example, http://192.168.184.1:8000/products.json. We can use any web server like apache,
nginx etc.,
The easiest way is to install node based http-server application. Follow the steps given below to
install and run http- server application
Go to JSONWebServer folder.
cd /path/to/JSONWebServer
http-server . -p 8000
Create a new file, Product.dart in the lib folder and move the Product class into it.
Write a factory constructor in the Product class, Product.fromMap to convert mapped data Map
into the Product object. Normally, JSON file will be converted into Dart Map object and then,
converted into relevant object (Product).
https://www.tutorialspoint.com/flutter/flutter_accessing_rest_api.htm 4/13
3/20/23, 3:11 AM Flutter - Accessing REST API
class Product {
final String name;
final String description;
final int price;
final String image;
Write two methods − parseProducts and fetchProducts - in the main class to fetch and load the
product information from web server into the List<Product> object.
https://www.tutorialspoint.com/flutter/flutter_accessing_rest_api.htm 5/13
3/20/23, 3:11 AM Flutter - Accessing REST API
} else {
throw Exception('Unable to fetch products from the REST API');
}
}
Future is used to lazy load the product information. Lazy loading is a concept to defer the
execution of the code until it is necessary.
In MyApp class, add new member variable, products of type Future<Product> and include it
in constructor.
In MyHomePage class, add new member variable products of type Future<Product> and
include it in constructor. Also, remove items variable and its relevant method, getProducts
method call. Placing the products variable in constructor. It will allow to fetch the products from
Internet only once when the application is first started.
Change the home option (MyHomePage) in the build method of MyApp widget to
accommodate above changes −
https://www.tutorialspoint.com/flutter/flutter_accessing_rest_api.htm 6/13
3/20/23, 3:11 AM Flutter - Accessing REST API
Create a new widget, ProductBoxList to build the product list in the home page.
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return GestureDetector(
child: ProductBox(item: items[index]),
onTap: () {
Navigator.push(
context, MaterialPageRoute(
builder: (context) =gt; ProductPage(item: items[index]),
),
);
},
);
},
);
}
}
Note that we used the same concept used in Navigation application to list the product except it is
designed as a separate widget by passing products (object) of type List<Product>.
Finally, modify the MyHomePage widget’s build method to get the product information using
Future option instead of normal method call.
https://www.tutorialspoint.com/flutter/flutter_accessing_rest_api.htm 7/13
3/20/23, 3:11 AM Flutter - Accessing REST API
if (snapshot.hasError) print(snapshot.error);
return snapshot.hasData ? ProductBoxList(items: snapshot.data)
Here note that we used FutureBuilder widget to render the widget. FutureBuilder will try to fetch
the data from it’s future property (of type Future<List<Product>>). If future property returns
data, it will render the widget using ProductBoxList, otherwise throws an error.
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'Product.dart';
https://www.tutorialspoint.com/flutter/flutter_accessing_rest_api.htm 8/13
3/20/23, 3:11 AM Flutter - Accessing REST API
@override
Widget build(BuildContext context) {
return ListView.builder(
https://www.tutorialspoint.com/flutter/flutter_accessing_rest_api.htm 9/13
3/20/23, 3:11 AM Flutter - Accessing REST API
itemCount: items.length,
itemBuilder: (context, index) {
return GestureDetector(
child: ProductBox(item: items[index]),
onTap: () {
Navigator.push(
context, MaterialPageRoute(
builder: (context) => ProductPage(item: items[index]),
),
);
},
);
},
);
}
}
class ProductPage extends StatelessWidget {
ProductPage({Key key, this.item}) : super(key: key);
final Product item;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(this.item.name),),
body: Center(
child: Container(
padding: EdgeInsets.all(0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Image.asset("assets/appimages/" + this.item.image),
Expanded(
child: Container(
padding: EdgeInsets.all(5),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Text(this.item.name, style:
TextStyle(fontWeight: FontWeight.bold)),
Text(this.item.description),
Text("Price: " + this.item.price.toString()),
RatingBox(),
],
https://www.tutorialspoint.com/flutter/flutter_accessing_rest_api.htm 10/13
3/20/23, 3:11 AM Flutter - Accessing REST API
)
)
)
]
),
),
),
);
}
}
class RatingBox extends StatefulWidget {
@override
_RatingBoxState createState() =>_RatingBoxState();
}
class _RatingBoxState extends State<RatingBox> {
int _rating = 0;
void _setRatingAsOne() {
setState(() {
_rating = 1;
});
}
void _setRatingAsTwo() {
setState(() {
_rating = 2;
});
}
void _setRatingAsThree() {
setState(() {
_rating = 3;
});
}
Widget build(BuildContext context) {
double _size = 20;
print(_rating);
return Row(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Container(
padding: EdgeInsets.all(0),
child: IconButton(
https://www.tutorialspoint.com/flutter/flutter_accessing_rest_api.htm 11/13
3/20/23, 3:11 AM Flutter - Accessing REST API
icon: (
_rating >= 1
? Icon(Icons.star, ize: _size,)
: Icon(Icons.star_border, size: _size,)
),
color: Colors.red[500], onPressed: _setRatingAsOne, iconSize: _size
),
),
Container(
padding: EdgeInsets.all(0),
child: IconButton(
icon: (
_rating >= 2
? Icon(Icons.star, size: _size,)
: Icon(Icons.star_border, size: _size, )
),
color: Colors.red[500],
onPressed: _setRatingAsTwo,
iconSize: _size,
),
),
Container(
padding: EdgeInsets.all(0),
child: IconButton(
icon: (
_rating >= 3 ?
Icon(Icons.star, size: _size,)
: Icon(Icons.star_border, size: _size,)
),
color: Colors.red[500],
onPressed: _setRatingAsThree,
iconSize: _size,
),
),
],
);
}
}
class ProductBox extends StatelessWidget {
ProductBox({Key key, this.item}) : super(key: key);
final Product item;
https://www.tutorialspoint.com/flutter/flutter_accessing_rest_api.htm 12/13
3/20/23, 3:11 AM Flutter - Accessing REST API
return Container(
padding: EdgeInsets.all(2), height: 140,
child: Card(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Image.asset("assets/appimages/" + this.item.image),
Expanded(
child: Container(
padding: EdgeInsets.all(5),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Text(this.item.name, style:TextStyle(fontWeight: FontWe
Text(this.item.description),
Text("Price: " + this.item.price.toString()),
RatingBox(),
],
)
)
)
]
),
)
);
}
}
Finally run the application to see the result. It will be same as our Navigation example except the
data is from Internet instead of local, static data entered while coding the application.
https://www.tutorialspoint.com/flutter/flutter_accessing_rest_api.htm 13/13