If you need a starting point, fork and clone this.
- Endpoints:
Get pets, type: Get, https://coded-pets-api-crud.eapi.joincoded.com/pets
Create a new pet, type: Post, https://coded-pets-api-crud.eapi.joincoded.com/pets
Update a pet, type: Put, https://coded-pets-api-crud.eapi.joincoded.com/pets/{petId}
Delete a pet, type: Delete, https://coded-pets-api-crud.eapi.joincoded.com/pets/{petId}
Adopt a pet, type: Post, https://coded-pets-api-crud.eapi.joincoded.com/pets/{petId}
-
In
pages
folder, create a new page calledadd_page.dart
. -
Include this page within your routes in
main.dart
. -
In your
home_page.dart
point theadd a new pet
button to the page we just created. -
In your widgets folder, create
add_form.dart
widget and inside it initialize a stateFul widget. -
Create a global key for your form.
-
Create a
Form
widget and assign it the key we just created. -
Create
TextFormField
s for our properties exceptadopted
,image
andid
. -
Create a button to submit our data.
-
Import your widget in the
add_page.dart
and render it. -
Back to
add_form.dart
, add a validator to each field as following:
All fields should not be empty.
Age should be of type int.
Gender should be either female or male
-
In your submit button
onPressed
, run your_formKey.currentState!.validate()
and test your form. -
On top of your form widget, create variables to hold our data.
-
In your
TextFormField
sonSaved
property, assign each value to a the variable you created for it. -
In your submit button
onPressed
check if the return of_formKey.currentState!.validate()
istrue
, and if so, run_formKey.currentState!.save()
. -
Install the
image_picker
package.
flutter pub add image_picker
- Import the package in
add_form.dart
. - On the top of your widget, create a variable to hold our image, and initialize an
ImagePicker
instance. - Within your form after the text fields, Create a
Row
with aGestureDetector
widget inside and aText
widget. - Within the
onTap
method, change it toasync
and call theImagePicker
instance to pick an image from the gallery. - Call
setState
and assign the result of the image picker to your_image
variable your created earlier. - Inside your
Container
check if the user selected an image and if so, display this image using theImage.File
widget. - Back to
services/pets.dart
, create a new function that returns a futurePet
object and takes aPet
argument. - Inside, Create a variable of type
Pet
and mark it aslate
. - Create variable
data
of typeFormData
and assign it toFormData.fromMap
and map your pet data inside it. - Create a request of type post to your endpoint and pass it the
data
variable we created:
Post, https://coded-pets-api-crud.eapi.joincoded.com/pets
-
Assign the
late
variable we created to the response, and return that variable. -
Don't forget to wrap your call with a
try-catch
block. -
Back to your
providers/pets.dart
, create a void function calledcreatePet
that takes an argument of typePet
. -
Inside it, call
PetsServices().createPet()
and pass to it the argument. -
Store the result in a variable called
newPet
. -
Insert this
newPet
in yourList
of pets in the provider. -
Don't forget to call
notifyListeners
. -
Lastly, Go to your submit button, and call the provider
createPet
function withlisten
set tofalse
and pass it the data of our form. -
Pop the screen so the user get's back to the home page.
-
As you did with the
add_page.dart
, create a page for updating a pet, as well as a form for that, both expects aPet
argument. -
Add your new page to your routes in
main.dart
, this time create a route param calledpetId
. -
Call your provider, look for the pet using the
petId
param, and pass it to the update page. -
In the
pet_card.dart
you have an edit icon, clicking on it should take the user to the update page and pass the pet id with it. -
In your form, access the pet argument using
widget.pet
. -
Using this, add an initial value to your field.
-
Back to
services/pets.dart
, create a function that returns futurePet
, requires an argument of typePet
and complete this function similarly to thecreatePet
function you did. -
Your endpoint is:
Put, https://coded-pets-api-crud.eapi.joincoded.com/pets/{petId}
-
Using string interpolation, inject the
pet.id
value within the url, and pass thedata
as a second argument. -
In your provider, create a function
updatePet
that takes aPet
argument and call thePetsServices().updatePet(pet: pet)
-
Finally, find the index of the pet we want to replace, and replace the pet with the response we got and call
notifyListeners
. -
In your update form submit button, call the provider function we just created and pass the form data.
-
Pop your screen so the user gets back to the
home_page
.