1
0
mirror of https://github.com/nisrulz/flutter-examples.git synced 2026-08-25 01:05:36 +00:00
Files
flutter-examples/using_http_get/lib/main.dart
Nishant Srivastava 5c53498119 refactor: standardize Dart and Android config across all 54 apps
- Bump all apps to sdk >=3.0.0 <4.0.0 and add flutter_lints + a shared
  analysis_options.yaml to every app (flutter analyze is now green repo-wide)
- Fix real compile errors in firebase_google_authentication and image_editor,
  plus deprecated APIs (Matrix4, SvgPicture.color, textScaleFactor),
  missing async 'mounted' guards, and misc lints via dart fix + manual fixes
- Align pubspec 'name' with each folder and Android namespace/applicationId
  with the documented github.nisrulz.* convention
- Convert bmi_calculator from a legacy Flutter module to a standard app
  with a proper android/ scaffold; fix missing android:exported in launcher
  manifests across 22 apps so all Android builds pass
- Untrack generated build files (GeneratedPluginRegistrant,
  .flutter-plugins-dependencies) and ignore them
2026-08-17 17:26:39 +02:00

96 lines
2.6 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 {
const MyGetHttpData({super.key});
@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.parse(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);
try {
if (dataConvertedToJSON.statusCode == 200) {
// Extract the required part and assign it to the global variable named data
data = dataConvertedToJSON['results'];
}
} catch (e) {
print(dataConvertedToJSON.statusCode);
}
});
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(
padding: const EdgeInsets.all(15.0),
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),
),
),
)
],
)),
);
}),
);
}
@override
void initState() {
super.initState();
// Call the getJSONData() method when the app initializes
getJSONData();
}
}