简体   繁体   中英

What is the purpose this code in flutter?

This the class--

class CategoriesModel{
  String imgUrl;
  String categoriesName;
}

This the function--

List<CategoriesModel> getCategories(){
 List<CategoriesModel> categories = new List();
 CategoriesModel categoriesModel = new CategoriesModel();

 //
 categoriesModel.imgUrl ="";
 categoriesModel.categoriesName = "";
 categories.add(categoriesModel);
 categoriesModel=new CategoriesModel();

 return categories;
}

I did not get this code please explain this in a simple way. Thanks in advance.

It would be good to have more context about why do you need/use this function.

It is simply returning a list of CategoriesModel with a single object and empty.

categoriesModel.imgUrl =""; categoriesModel.categoriesName = ""; categories.add(categoriesModel);

this new object does not makes much sense:

categoriesModel=new CategoriesModel();

class CategoriesModel{
  String imgUrl;
  String categoriesName;
}

You have a class with two properties of type String


List<CategoriesModel> getCategories(){
 List<CategoriesModel> categories = new List();
 CategoriesModel categoriesModel = new CategoriesModel();

 //
 categoriesModel.imgUrl ="";
 categoriesModel.categoriesName = "";
 categories.add(categoriesModel);
 categoriesModel=new CategoriesModel();

 return categories;
}

A function, you create a new list and then create a new instance of the class CategoeriesModel() . Then you set the value of imgUrl and categoriesName to empty String and add them to a list. For some reason you create another instance of CategoeriesModel() , and return the list with the values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM