Fetch the list of musics
This commit is contained in:
32
lib/api.dart
Normal file
32
lib/api.dart
Normal file
@ -0,0 +1,32 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:music_web_player/config.dart';
|
||||
|
||||
class MusicEntry {
|
||||
final int id;
|
||||
final String artist;
|
||||
final String title;
|
||||
|
||||
const MusicEntry({
|
||||
required this.id,
|
||||
required this.artist,
|
||||
required this.title,
|
||||
});
|
||||
}
|
||||
|
||||
class API {
|
||||
/// Get the list of music
|
||||
static Future<List<MusicEntry>> getList() async {
|
||||
final response = await Dio().get(config.apiURL + "/list",
|
||||
options: Options(headers: {"Token": config.apiToken}));
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception("Request failed with status ${response.statusCode} !");
|
||||
}
|
||||
|
||||
return response.data
|
||||
.map((r) =>
|
||||
MusicEntry(id: r["id"], artist: r["artist"], title: r["title"]))
|
||||
.toList()
|
||||
.cast<MusicEntry>();
|
||||
}
|
||||
}
|
@ -1,4 +1,8 @@
|
||||
// ignore_for_file: avoid_print
|
||||
|
||||
import 'package:fluentui_system_icons/fluentui_system_icons.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:music_web_player/api.dart';
|
||||
import 'package:music_web_player/config.dart';
|
||||
|
||||
void main() {
|
||||
@ -32,8 +36,59 @@ class AppHome extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _AppHomeState extends State<AppHome> {
|
||||
var error = false;
|
||||
List<MusicEntry>? musics;
|
||||
|
||||
Future<void> load() async {
|
||||
try {
|
||||
setState(() => error = false);
|
||||
musics = await API.getList();
|
||||
setState(() {});
|
||||
} catch (e, s) {
|
||||
print("$e $s");
|
||||
setState(() => error = true);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
load();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold();
|
||||
return Scaffold(body: buildBody(context));
|
||||
}
|
||||
|
||||
Widget buildBody(BuildContext context) {
|
||||
if (error) {
|
||||
return Center(
|
||||
child: IntrinsicHeight(
|
||||
child: Column(
|
||||
children: [
|
||||
const Icon(FluentIcons.warning_24_regular, size: 50),
|
||||
const SizedBox(height: 50),
|
||||
const Text("Failed to load musics list!"),
|
||||
const SizedBox(height: 50),
|
||||
ElevatedButton(
|
||||
onPressed: load,
|
||||
child: Text("Try again".toUpperCase()),
|
||||
),
|
||||
],
|
||||
),
|
||||
));
|
||||
}
|
||||
|
||||
if (musics == null) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
if (musics!.isEmpty) {
|
||||
return const Center(child: Text("Musics list is empty!"));
|
||||
}
|
||||
|
||||
// TODO : go on
|
||||
return Text(musics![0].artist);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user