Getting data from Google Sheetsš ā Flutter Appš± ā Part 2
In this article, Weāll learn to load data from Google Sheets and show it into the list in the Flutter app.
--
Hello everyone, this is Part 2 of this series where weāll learn to get data from Google Sheets into the Flutter app. If anyhow you missed Part 1 of this article series then hereās the link:
Continuing from the previous articleā¦
In the previous article, we set up Google AppScript to insert/append fields into the Google Sheet using doPost()
method. Now weāll be implementing doGet()
method which will return the list of feedback responses as JSON array of objects š. So letās startā¦
Now, Just open your Google AppScript code again and just add a new method doGet()
as below.
Hereās a quick ā”ļø explanation for the above script:
- First of all, after opening Google Sheet, weāve received all values.
- Then iterating data in descending order so that weāll be able to see recent feedback item first.
- Adding each feedback to
data
. - Finally returning data as a JSON.
Deploy New version š :
- Once AppScript is ready, again deploy your AppScript by Selecting a tab: Publish ā Deploy as a web app.
- Assign a new version to your app and click āDeployā š.
- After that, you can check if itās working or not by visiting that URL. Youāll see it like below.
- Niceš. This is working perfectly! Now letās start Flutter š.
ā”ļø Letās Flutter š:
In the previous article, weāve already done setting up the Flutter project. So letās continue directly from there.
- In form.dart, you can see we have a factory method called
fromJson()
which instantiates object from JSON. We will be using that function here in this article.
factory FeedbackForm.fromJson(dynamic json) {
return FeedbackForm(
"${json['name']}",
"${json['email']}",
"${json['mobileNo']}",
"${json['feedback']}"
);
}
- Now just go to form_controller.dart and weāll create a new method called
getFeedbackList()
which will return List of Feedback from AppScript URL viaGET
request which we recently created in the previous step.
- Okay! Weāre now done with data controlling part. Now letās develop UI for it. Create another dart file for UI letās say feedback_list.dart as below.
You can refer full code from GitHub repository here. Here Iām just showing an important part of code.
- So this is
ListView
which is being populated frominitState()
method once the list is loaded from the network. Once this screen is loaded,initState()
will be invoked and after some seconds, youāll see a list of responses.
Weāve done the implementation of our flutter app. Letās test it š.
Run command ā flutter run
and see the magic! š
Yeah! š Itās working as expected. As you can see, this data is loaded from Google Sheets using Google AppScript. Hope you liked that. If you find it helpful please share this. Maybe itāll help someone needy!
Wanna try it right now? š¤·āāļø
You can try the web version of this app here.
At ScaleReal We believe in Sharing and Open Source.
If you found this project useful, then please consider giving it a āļø on GitHub and sharing it with your friends via social media.
Sharing is Caring!
Thank You! š
āļø Resources:
Hereās GitHub repo which contains Flutter + Google AppScript Code.
Web Version:
free to reach me anytime onā¦