Select Git revision
Forked from
신선 / 1801_OS_assignment4
Source project has a limited visibility.
-
HyukSang Kwon authoredHyukSang Kwon authored
main.dart 6.63 KiB
import 'dart:convert';
import 'package:english_words/english_words.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:http/http.dart' as http;
void main() {
runApp(
ChangeNotifierProvider(
create: (context) => MyAppState(),
child: const MyApp(),
),
);
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
var appstate=context.watch<MyAppState>();
return MaterialApp(
title: 'Namer App',
theme: ThemeData(
useMaterial3: true,
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepOrange),
),
home: FutureBuilder<List<WordPair>>(
future: appstate.futureFavorites,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
} else if (snapshot.hasError) {
return Center(child: Text('Error : ${snapshot.error}'),);
} else if (snapshot.hasData) {
return MyHomePage();
} else {
return Center(child: Text('No Favorites yet'));
}
}
),
);
}
}
class MyAppState extends ChangeNotifier {
var current = WordPair.random();
void getNext() {
current = WordPair.random();
notifyListeners();
}
var favorites = <WordPair>[];
late Future<List<WordPair>> futureFavorites;
MyAppState() {
futureFavorites = fetchFavorites();
}
Future<List<WordPair>> fetchFavorites() async {
final response=await http.get(
Uri.parse('https://wonbin3977.web.ajousw.kr/likes'));
List<WordPair> list = [];
var jsonData = jsonDecode(response.body);
for(Map<String,dynamic> obj in jsonData) {
List<String> res=obj["id"].split('_');
list.add(WordPair(res[0],res[1]));
}
favorites = [...list];