import 'dart:async'; import 'dart:convert'; import 'package:http/http.dart' as http; import 'package:rxdart/rxdart.dart'; import 'package:empty/models/album.dart'; import 'package:empty/models/bbs.dart'; import 'package:empty/constants.dart'; class AlbumBloc { final _albumSubject = BehaviorSubject(); final _albumGuide = BehaviorSubject(); final _bbsList = BehaviorSubject(); Future getData(String type,String page, String size) async { var response = await http.get( kConfig['bbs'] + type + "?page=" + page + "&size=" + size, headers: {'Content-Type':'application/json','x-aliot-token':kEmptyToken}, ); if (response.statusCode == 200) { Bbs result = Bbs.fromJson( json.decode(response.body) ); return result; } else { print('Request failed with status: ${response.statusCode}.'); } } Future fetchData(String type, String section) async { var response = await http.post( kConfig['list'], body: jsonEncode({'type':type,'section':section,'rcmd':'','size':20,'page':1}), headers: {'Content-Type':'application/json','x-aliot-token':kEmptyToken}, ); //print(kUserInfo['emptyToken']); if (response.statusCode == 200) { // print('album : ' + response.body); Album result = Album.fromJson( json.decode(response.body) ); return result; } else { print('Request failed with status: ${response.statusCode}.'); } } void fetch(String type, String section) async { var albumResult = await fetchData(type,section); _albumSubject.add(albumResult); } void guide(String type, String section) async { var albumResult = await fetchData(type,section); _albumGuide.add(albumResult); } void bbs(String type,String page, String size) async { var albumResult = await getData(type,page,size); _bbsList.add(albumResult); } Stream get albumResult => _albumSubject.stream; Stream get albumGuide => _albumGuide.stream; Stream get bbsList => _bbsList.stream; }