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(
home: HomePage(),
);
}
}
//Creating a class user to store the data;
class Userinfo {
final int id;
final String nom;
final String prenom;
Userinfo({
required this.id,
required this.nom,
required this.prenom,
});
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
//Applying get request.
Future<List<Userinfo>> getRequest() async {
//replace your restFull API here.
String url = "http://10.0.2.2:8080/userinfo/allusers";
final response = await http.get(Uri.parse(url));
var responseData = json.decode(response.body);
//Creating a list to store input data;
List<Userinfo> users = [];
for (var singleUser in responseData) {
Userinfo user = Userinfo(
id: singleUser["id"],
nom: singleUser["nom"],
prenom: singleUser["prenom"],
);
//Adding user to the list.
users.add(user);
}
return users;
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
title: const Text("Http Get Request."),
leading: const Icon(
Icons.get_app,
),
),
body: Container(
padding: const EdgeInsets.all(16.0),
child: FutureBuilder(
future: getRequest(),
builder: (BuildContext ctx, AsyncSnapshot snapshot) {
if (snapshot.data == null) {
return const Center(
child: CircularProgressIndicator(),
);
} else {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (ctx, index) => ListTile(
title: Text(snapshot.data[index].nom),
subtitle: Text(snapshot.data[index].prenom),
contentPadding: const EdgeInsets.only(bottom: 20.0),
),
);
}
},
),
),
),
);
}
}
/*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});
// 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: 'Flutter Demo 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> {
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
void consomme() async {
var url = Uri.parse('http://10.0.2.2:8080/userinfo/allusers');
// make http get request
var response = await http.get(url);
// check the status code for the result
if (response.statusCode == 200) {
print(response.body);
} else {
print('Request failed with status: ${response.statusCode}.');
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: (){
consomme();
},
child: const Text('Disabled'),
),
const SizedBox(height: 30),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}*/