Custom Data Types
Sometimes, the existing data types may not be enough to store certain kinds of information. In those cases, you can create a custom data type, which is a combination of existing data types.
For example, let's say you want to store a user's address, which consists of different parts like the street, city, state, and postal code. You can create a custom data type called "address" that combines all these parts together.
So, instead of using individual variables to store different pieces of information, custom data types allow you to group them together, making it easier to work with. It gives you more flexibility and enables you to handle more complex data structures based on your specific needs.
- If you're a newbie, think of custom data types as a way to store multiple variables (which can be of the same or different data types) inside a single variable. It's like having a box that can hold different things together.
- If you have a developer background, you can think of custom data types as classes. In programming, classes allow us to define our own data structures with specific properties and behaviors. It's like creating a blueprint for an object that can hold and manipulate related data.
Address as custom data type
- 1.Reusability: Custom data types can be reused across different parts of your application. Once defined, they can be utilized in App State, Firestore, API, custom code, and more!
- 2.Data validation: Storing into a custom data type helps you validate the values for each variable added inside it. This validation is impossible if you store all the information as combined data in plain text formats like JSON.
- 3.Readability: By defining custom data types, you clearly express the intent and purpose of the data. Especially in Firestore documents, It helps you declutter the document fields by nesting subfields into one parent field. For example, Instead of creating fields for apartment, street, city, and other information directly inside the 'users' document, you could add these fields into one parent field, called 'address', which gives you better visual clarity.
To create a Custom Data Type:
- 2.Click on the + button. A popup will open, enter the DataTypeName (This will be the name of the data type, e.g., 'address'), and click Create.
- 3.Click on the + Add Field.
- 4.Inside the field_name box, enter the name of the field. For example, street, city, country, etc.
- 5.Click on the Data Type dropdown menu and select the suitable one.
- 6.If you want to store multiple items of this data type, such as, a list of fruits, attendees, product names in single variable, enable the Is List option.
- 7.Click Create.
- 8.Similarly, add other fields.
Creating custom data type