import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:tmt_01/model/ResponseModel.dart';

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

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

// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;

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

class _MyHomePageState extends State<MyHomePage> {

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),

body:
FutureBuilder<List<ResponseModel>>(
future: fetchData(),
builder: (BuildContext context, AsyncSnapshot<List<ResponseModel>> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
} else if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
} else {
return ListView.builder(
itemCount: snapshot.data!.length,
itemBuilder: (context, index) {
final post = snapshot.data![index];
return ListTile(
title: Text(post.ma_the!),
subtitle: Text(post.ho_ten!),
);
},
);
}
},
)

);
}

Future<List<ResponseModel>> fetchData() async {
// Replace this URL with your API endpoint
final response = await http.get(Uri.parse('http\20V%C5%A9%20H%E1%BB%AFu&tenkhoi=6&tenlop=A&tenhuyen=B%C3%ACnh%20Giang'));
if (response.statusCode == 200) {
// If the call to the server was successful, parse the JSON
final data = json.decode(response.body) as List<dynamic>;
return data.map((json) => ResponseModel.fromJson(json)).toList();
} else {
// If that call was not successful, throw an error.
throw Exception('Failed to load data');
}
}

}
=================
class ResponseModel {

int? STT;
String? ho_ten;
String? ma_the;

ResponseModel({
this.STT,
this.ho_ten,
this.ma_the,});

ResponseModel.fromJson(dynamic json) {
STT = json['STT'];
ho_ten = json['ho_ten'];
ma_the = json['ma_the'];
}

Map<String, dynamic> toJson() {
final map = <String, dynamic>{};
map['STT'] = STT;
map['ho_ten'] = ho_ten;
map['ma_the'] = ma_the;
return map;
}
}