09 Interacting With REST API
09 Interacting With REST API
https://github.com/betsegawlemma/course_flutter_app/tree/main
/lib
Making Network Request
Adding the http package dependency in your pubspec.yaml file
Dependencies:
flutter:
sdk: flutter
http: ^0.12.2
Making Network Request
Importing the http package
if (response.statusCode == 200) {
return Course.fromJson(jsonDecode(response.body));
} else {
throw Exception('Failed to load course');
}
}
CRUD: Update Data
Sending UPDATE request to REST API
if (response.statusCode != 204) {
throw Exception('Failed to delete course.');
}
}
Sample CourseApp
Simple Flutter CRUD Application to Manage Course Info
{"id":"60266dfa3eb8d71118fb4c22","title":"AMP","code":"SI
TE0132","description":"lorem ipsum","ects":7}
The REST API
GET Request
[{"id":"60266dfa3eb8d71118fb4c22","title":"AMP","code":"S
ITE0132","description":"lorem
ipsum","ects":7},{"id":"60266eaa3eb8d71118fb4c23","title"
:"SEII","code":"SITE0232","description":"Lorem
Ipsum","ects":5}]
The REST API
PUT Request
No content
The REST API
DELETE Request
curl -X DELETE
http://localhost:3000/courses/60266eaa3eb8d71118fb4c23
No content
Sample CRUD Application
List of Courses Screen
Sample CRUD Application
Add New Course Screen
Sample CRUD Application
Course Detail Screen
Business Logic
Data
Repository
Data Provider
Code Structure
Create separate directory for each feature inside
the lib directory
flutter_bloc
http
equatable
CourseApp: Screens
course_add_update.dart
https://github.com/betsegawlemma/course_flutter_ap
p/blob/main/lib/course/data_provider/course_data.dart
CourseApp: Repository
Just abstraction of the details of
the data provider layer
https://github.com/betsegawl
emma/course_flutter_app/blob
/main/lib/course/repository/
course_repository.dart
CourseApp: Bloc
course_event.dart
course_state.dart
course_bloc.dart
https://github.com/betsegawlemma/course_flu
tter_app/tree/main/lib/course/bloc
CourseApp: CourseEvent
Four Events
corresponding to the
four CRUD operations
CourseApp: CourseState
Three States
CourseApp: CourseBloc
https://github.com/betsegawlemma/course_flutter_app/blob/main/lib/bloc_observer.dart
CourseApp
It is the root widget
Dependency Tree
CourseAPP
CourseRepository
CourseDataProvider
HttpClient
main.dart
The code on line 17 injects the course repository to the CourseApp and
creates CourseApp instance
Providing the Repository to the widget tree
The build method of CourseApp widget returns a MaterialApp widget
https://github.com/betsegawlemma/course_flutter_app/blob/main/lib/main.dart
Generating the CourseLoad Event
Inside the CourseApp while providing the CourseBloc to the tree we can
generate the first CourseLoad event
Consuming CourseLoadSuccess state
The default route for the application is CourseList screen
https://github.com/betsegawlemma/course_flutter_app/blob/mai
n/lib/course/screens/course_add_update.dart
DeleteCourse Event
Inside the CourseDetail widget when the delete button is pressed
CourseDelete event is generated
References
https://flutter.dev/docs/cookbook (Networking and Forms section)
https://bloclibrary.dev/