What is the purpose this code in flutter? - 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.

Related

Flutter dart replace replace Json object with variables

In this case I have class. Where I took a variable. Also I have a Json map. So I want to change Json map object replace with variables. Here is my code example....
So how can I achieve that
I want replace Json object with dart variable
class Data {
late String slug;
Map<String, String> singleProductVariable = {"slug": "$slug"};
}
Firstly, there is no JSON in your code sample.
I assume that you would like to set the value of the corresponding key in your Map when setting the variable.
If so, you might want to use a setter in a next way:
class Data {
String _slug;
late Map<String, String> v = {"slug": _slug};
Data(String slug) : _slug = slug;
set slug(String str) => v['slug'] = str;
}
void main() {
final d = Data("slug");
print(d.v);
d.slug = "newSlug";
print(d.v);
}
The output of the code above will be:
{slug: val}
{slug: newVal}

How can I convert a `List<Map<String,String>>` to a `Set<Map<String,String>>` in flutter?

I made Hindi Vocabulary app using flutter.
I want to know how to convert a List<Map<String,String>> to a Set<Map<String,String>>.
Because if users add some words which they want to remind, they can add this in unmemory list. But if they see the same section, the words they want to add are overlapped. So I want to terminate the overlapping words using the set.
Here is my code:
class unMemory_words {
String words;
String word_class;
String mean;
String example_hindi;
String example_korean;
Map<String, String> _saved_word_list;
static List<Map<String, String>> list = new List<Map<String, String>>();
unMemory_words(
String words,
String word_class,
String mean,
String example_hindi,
String example_korean,
) {
this.words = words;
this.word_class = word_class;
this.mean = mean;
this.example_hindi = example_hindi;
this.example_korean = example_korean;
_saved_word_list = {
'hindi': this.words,
'case': this.word_class,
'meaning': this.mean,
'hindi_example_sentence': this.example_hindi,
'korean_example_sentence': this.example_korean
};
list.add(_saved_word_list);
}
}
Thank you!
You can do this by this way:
final list = <Map<String, String>>[];
final set = list.toSet();

Dart Flutter How to initialize a class method inside of its class?

Here is my class:
class WorldTimeClass {
String flag;
String url;
String time;
String location;
WorldTimeClass({this.flag, this.url, this.time, this.location});
Future<String> getData() async {
try{
Response load = await get('http://worldtimeapi.org/api/timezone/$url');
Map x(){if(load.statusCode == 200){
print(load.statusCode);
Map map = jsonDecode(load.body);
return map;}
else{
print('No Access');
return {1:'NoAccess.'};}
}
Map myMap = x();
String datetime = myMap['utc_datetime'];
String offsetUTC = myMap['utc_offset'];
DateTime dateTimeObjectConvert = DateTime.parse(datetime);
// Below converts the datetime string to a DateTime Object and then converts the UTC Offset to a substring only '01' out of +01:00 and then converts it to an int Object and then adds it to the DateTime Object as a Duration (hours);
dateTimeObjectConvert = dateTimeObjectConvert.add(Duration(hours: int.parse(offsetUTC.substring(1,3))));
return time = dateTimeObjectConvert.toString();
}
catch(e,s){
return 'Could not access time data from API.\nWe are sorry, please try again.\nError occured: $e';
}
}
var myString = getData().then((value) => value);
DateFormat pretty = DateFormat().add_jm().format(myString);
}
How can I access myString and execute it inside my class in order to use the resulting String object to use it inside a second method pretty ?
Also, I need to understand what does the below exception mean?
Only static members can be accessed in initializers.
Only static members can be accessed in initializers.
This basically means that you cannot call methods of a class or access properties of a specific class directly under class declaration.
You are getting the error on those two lines:
var myString = getData().then((value) => value);
DateFormat pretty = DateFormat().add_jm().format(myString);
Therefore create a method that returns a String then all you have to do is to call that method and it will give you the String, and add the code above inside the method:
String getDateFormat(){
var myString = getData().then((value) => value);
return DateFormat().add_jm().format(myString);
}
To access your myString variable you'll have to do one of those things:
Instantiate an WorldTimeClass object and access it using yourWorldClassObject.myString
Make it into in static member by using the static keyword like static var myString. This is what " Only static members can be accessed in initializers. " is all about. You have to create an instance of the class if you want to access it's properties, or make them static to access them without the need to instantiate an object. Simply WorldTimeClass.myString.

Vaadin 6 combo box linked with enum

I wanted to know if there is a way to assign the select items all the values of an enum instead of manually adding each one of them.Currently I do this:
ComboBox myBox = new ComboBox();
for(SelectValuesEnum enum: SelectValuesEnum.values()){
myBox.addItem(enum)
}
With this you get a List with all enum values
new ArrayList<MyEnum>(Arrays.asList(SelectValuesEnum.values()));
And this you can convert it into a Collection and use it in the ComboBox constructor which accepts a Collection as argument.
You can use simple BeanContainer:
BeanContainer<Integer, YouEnum> cbContainer = new BeanContainer<Integer, YouEnum>(YouEnum.class);
cbContainer.setBeanIdProperty("id");
cbContainer.addAll(EnumSet.allOf(YouEnum.class));
ComboBox cb = new ComboBox(null, cbContainer);
cb.setItemCaptionPropertyId("fieldDescription");
cb.setImmediate(true);
// return cb;
public enum YouEnum {
VAL_1("value 1"),
VAL_2("value 2");
private final String fieldDescription;
private YouEnum(String value) {
fieldDescription = value;
}
public String getFieldDescription() {
return fieldDescription;
}
public String getId(){
return String.valueOf(ordinal());
}
}

Where does IModel Apache Wicket retrieve an object?

First of all, please take a look at how IModel is used in this example:
#SuppressWarnings("serial")
public static List<IColumn> getTableColumns(
final ReportParams reportParams, final boolean columnsSortable
) {
List<IColumn> columns = new ArrayList<IColumn>();
final Map<String,ToolInfo> eventIdToolMap = Locator.getFacade().getEventRegistryService().getEventIdToolMap();
// site
if(Locator.getFacade().getReportManager().isReportColumnAvailable(reportParams, StatsManager.T_SITE)) {
columns.add(new PropertyColumn(new ResourceModel("th_site"), columnsSortable ? ReportsDataProvider.COL_SITE : null, ReportsDataProvider.COL_SITE) {
#Override
public void populateItem(Item item, String componentId, IModel model) {
final String site = ((Stat) model.getObject()).getSiteId();
String lbl = "", href = "";
Site s = null;
try{
s = Locator.getFacade().getSiteService().getSite(site);
lbl = s.getTitle();
href = s.getUrl();
}catch(IdUnusedException e){
lbl = (String) new ResourceModel("site_unknown").getObject();
href = null;
}
item.add(new ImageWithLink(componentId, null, href, lbl, "_parent"));
}
});
}
And my questions are:
How does populateItem get an input for IModel parameter?
I cannot find any code in this application, which explicitly constructs IModel object. Is it correct for me to assume that the object is retrieved directly from a table in the database? I'm thinking of this because Mapping Hibernate is used for this application.
The models are created using the IDataProvider you provide to the DataTable (DataTable constructor will also take your IColumn List) .
The IDataProvider could use Hibernate - hard to say without having more information on that implementation.