HTTP GET Response in Flutter

Model Class


// To parse this JSON data, do
//
// final welcome = welcomeFromJson(jsonString);

import 'dart:convert';

List<Welcome> welcomeFromJson(String str) => List<Welcome>.from(json.decode(str).map((x) => Welcome.fromJson(x)));

String welcomeToJson(List<Welcome> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class Welcome {
Welcome({
required this.userId,
required this.id,
required this.title,
});

int userId;
int id;
String title;

factory Welcome.fromJson(Map<String, dynamic> json) => Welcome(
userId: json["userId"],
id: json["id"],
title: json["title"],
);

Map<String, dynamic> toJson() => {
"userId": userId,
"id": id,
"title": title,
};
}

Main Class


// Importing important packages require to connect
// Flutter and Dart
import 'dart:convert';

import 'package:abc_quize/Welcome.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

// Main Function
void main() {
// Giving command to runApp() to run the app.

/* The purpose of the runApp() function is to attach
the given widget to the screen. */
runApp(const MyApp());
}

// Widget is used to create UI in flutter framework.

/* StatelessWidget is a widget, which does not maintain
any state of the widget. */

/* MyApp extends StatelessWidget and overrides its
build method. */
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);

// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
// title of the application
title: 'Hello World Demo Application',
// theme of the widget
theme: ThemeData(
primarySwatch: Colors.lightGreen,
),
// Inner UI of the application
home: const MyHomePage(title: 'Home page'),
);
}
}

/* This class is similar to MyApp instead it
returns Scaffold Widget */
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;

@override
State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
List<Welcome> reslut = [];

Future<void> getData() async {
var url = "https://jsonplaceholder.typicode.com/albums";

try {
final response = await http.get(
Uri.parse(url),
);
print('response.statusCode');
if (response.statusCode == 200) {
var responseData = json.decode(response.body);
var responseData1 = responseData;
print('list---------------->$responseData');
for (var coinJSON in responseData1) {
reslut.add(Welcome.fromJson(coinJSON));
}
print('---------------->${reslut[0].id}');
}
} catch (error) {
rethrow;
}
}

@override
void initState() {
// TODO: implement initState

getData();
super.initState();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
// Sets the content to the
// center of the application page
body: ListView.builder(
itemCount: reslut.length,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Card(
color: Colors.amber,
child: ListTile(
leading: Center(child: Text(reslut[index].id.toString())),
title: Text(reslut[index].userId.toString()),
subtitle: Text(reslut[index].title.toString()),
),
),
);
},
),
);
}
}


Comments

Popular posts from this blog

Unlocking the Power of OOP: A Beginner's Guide to Objects, Encapsulation, Inheritance, Abstraction, and Polymorphism

Building a Flutter Firebase Firestore CRUD App