Initial commit: Penyerahan final Source code Tugas Akhir
This commit is contained in:
51
lib/providers/promo_course_provider.dart
Normal file
51
lib/providers/promo_course_provider.dart
Normal file
@ -0,0 +1,51 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:initial_folder/models/course_model.dart';
|
||||
import 'package:initial_folder/services/course_service.dart';
|
||||
|
||||
enum ResultState { Loading, NoData, HasData, Error }
|
||||
|
||||
class PromoCourseProvider with ChangeNotifier {
|
||||
final CourseService courseService;
|
||||
PromoCourseProvider({required this.courseService}) {
|
||||
getPromoCourse();
|
||||
}
|
||||
|
||||
List<CourseModel> _course = [];
|
||||
|
||||
ResultState? _state;
|
||||
|
||||
String _message = '';
|
||||
|
||||
List<CourseModel> get result => _course;
|
||||
|
||||
ResultState? get state => _state;
|
||||
|
||||
String get message => _message;
|
||||
|
||||
set course(List<CourseModel> course) {
|
||||
_course = course;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<dynamic> getPromoCourse() async {
|
||||
try {
|
||||
_state = ResultState.Loading;
|
||||
notifyListeners();
|
||||
List<CourseModel> course = await courseService.getPromoCourse();
|
||||
if (course.isEmpty) {
|
||||
_state = ResultState.NoData;
|
||||
print("Tidak ada data promo dri api");
|
||||
notifyListeners();
|
||||
return _message = 'Empty Data';
|
||||
} else {
|
||||
_state = ResultState.HasData;
|
||||
notifyListeners();
|
||||
return _course = course;
|
||||
}
|
||||
} catch (e) {
|
||||
_state = ResultState.Error;
|
||||
notifyListeners();
|
||||
return _message = 'Error --> $e';
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user