how to convert List<Doctor> items = new List(); to List<String>? can you help me? - flutter

how to convert List items = new List(); to List? can you help me?

List<Doctor> items = new List();
--- add data to items list ---
then,
List<String> strings = new ArrayList<>(items.size());
for (Doctor doctor: items) {
strings.add(Objects.toString(doctor, null));
}
if you are using java 8,
List<String> strings = items.stream()
.map(doctor -> Objects.toString(doctor , null))
.collect(Collectors.toList());

https://api.dartlang.org/stable/2.4.0/dart-core/List-class.html
^ Use .map method of List<Doctor> instance.
For example
var itemStrings = items.map((doc) {
// Title property as example
return doc.title;
}).toList();
I recommend that you take dart language tour

Related

Duplicate Array list value automatically changed in Flutter

I'm very surprised after this issue. First, inform all things I have used in my project.
I have used Getx in my project. I have called the API using the Getx controller file.
Below code used in getx controller file. "PLTimeSlotModel" is model and. it has two params (name, isselect).
var futureTimeSlot_PL_C_D = Future.value(<PLTimeSlotModel>[]).obs;
callTimeSlotAPI() async {
futureTimeSlot_PL_C_D.value = FetchTimeSlotList();
}
Future<List<PLTimeSlotModel>> FetchTimeSlotList() async {
// Fetching data with API calling
}
Screen A:
List<PLTimeSlotModel> listA = [];
List<PLTimeSlotModel> listB = [];
#override
void initState() {
super.initState();
_plController.callTimeSlotAPI();
}
Another method is to create two lists using the future list:
List<PLTimeSlotModel> temp1 = await _plController.futureTimeSlot_PL_C_D.value;
temp1.forEach((element) {
listA.add(element);
listB.add(element);
});
onclick:
for(int i =0;i<listA.length;i++){
listA[i].isselect = false;
print(listA[i].isselect);
print(listB[i].isselect);
}
Now the issue is I have changed/updated the only "listA" value, So why automatically set the same value to "listB"? The two list is based on the one list.
A List in Dart contains references objects, not the objects themselves. So what you are doing is copying references to objects into two lists. But since they are pointing at the same objects, you will see any modification in one list also happen in the other list. You need to copy each PLTimeSlotModel object and put the copy into your new list.
One way to copy objects is to create an constructor which takes an object of the same type and creates a new object based on this first object. So something like this:
class Person {
String name;
Person(this.name);
Person.fromPerson(Person other) : this(other.name);
#override
String toString() => 'Person($name)';
}
void main() {
final persons = [
Person('Adam'),
Person('Bob'),
];
// Make new list based on persons
final otherPersonList = [
...persons.map((person) => Person.fromPerson(person))
];
otherPersonList[0].name = 'Carl';
print(persons); // [Person(Adam), Person(Bob)]
print(otherPersonList); // [Person(Carl), Person(Bob)]
}

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();

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.

How can I combine multiple objects based on the same value they have in Dart?

class Item{
var name;
var productId;
Item(this.name,this.productId);
}
class Price{
List<String> listOfProductIds = [];
double amount;
}
Lets say I have list of prices:
[["productid1","productid3"], 199.00], ["productid2","productid4"],299.00],...);
I have TWO different lists.
This one:
[["myName","productid1"],["myNamee","productid2"]]
And, this one:
[["myName","productid3"],["myNamee","productid4"]]
for example, I want to retrieve items for a price of 199.00. basically I want to show ["myName","productid1"] and ["myName","productid3"] How can I achieve this?
As far as I know this is a custom use case and this cannot be done with a single statement in Dart. However, this result can be achieved with the help of the forEach loop in Dart.
Here is a minimal example reproducing your above problem -
class Prices {
List<String> products;
double price;
Prices(this.products, this.price);
}
void main() {
List<Prices> priceList = [Prices(["productid1","productid3"], 199.00), Prices(["productid2","productid4"],299.00)];
List<List<String>> productList1 = [["myName","productid1"],["myNamee","productid2"]];
List<List<String>> productList2 = [["myName","productid3"],["myNamee","productid4"]];
List<List<String>> resultList = List();
List<String> comparatorList = priceList.singleWhere((priceObj) => priceObj.price == 199.0).products;
productList1.forEach((product) {
if(comparatorList.contains(product[1])) {
resultList.add(product);
}
});
productList2.forEach((product) {
if(comparatorList.contains(product[1])) {
resultList.add(product);
}
});
print(resultList.toString());
//Above statement prints [[myName, productid1], [myName, productid3]]
}
In the above example first we create the comparatorList which contains the names of only those products which are priced at 199.0.
Then, we extract only those items from both the product lists which are present in the comparatorList as well. Hope this helps!
This is what I have done to overcome the issue:
Item tempOne,tempTwo;
for(int i=0; i<prices.listOfProductIds.length;i++){
for(int j=0; j<itemOne.length; j++){
if((prices.listOfProductIds[i]) == (itemOne.productId)){
tempOne = itemOne;
break;
}
}
for(int j=0; j<itemTwo.length; j++){
if((prices.listOfProductIds[i]) == (itemTwo.productId)){
tempTwo = itemTwo;
break;
}
}
Wrapper finalResult = new Wrapper(tempOne,tempTwo,prices[i]);
}
Defined wrapper class
class Wrapper{
Item itemOne,itemTwo;
Price price
Warpper(this.itemOne,this.itemTwo,this.price)
}

GWT Sorting after filtering a table

I have a simple table (CellTable) and a TextBox for filter.
Values for CellTable I take from backend. After list of items is loaded I can put some filter string to get suitable result. To filter list from dataprovider I use a predicate. After filtering I set result to CellTable.
Like here:
void onFilterNameKeyUp(KeyUpEvent e) {
List<CustomerDTO> filtered = this.dataProvider.getList();
Predicate<CustomerDTO> filter = new Predicate<CustomerDTO>() {
#Override
public boolean apply(CustomerDTO input) {
if (input.getName() == null) {
return false;
} else {
return input.getName().toLowerCase()
.contains(filterName.getValue().toLowerCase());
}
}
};
filtered = Lists.newArrayList(Iterables.filter(this.dataProvider.getList(), filter));
this.customerList.setRowCount(filtered.size(), true);
this.customerList.setRowData(0, filtered);
}
this.dataProvider = new ListDataProvider<CustomerDTO>();
dataProvider.addDataDisplay(this.customerList);
final ListHandler<CustomerDTO> sortHandler = new ListHandler<CustomerDTO>(
this.dataProvider.getList());
this.customerList.addColumnSortHandler(sortHandler);
this.customerList.getColumnSortList().push(this.rbKeyNameColumn);
Everything work fine but I have also a sort handler for first column of this table and when I want to sort a list then I'm sorting original list of items.
How to solve this problem? It means that I want to sort and display filtered list instead of original list from dataprovider.
Please help.
I have a table changing content after receiving some data from the server. This is my approach:
public void onSuccess(List<List<String>> result) {
List<Product> list = dataProvider.getList();
list.clear();
r_tableproducts.setRowCount(result.size());
for (List<String> l : result) {
list.add(new Product(l));
}
r_tableproducts.getColumnSortList().clear();
r_tableproducts.getColumnSortList().push(r_tableproducts.getColumn(1));
ColumnSortEvent.fire(r_tableproducts,r_tableproducts.getColumnSortList());
}
This sorts my list using 2nd column after reciving new data (overriding all old data).
Maybe try this approach? Filter your data in a new list and modify the list your dataProvider provides you, adding all elements.
Something like:
List<CustomerDTO> result = Lists.newArrayList(Iterables.filter(this.dataProvider.getList(), filter));
this.customerList.setRowCount(result.size(), true);
this.dataProvider.getList().clear();
this.dataProvider.getList().addAll(result);
You need to separate the dataProvider that holds the full list and provide a separate "filtered" data provider that will be driving the display of the cellTable. The cellTable in below example is bound to a filteredDataProvider for sorting, display purposes.
The code below is to give a general idea and therefore may not be syntactically accurate.
void onFilterNameKeyUp(KeyUpEvent e) {
List<CustomerDTO> filtered = this.dataProvider.getList();
Predicate<CustomerDTO> filter = new Predicate<CustomerDTO>() {
#Override
public boolean apply(CustomerDTO input) {
if (input.getName() == null) {
return false;
} else {
return input.getName().toLowerCase()
.contains(filterName.getValue().toLowerCase());
}
}
};
filtered = Lists.newArrayList(Iterables.filter(this.dataProvider.getList(), filter));
this.customerList.setRowCount(filtered.size(), true);
this.filteredDataProvider.getList().clear();
this.filteredDataProvider.getList().addAll(filtered);
}
this.dataProvider = new ListDataProvider<CustomerDTO>();
// Code here to populate the dataProvider
this.filteredDataProvider = new ListDataProvider<CustomerDTO>();
// Creating separate DataProvider to hold the filtered set
this.filteredDataProvider.getList().addAll(this.dataProvider.getList());
filteredDataProvider.addDataDisplay(this.customerList);
// dataProvider.addDataDisplay(this.customerList);
// Sort Handler uses filteredDataProvider instead of the dataProvider with full list
final ListHandler<CustomerDTO> sortHandler = new ListHandler<CustomerDTO>(
this.filteredDataProvider.getList());
this.customerList.addColumnSortHandler(sortHandler);
this.customerList.getColumnSortList().push(this.rbKeyNameColumn);