How can I convert a `List<Map<String,String>>` to a `Set<Map<String,String>>` in flutter? - 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();

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 to set New Variable value from Old Variable value, if New Variable value changed the Old Variable not follow the changes

As stated in the title
Look at this code Example:
void main() {
final Student student = Student('Lincoln', 29);
print('Student before $student');
final Student newStudent = student;
newStudent?.name = 'Abraham';
print('new Student $newStudent'); /// 'Abraham', 29
print('Student after $student'); /// 'Abraham', 29 - but I need this output still 'Lincoln', 29
}
class Student {
Student(this.name, this.age);
String? name;
int? age;
#override
String toString() => '$name, $age';
}
From the code above if we set newStudent and make changes, the student variable also follows the changes, but I don't want the student variable changed. How to solve this?
You should make a new Student instance for the new one. If you want it to have the same name as age as the old you could do this for example:
final Student newStudent = Student(student.name, student.age);
and also study this example..this will clear the concept...
final List<int> numbers=[1,2,3];
print(numbers);
final List<int> numbers2=numbers;
numbers2.add(100);//this will add also to numbers
print(numbers);
//so use following for keep original array as it was
final List<int> numbers3=[...numbers];//user this spread operator
numbers3.add(200);
print(numbers);
so what u have to focus is
we passing reference not value by this statement
Student newstudent=&student (like in C language, here & sign is not used in dart

Correct declaring of a class in Dart

I am new to dart and I have some basicaly question to the language itself.
During the last days I started with classes in dart.
Now I have a short question about how to declare a class correct.
void main() {
Book harryPotter =
Book(title: "Goblet of Fire", author: "J. K. Rolling", pageCount: 300);
print(harryPotter._title); // 1 -> print "A" to the console
print(harryPotter._author); // 2 -> LateInitializationError: Field '_author#18448617' has not been initialized.
}
class Book {
String _title = "A";
late String _author;
late int _pageCount;
Book(
{required String title,
required String author,
required int pageCount}); // 3
}
Why can I access to the variable even if it's set to private?
Why does the late keyword throw an error, the variable is set during the constructor call?
Do I need to write in the constructor "Book({required String this.title});", or "Book({required String title});" like in the example? If it doesn't matter, why?
Thanks for helping!
Benjamin
Your constructor is not initializing the variables!
It should be:
Book({required String title, required String author, required int pageCount})
: _title = title,
_author = author,
_pageCount = pageCount;
Without that, the _author field is not set at all, and reading an unset late field is an error.
You can't use this.something because the fields have private names (_author) and named parameters cannot have private names. Otherwise that would have been the correct approach. Instead you need to have public-named parameters and then use the value to initialize the field in an initializer list.
With that change, the fields also don't need to be late and can instead be final:
class Book {
final String _title;
final String _author;
final int _pageCount;
Book({required String title, required String author, required int pageCount})
: _title = title,
_author = author,
_pageCount = pageCount;
}
You can access the private variables from inside the same library because Dart privacy is library based.

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 to convert List<Doctor> items = new List(); to List<String>? can you help me?

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