![]() |
In this article, we will learn how to create a dart model automatically with a sample of code using the freezed package. If you are making a large-scale app. You cannot write a model class file for every data you receive from the backend. So here’s a simple solution for it where this model will automatically generate you just need to add sample data in some format. Step By Step ImplementationStep 1: Create a New Project in Android StudioTo set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter. Step 2: Add the following packagesAdd the following dev dependency in your project Dart
Add the follwing dependency in your pubspec.yaml file Dart
Let’s understand what this package will use for
Step 3: Create a model folder and create a user model file in it and add sample data in itWe are using this sample json data for which we will create a model { Sample Code for creating freezed model in models/postmodels/postmodel.dart. Dart
Step 4: Run command to create automatic freezed model class in the modelIn terminal run this command to create a model class Dart
Before running this command After running this command 2 more files will be created i.e post_model.freezed.dart,post_model.g.dart. This file contains many function like copyWith, toJson, from Json, toString, hashcode and many more. This will help you to access the model data easily. |
Reffered: https://www.geeksforgeeks.org
Dart |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |