This is the 12th day of my participation in the Novembermore Challenge

preface

Recently, I am in touch with Flutter development. Many contents are new. I have made many detours and learned a lot. This post will share a technique used in the development of Flutter, which is to automatically generate dart files for model entity classes based on background interface request data.

Dart files are automatically generated using the JSON-to-entity class. The scenario is to get a Json with a complex data structure, or a Json with a lot of nested data. Because writing the model by hand is really inefficient, you can use this method to automatically generate THE Json entity class. You can create models for complex data. Now let’s look at the specific operation method.

steps

Dart files automatically generated by using the JSON-to-entity class are required for data processing in Flutter development. This not only facilitates data processing, but also saves a lot of operation time and improves development efficiency. The specific operation is divided into three steps, as follows:

1. Firstly, the whole JSON data format returned by the background was obtained through the interface provided by the background. I obtained the data according to the postman direct callback interface, and then selected all replicated data instances.

Github. IO /json_to_dar… Copy the obtained data entity class into the corresponding JSON input box, then enter the name of the dart file you want to Generate, click “Generate Dart” button, convert the JSON data into the DART entity class on the left side of the website, and display all the content of the generated DART file.

3. Create a DART file with the same name as the Json entity class in the Flutter project and copy all the generated content into the dart file in the flutter project.

The last

This is the end of the discussion about how to use JSON to Dart in Flutter development. This is a very useful knowledge that can save development time and improve development efficiency, especially when obtaining data from new data structures. Directly through the above steps can directly generate the corresponding data processing files, is easy to use, other no longer described.

The above is all the content of this chapter. Welcome to pay attention to the wechat public account of Sanzhan “Program Ape by Sanzhan”, and the Sina Weibo account of Sanzhan “Sanzhan 666”, welcome to pay attention!