How to Get API Request with Detail In Flutter?
Development teams using “How to Get API Request with Detail In Flutter?” in client or internal work can use the operational overview to document project effort and hand-offs, then assess the record against delivered functionality, tests and agreed outcomes.
Validate current syntax, security guidance and supported versions through GitHub before using the example in production.
Hi friends,
In this post, we will learn how to Flutter API Get Request with details Example. i explained simply step by step how to use get api Request with detailsin flutter. Here you will learn Simple How to api request with details flutter. This tutorial will give you simple example How to get Request api example in flutter.
I will give you simple Example of api get request example flutter.
So let's see bellow example:
Step 1 : Create Flutter Project
Follow along with the setup, you will be creating an Flutter app.
- $flutter create flutter_get_request_example
Navigate to the project directory:
- $cd flutter_get_request_example
Open pubspec.yaml in your code editor and add below plugin:
dependencies:
flutter:
sdk: flutter
http: ^0.12.0+2
Step 2 : Handl GET Request
Create a http_service.dart file in the lib directory
import 'dart:convert';
// ignore: import_of_legacy_library_into_null_safe
import 'package:http/http.dart';
import 'post_model.dart';
class HttpService {
final String postsURL = "https://jsonplaceholder.typicode.com/posts";
Future> getPosts() async {
Response res = await get(postsURL);
if (res.statusCode == 200) {
List body = jsonDecode(res.body);
List posts = body
.map(
(dynamic item) => Post.fromJson(item),
)
.toList();
return posts;
} else {
throw "Sorry! Unable to get post.";
}
}
}
Then, create a post_model.dart file in the lib directory.
import 'package:flutter/foundation.dart';
class Post {
final int userId;
final int id;
final String title;
final String body;
Post({
required this.userId,
required this.id,
required this.title,
required this.body,
});
factory Post.fromJson(Map json) {
return Post(
userId: json['userId'] as int,
id: json['id'] as int,
title: json['title'] as String,
body: json['body'] as String,
);
}
}
Step 3 : Display Post Data
Next, create a post_detail.dart file in the lib directory
import 'package:flutter/material.dart';
import 'post_model.dart';
class PostDetail extends StatelessWidget {
final Post post;
PostDetail({@required this.post});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(post.title),
),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Column(
children: [
Card(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
ListTile(
title: Text("Title"),
subtitle: Text(post.title),
),
ListTile(
title: Text("ID"),
subtitle: Text("${post.id}"),
),
ListTile(
title: Text("Body"),
subtitle: Text(post.body),
),
ListTile(
title: Text("User ID"),
subtitle: Text("${post.userId}"),
),
],
),
),
],
),
),
)
);
}
}
Next, create a posts.dart file in the lib directory
import 'package:flutter/material.dart';
import 'http_service.dart';
import 'post_detail.dart';
import 'post_model.dart';
class PostsPage extends StatelessWidget {
final HttpService httpService = HttpService();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Api get request"),
),
body: FutureBuilder(
future: httpService.getPosts(),
builder: (BuildContext context, AsyncSnapshot> snapshot) {
if (snapshot.hasData) {
List? posts = snapshot.data;
return ListView(
children: posts!
.map(
(Post post) => ListTile(
title: Text(post.title),
subtitle: Text("${post.userId}"),
onTap: () => Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => PostDetail(
post: post,
),
),
),
),
)
.toList(),
);
} else {
return Center(child: CircularProgressIndicator());
}
},
),
);
}
}
Open lib/main.dart and modify it to use PostsPage:
import 'package:flutter/material.dart';
import 'posts.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'HTTP',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: PostsPage(),
);
}
}
Step 4 : Run this Debug App
Output
I hope it will help you....
- How to Use the groupmod Command in Linux?
- How to Restrict Access to a Folder with htaccess?
- How to Zip UnZip Files And Directories In Linux using Command Line?
- Laravel 8 Refresh DataTable Without Reloading Page
- How to Model Disable Primary Key & Auto Increment Tutorial?
- How to Remove WordPress Version Number?
- How to Count Total Number of Bits in Number?
- Flutter Route and Navigation Example