mirror of
https://github.com/nisrulz/flutter-examples.git
synced 2025-05-19 22:16:27 +08:00
89 lines
2.5 KiB
Dart
89 lines
2.5 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'dart:async';
|
|
import 'package:http/http.dart' as http;
|
|
import 'dart:convert';
|
|
|
|
void main() {
|
|
runApp(MaterialApp(
|
|
home: MyGetHttpData(),
|
|
));
|
|
}
|
|
|
|
// Create a stateful widget
|
|
class MyGetHttpData extends StatefulWidget {
|
|
@override
|
|
MyGetHttpDataState createState() => MyGetHttpDataState();
|
|
}
|
|
|
|
// Create the state for our stateful widget
|
|
class MyGetHttpDataState extends State<MyGetHttpData> {
|
|
final String url = "https://swapi.dev/api/people";
|
|
List data;
|
|
|
|
// Function to get the JSON data
|
|
Future<String> getJSONData() async {
|
|
var response = await http.get(
|
|
// Encode the url
|
|
Uri.encodeFull(url),
|
|
// Only accept JSON response
|
|
headers: {"Accept": "application/json"});
|
|
|
|
// Logs the response body to the console
|
|
print(response.body);
|
|
|
|
// To modify the state of the app, use this method
|
|
setState(() {
|
|
// Get the JSON data
|
|
var dataConvertedToJSON = json.decode(response.body);
|
|
// Extract the required part and assign it to the global variable named data
|
|
data = dataConvertedToJSON['results'];
|
|
});
|
|
|
|
return "Successfull";
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text("Retrieve JSON Data via HTTP GET"),
|
|
),
|
|
// Create a Listview and load the data when available
|
|
body: ListView.builder(
|
|
itemCount: data == null ? 0 : data.length,
|
|
itemBuilder: (BuildContext context, int index) {
|
|
return Container(
|
|
child: Center(
|
|
child: Column(
|
|
// Stretch the cards in horizontal axis
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: <Widget>[
|
|
Card(
|
|
child: Container(
|
|
child: Text(
|
|
// Read the name field value and set it in the Text widget
|
|
data[index]['name'],
|
|
// set some style to text
|
|
style: TextStyle(
|
|
fontSize: 20.0, color: Colors.lightBlueAccent),
|
|
),
|
|
// added padding
|
|
padding: const EdgeInsets.all(15.0),
|
|
),
|
|
)
|
|
],
|
|
)),
|
|
);
|
|
}),
|
|
);
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
|
|
// Call the getJSONData() method when the app initializes
|
|
this.getJSONData();
|
|
}
|
|
}
|