Console print value detail on the List - flutter

I have class Student and List like below
List<Student> info;
class Student {
final String id;
final String name;
Student({this.id, this.name});
}
and I have some code to add value to List
for (var v; v < count; v++) {
info.add(Student(
id: ....,
name: ....,
));
}
How can I console print detail of value on List<Student> info for test?

Override toString of Student class to make it return the String you want:
class Student {
final String id;
final String name;
Student({this.id, this.name});
#override
String toString() => 'id: $id, name: $name';
}
Then print the list with print(info).

You can just do this
print(info.toString());
to print the whole list.

Related

How get class object keys in flutter

I created one class as Student.
class Student{
int id;
String name;
Student({this.id,this.name});
}
Now I need to print key - id and name
For a proper way to do that you could check my code below. This is how to override the toString().
import 'package:flutter/foundation.dart';
#immutable
class Student{
final int id;
final String name;
const Student({required this.id,required this.name});
#override
String toString() {
return 'Student{id: $id, name: $name}';
}
}
Then you could print like this:
Student student = const Student(id: 1, name: 'John');
print(student);
print(student.id);
print(student.name);
Create a toJson method inside your Student class, and print out your parameters.
class Student{
int id;
String name;
Student({this.id,this.name});
Map<String, dynamic> toJson() => {
if(id!= null) "Id": id,
if(name != null) "Name": name,
};
}
then,
Student exampleStudent= Student(id: '001', name: "john"); //feed values
print(exampleStudent.toJson());

Can I use MongoRepository to sort in the order of the score?

I'm going to sort the search results in the database in the order of the score.
However, I could not find a way to sort using mongoRepository, so I asked a question.
Is there a way to sort by socre using MongoRepository?
This is my Entity
#Data
#Document(collection = "cad")
public class Cad {
#Id
private String id;
private String author;
private String mainCategory;
private String subCategory;
private String title;
private String index;
private String s3Url;
private String createdAt;
public Cad(String author, String mainCategory, String subCategory, String title, String index, String s3Url, String createdAt) {
this.author = author;
this.mainCategory = mainCategory;
this.subCategory = subCategory;
this.title = title;
this.index = index;
this.s3Url = s3Url;
this.createdAt = createdAt;
}
}
And This is my Repository
public interface CadRepository extends MongoRepository<Cad, String> {
List<Cad> findAllByTitleOrIndexOrMainCategoryOrSubCategoryContains(String text1, String text2, String text3, String text4);
}
And This is my Service
#RequiredArgsConstructor
#Service
#Component
public class CadService {
private final CadRepository cadRepository;
public List<Cad> searchCadFile(String searchText) {
try {
List<Cad> list = cadRepository.findAllByTitleOrIndexOrMainCategoryOrSubCategoryContains(searchText, searchText, searchText, searchText);
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
I tried #Aggregate Annotation, but I didn't get the result I wanted.
Any hint would be appreciated. Thank you :)
You can add a org.springframework.data.domain.Sort parameter to your repository method:
List<Cad> findAllByTitleOrIndexOrMainCategoryOrSubCategoryContains(String text1, String text2, String text3, String text4, Sort sort);
And when calling your repository method you can add the property / properties you want to sort on:
List<Cad> list = cadRepository.findAllByTitleOrIndexOrMainCategoryOrSubCategoryContains(searchText, searchText, searchText, searchText, Sort.by("index"));
Or you can add the property to the method name:
List<Cad> findAllByTitleOrIndexOrMainCategoryOrSubCategoryContainsOrderByIndexAsc(String text1, String text2, String text3, String text4);
Things are more explained in detail here: https://www.baeldung.com/spring-data-sorting

method in ArangoRepository extension class using COLLECT in query annotation to group by and count not working

I have a simple node like this below
#Document("users")
public class User {
#Id // db document field: _key
private String id;
#ArangoId // db document field: _id
private String arangoId;
private String firstName;
private String lastName;
private String country;
public User() {
super();
}
public User(String id) {
this.id = id;
}
public User(String id, String country) {
this.id = id;
this.country = country;
}
// getter & setter
#Override
public String toString() {
return "User [id=" + id + ", name=" + firstName + ", surname=" + lastName + "]";
}
public String getId() {
return id;
}
}
here is the repository class but the method getListOfCountryAndNumUsers returns null even though i have inserted users with different countries into the database.
public interface UserRepository extends ArangoRepository<User, String> {
#Query("FOR u IN users COLLECT country = u.country WITH COUNT INTO length RETURN
{\"country\" : country, \"count\" : length }")
Iterable<CountryAndNumUsers> getListOfCountryAndNumUsers();
}
I think the problem could be with the the syntax of my query in the query annotation. I didnt see any direct example of using collect operation in the spring data arango db part of arangodb documentation here but I saw the collect operation in the section "high level operations" of arangoDb documentation here
Please Help. Thanks. !
So I discovered my error. It was in a class I didn't add in the question. That is the class for the return object of the method getListOfCountryAndNumUsers()
i.e class CountryAndNumUsers.
public class CountryAndNumUsers {
private String country;
private Integer numberOfUsers;
public CountryAndNumUsers(String country, Integer numberOfUsers) {
this.country = country;
this.numberOfUsers = numberOfUsers;
}
public String getCountry() {
return country;
}
public Integer getNumberOfUsers() {
return numberOfUsers;
}
}
so there was a mapping mismatch since the query returns an object with different field names. I changed the query to this below so that it matches
#Query("FOR u IN users COLLECT country = u.country WITH COUNT INTO length RETURN {\"country\" : country, \"numberOfUsers\" : length }")

Adding List<object> to class

New programmer here, I'm trying to add a List object to a list within another class but it keeps giving me an error.I've been stuck on this problem for hours now
void main()
{
List<Hobbies> hobby;
Hobbies hobbies = new Hobbies("Football");
hobby.add(hobbies);
User user1 = new User("Harris", 22, hobby);
print(user1.getAge());
}
class User
{
String name;
int age;
List<Hobbies> hobbies;
User(String name, int age, List<Hobbies> hobbies)
{
this.name = name;
this.age = age;
this.hobbies = hobbies;
}
getAge()
{
print(name);
}
}
class Hobbies
{
String name;
Hobbies(String name)
{
this.name = name;
}
getAge()
{
print(name);
}
}
The error i keep getting
TypeError: C.JSNull_methods.add$1 is not a functionError: TypeError: C.JSNull_methods.add$1 is not a function
You mixed a lot of things here, but let's try to unwrap it all one by one. :)
So few things regarding naming conventions to make your life easier:
Don't use plurals for object names if they are representing one item, and not a list of something.
Always use plurals for property of type list.
Getter methods should always return a type, if you miss that part, you won't see compile time errors your upper project has at the moment trying to print variables instead of returning values, and than again printing in main file for 2nd time...
If you follow those principles, you would get your objects looking like this
class User {
String name;
int age;
List<Hobby> hobbies;
User(String name, int age, List<Hobby> hobbies) {
this.name = name;
this.age = age;
this.hobbies = hobbies;
}
int getAge() => print(name);
}
class Hobby {
String name;
Hobby(String name) {
this.name = name;
}
String getName() => this.name;
}
After this is sorted, let's approach adding data and initialising those objects:
void main() {
List<Hobby> hobbies = [Hobby("Football")];
User user1 = new User("Harris", 22, hobbies);
print(user1.getAge().toString());
}
You need to initialize a List, otherwise it is null and null has no add, so do this:
final hobby = List<Hobbies>();
// or final List<Hobbies> hobby = [];
// or final hobby = <Hobby>[];
Hobbies hobbies = new Hobbies("Football");
hobby.add(hobbies);
final user1 = User("Harris", 22, hobby);

dropdownchoice with choicerenderer, how to get values

i have problem trying to get some values. this is my situation (wicket)
i have a dropdownchoice study_template, i don't have problem populating the DDC, the problem is when i try to get some value (id or name). this is the code
ChoiceRenderer choiceRenderer = new ChoiceRenderer("name", "id");
study_template = new DropDownChoice( "study_template",new PropertyModel( this, "selectedTemplate" ),template_names , choiceRenderer);
template_names is a list< SelectOption > with the values obtain from a DB. this works OK.
this is the class that i use to populate the DDC
public class SelectOption implements java.io.Serializable {
private long id;
private String name;
public SelectOption(long id, String name ) {
this.id = id; this.name=name;
}
public long getId()
{return id; }
public String getName()
{return name; }
}
normally i can get the value with study_template.getModelObject(), but in this case it doesn't work, i don't have any ideas about to obtain the id and the name , i know that i need GETID() and GETNAME(), but i don't know how to use it, any help will be appreciated
You could use something as below:
public class SpikePage extends WebPage {
class Person {
String id;
String name;
public Person(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public SpikePage() {
Person employee = new Person("E001", "ABC");
Person manager = new Person("E002", "XYZ");
List personList = new ArrayList(2);
personList.add(employee);
personList.add(manager);
Form form = new Form("form");
final DropDownChoice personDropDownChoice = new DropDownChoice("person", new ArrayList(personList), new IChoiceRenderer() {
#Override
public Object getDisplayValue(Person object) {
return object.getId().concat("-").concat(object.getName());
}
#Override
public String getIdValue(Person object, int index) {
return object.getId();
}
});
personDropDownChoice.setOutputMarkupId(true);
personDropDownChoice.setNullValid(false);
form.add(personDropDownChoice);
final Label label = new Label("name");
label.setOutputMarkupId(true);
form.add(label);
personDropDownChoice.add(new AjaxFormComponentUpdatingBehavior("onchange") {
#Override
protected void onUpdate(AjaxRequestTarget target) {
Person selectedPerson = personDropDownChoice.getModelObject();
label.setDefaultModelObject("Hi ".concat(selectedPerson.getName()));
target.add(label);
}
});
add(form);
}
}
Thanks for your answer, i already get it work
i use this
private SelectOption selectedTemplate;
and then
selectedTemplate.getId();