Show Data of Mysql Database with HTTP Library with Show API

 import "dart:convert";

import "package:flutter/material.dart";
import "package:http/http.dart" as http;

void main() {
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Show data with HTTP API",
debugShowCheckedModeBanner: false,
theme: ThemeData(
appBarTheme: const AppBarTheme(
backgroundColor: Colors.grey,
centerTitle: true,
),
),
home: const ShowData(),
);
}
}

class ShowData extends StatefulWidget {
const ShowData({super.key});

@override
State<ShowData> createState() {
return ShowDataState();
}
}

class ShowDataState extends State<ShowData> {
Future<List<ShowDataModel>> fetchShowDataModel() async {
final response = await http.get(
Uri.parse("http://192.168.1.41/Flutter/show.php"),
);

if (response.statusCode == 200) {
Iterable jsonResponse = jsonDecode(response.body);

List<ShowDataModel> dataInputs = jsonResponse.map(
(model) {
return ShowDataModel.fromJson(model);
},
).toList();
return dataInputs;
} else {
throw Exception("Failed to load ShowDataModel");
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text(
"Show data with HTTP API",
style: TextStyle(fontWeight: FontWeight.bold),
),
),
body: Center(
child: FutureBuilder<List<ShowDataModel>>(
future: fetchShowDataModel(),
builder: (context, snapshot) {
if (snapshot.hasData) {
List<ShowDataModel>? data = snapshot.data;
return ListView.builder(
itemCount: data!.length,
itemBuilder: (context, index) {
return Card(
child: ListTile(
title: Text(
data[index].ID.toString(),
style: const TextStyle(
fontWeight: FontWeight.bold, fontSize: 20.0),
),
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"Name : ${data[index].name.toString()}",
style: const TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 10.0),
Text("BirthDate is : ${data[index].birth_date}"),
Text("Phone Number is : ${data[index].phone}"),
Text("Gender : ${data[index].gender}"),
],
),
),
);
},
);
} else if (snapshot.hasError) {
return Text("Error: ${snapshot.error}");
}
return const Center(
child: CircularProgressIndicator(),
);
},
),
),
);
}
}

class ShowDataModel {
final String ID;
final String name;
final String birth_date;
final String phone;
final String gender;

ShowDataModel(
{required this.ID,
required this.name,
required this.birth_date,
required this.phone,
required this.gender});

factory ShowDataModel.fromJson(Map<String, dynamic> json) {
return ShowDataModel(
ID: json["ID"] ?? 0,
name: json["name"] ?? "",
birth_date: json["birth_date"] ?? "",
phone: json["phone"] ?? "",
gender: json["gender"] ?? "",
);
}
}
<?php

header('Access-Control-Allow-Origin: *');

header('Access-Control-Allow-Methods: GET, POST');

header("Access-Control-Allow-Headers: X-Requested-With");
       
         $con = new mysqli("localhost","root","","student");
        $result= $con->query("select * from personal");    
          while($r= $result->fetch_object())
          {
            $row[]=$r;
          }
           echo json_encode($row);
?>



Comments

Popular posts from this blog

Pagination with Bloc Pattern in Flutter

Pagination First Practical in Flutter

ExpansionPanel with ExpansionPanelList with Complete Collapse Operation in Flutter