Flutter & Firebase App Tutorial — Part 4 Custom model in Flutter App
Create a flutter authenticate app in 10 mins by using firebase as the back-end. You will learn how to transform a Firebase object into a custom class to let your app look simpler and structured. If you haven’t set up your environment, please go to Flutter & Firebase App Tutorial — Part 1 Get Started.
More Tutorials:
- Flutter & Firebase App Tutorial — Part 1 Get Started
- Flutter & Firebase App Tutorial — Part 2 App Structure
- Flutter & Firebase App Tutorial — Part 3 Firebase Sign In Anonymously
- Flutter & Firebase App Tutorial — Part 4 Custom model in Flutter App
- Flutter & Firebase App Tutorial — Part 5 Stream & Provider in Flutter
- Flutter & Firebase App Tutorial — Part 6 Firebase Sign Out
- Flutter & Firebase App Tutorial — Part 7 Firebase Sign In and Register With Email
- Flutter & Firebase App Tutorial — Part 8 toggle between forms and form validation
- Flutter & Firebase App Tutorial — Part 9 loading Spinkit and theme & layout
Table of Contents
- Create a custom model
- Turn Firebase object into the custom model
Now, we currently can get back the response from the Firebase when we sign in anonymously if it was a success. The response from the Firebase is a User
object that contains a UID identifying the user that’s logged in but it also contains lots of information that we do not need for this app.
Hence, when we receive the User
object back, we turn it into an object that is simpler and just contains the information that we need.
Step 1️⃣: Create a models
folders and a file called UserModel.dart.
Step 1.1 The only thing we need is afinal
property that won't change as the user goes between different screens and is also a String
:uid
Step 1.2 Set the uid
property in a constructor. UserModel({this.uid});
will user name parameter to set the final
property.
When we create a new instance of the UserModel
class, we will pass in theuid
property and it will auto-apply it to the final String uid
property.
class UserModel {
final String uid; UserModel({required this.uid});
}
Step2️⃣: Turn Firebase object into the custom model
We will turn the Firebase User
object into a UserModel
object based on the UserModel
class.
Step 2.1 Go to auth_service
file and create a method that returns an UserModel
object. It’s a private function denoted by this underscore meaning we can only use it inside this file.
We accept a FirebaseUser
object in this method. And Inside this method, we take that User
object and turn it into an UserModel
object.
Step 2.1 Return an UserModel
object in the signInAnonymously()
method
Now we simplify the FirebaseUser
object in our app and don't have the extra information we do not need. And we now can control the properties in our app.
Next, we can listen to the users signing in so we can react to that and show the correct content.
Currently, we can sign in to our app and Firebase can return the User
object back into our custom UserModel
object.
Next, we will listen for when we get the User
object back and there is an auth change. For example, a user just logs in and we want to then show the user the Home screen instead of the Sign-in screen.
More Tutorials:
- Flutter & Firebase App Tutorial — Part 1 Get Started
- Flutter & Firebase App Tutorial — Part 2 App Structure
- Flutter & Firebase App Tutorial — Part 3 Firebase Sign In Anonymously
- Flutter & Firebase App Tutorial — Part 4 Custom model in Flutter App
- Flutter & Firebase App Tutorial — Part 5 Stream & Provider in Flutter
- Flutter & Firebase App Tutorial — Part 6 Firebase Sign Out
- Flutter & Firebase App Tutorial — Part 7 Firebase Sign In and Register With Email
- Flutter & Firebase App Tutorial — Part 8 toggle between forms and form validation
- Flutter & Firebase App Tutorial — Part 9 loading Spinkit and theme & layout
Seeing my followers grow will encourage me to write more articles. If you have any questions or anything to be improved please write a comment in the comments section.
I am a Flutter lover, and I talk about flutter, mobile development, and artificial intelligence.
👉 The source code is updated in Github