Created
July 25, 2023 21:47
-
-
Save Nonlinearsound/e6d303063fd730ddcd9be2a43349fbab to your computer and use it in GitHub Desktop.
Initial example of getting data from a server in Flutter and displaying that data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'dart:convert'; | |
import 'package:flutter/material.dart' hide MenuItem; | |
import 'package:http/http.dart' as http; | |
void main() async { | |
runApp(const MyApp()); | |
} | |
class Album { | |
final int userId; | |
final int id; | |
final String title; | |
const Album({ | |
required this.userId, | |
required this.id, | |
required this.title, | |
}); | |
factory Album.fromJson(Map<String, dynamic> json) { | |
return Album( | |
userId: json['userId'], | |
id: json['id'], | |
title: json['title'], | |
); | |
} | |
} | |
Future<Album> fetchAlbum() async { | |
final response = await http | |
.get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1')); | |
if (response.statusCode == 200) { | |
// If the server did return a 200 OK response, | |
// then parse the JSON. | |
return Album.fromJson(jsonDecode(response.body)); | |
} else { | |
// If the server did not return a 200 OK response, | |
// then throw an exception. | |
throw Exception('Failed to load album'); | |
} | |
} | |
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( | |
primarySwatch: Colors.blue, | |
), | |
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; | |
late Future<Album> futureAlbum; | |
@override | |
void initState() { | |
super.initState(); | |
futureAlbum = fetchAlbum(); | |
} | |
void _incrementCounter() { | |
setState(() { | |
_counter++; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
const Text( | |
'You have pushed the button this many times:', | |
), | |
Text( | |
'$_counter', | |
style: Theme.of(context).textTheme.headline4, | |
), | |
FutureBuilder<Album>( | |
future: futureAlbum, | |
builder: (context, snapshot) { | |
if (snapshot.hasData) { | |
return Text(snapshot.data!.title); | |
} else if (snapshot.hasError) { | |
return Text('${snapshot.error}'); | |
} | |
// By default, show a loading spinner. | |
return const CircularProgressIndicator(); | |
}, | |
), | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: _incrementCounter, | |
tooltip: 'Increment', | |
child: const Icon(Icons.add), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment