How to add class to list in dart? - flutter

class CCC {
int num;
CCC(this.num) : this.num = num;
}
void main() {
List list = [];
CCC c1 = CCC(10);
CCC c2 = CCC(20);
print(list);
}
I really don't know. A two-dimensional array is needed in conjunction with the db, but unless you use a class in the list, isn't there an array?

as your question is not clear what you want to ask but what I understand is you want to add class object in list.... For this you have can specify list type then add object of class to it. like,
class CCC{
final int num;
CCC(this.num);
}
void main(){
// specify the list type
List<CCC> list = [];
CCC c1 = CCC(10);
CCC c2 = CCC(20);
list.add(c1);
list.add(c2);
// here you can print all items' property
for (var item in list){
print(item.num);
}
}
I hope I understood your question right

Related

Java object creation storage space

Given the following code:
class cl
{
int aa = 5;
String st = “Top“;
}
class c2
{
public static void main ()
}
c1 jj = new c1();
}
How is st stored in jj ?
Just as a Reference To the actual String „Top“ or is the Full String Stores within jj ?
Thanks for any help.

Using List to store and find class element with random member variable

I want to store a class in a List to later retrieve the value. Now the problem I
m having is that one of the members is a random value. So when trying to retrieve the entry I can't find it, since I can't produce the 'key'.
I'm happy for suggestions on implementing this in a different way.
So this is the example class I want to store in a list:
class MyDevice{
int device;
int randomNumber;
void set(int device, int random){
this.device = device;
this.randomNumber = random;
}
}
class Handling{
final List<MyDevice> myDevicesList = new List<MyDevice>();
...
...
MyDevice dev = new MyDevice();
dev.set(device, random);
//Store entry into List
myDevicesList.add(dev);
...
...
Now I want to delete or possibly replace an entry, but I will have no way of
finding it since the random value is not known after storing above.
// Note that I have the device value supplied in a call-back, for the sake of this
// example let's just define a method for removing
void removeEntry(int device){
MyDevice dev = new MyDevice();
dev.set(device, );
myDevicesList.remove(dev);
}
How can I find / remove an entry that is a class and I only want to 'search' the list with one of the embers in the class that is stored?
Is the only solution to walk the list and make my own compare method to identify the entry by the 'device' member?
All you need is the removeWhere List method. You have to remove where MyDevice.device matches the argument, device.
Here's the code you requested for
void removeEntry(device) {
myDevicesList.removeWhere((element) => element.device == device);
}
Here's the full code.
import 'dart:math';
class MyDevice {
int device;
int randomNumber;
void setValue(int device, int random) {
this.device = device;
this.randomNumber = random;
}
}
class Handling {
final List<MyDevice> myDevicesList = new List<MyDevice>();
void addToList(device, random) {
MyDevice dev = new MyDevice();
dev.setValue(device, random);
myDevicesList.add(dev);
}
void removeEntry(device) {
myDevicesList.removeWhere((element) => element.device == device);
}
List<MyDevice> get list {
return myDevicesList;
}
}
void main() {
final handle = new Handling();
handle.addToList(2, Random().nextInt(5));
handle.addToList(18, Random().nextInt(4));
handle.addToList(12, Random().nextInt(9));
print("List before ${handle.list.map((element) {
return [element.device, element.randomNumber];
})}");
handle.removeEntry(18);
print("List after ${handle.list.map((element) {
return [element.device, element.randomNumber];
})}");
}
The output is
List before ([2, 1], [18, 0], [12, 7])
List after ([2, 1], [12, 7])
Here's the solution on dartpad
You don't know random value, so you have to manually compare and remove item from list.
Following code may help you.
In following example i added 1,2 and 5 and remove 2 after that you can see in print only 1 and 5.
class MyDevice {
int device;
int randomNumber;
void set(int device, int random) {
this.device = device;
this.randomNumber = random;
}
}
class Delet2 extends StatefulWidget {
#override
_Delet2State createState() => _Delet2State();
}
class _Delet2State extends State<Delet2> {
final List<MyDevice> myDevicesList = new List<MyDevice>();
#override
void initState() {
super.initState();
add(1);
add(2);
add(5);
delete(2);
for (int k = 0; k < myDevicesList.length; k++) {
print(myDevicesList[k].device);
}
}
add(int number) {
MyDevice dev = new MyDevice();
dev.set(number, Random().nextInt(1000));
myDevicesList.add(dev);
}
delete(int number) {
for (int i = 0; i < myDevicesList.length; i++) {
if (myDevicesList[i].device == number) {
myDevicesList.removeAt(i);
}
}
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(),
);
}
}

Is it possible to make an Array of Objects in Dart/Flutter

Hello I am wondering if it is possible to make an array of objects in dart/flutter.
class Student {
int math;
}
Student[] studentArray = new
Student[7];
studentArray[0] = new Student();
void main() {
List persons = [User("foo"), User("bar")]; //non-empty on create
List users = []; //blank initially
for (int i = 0; i < 5; i++) {
users.add("User $i");
}
//print using iterator
for (var u in users) {
print(u);
}
/*
* first element can be accessed using users[0] or users.first
* last element can be accessed using users.last
* */
}
class User {
String name;
User(this.name);
}
Sure thing
import 'dart:collection';
class MyClass {
List<MyObjectClass> myList = [];
}
To make array of objects in dart we have List.
In your scenario to get array of Student type objects we define List of type student and add the objects of student inside the List. Below is the sample example code of adding Student objects into a List.
List<Student > StudentDetails = [new Student (),new Student ()];
for more information about how to add objects into list refer to the link
https://kodeazy.com/flutter-array-userdefined-class-objects/

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)
}

C++ Accessing variables in a function in one class from a different class

I'm trying to code a program with multiple classes such the one of the class reads the variables from a text file and the other classes use these variables for further processing.
The problem I'm facing is that I'm having trouble passing the variables from one class to another class, I did try "friend" class and also tried to use constructors but failed
to get the desired output.
The best I could do was
suppose I have class 1 and class 2, and I have a variable "A=10" declared and initialised in class 1, with the help of constructor I inherit it in class 2;
when I print it in class 1, it gives a correct output as 10 but when I print it in class 2 it gives an output as 293e30 (address location)
Please guide me on how to this.
Class1
{
public:
membfunc()
{
int A;
A = 10;
}
}
Class2
{
public:
membfunc2()
{
int B;
B = A + 10;
}
membfunc3()
{
int C, D;
C = A + 10;
D = B + C;
}
}
If i print variables, i expect to get
A = 10, B = 20, C = 20, D = 40
But what I get is
A = 10, B=(252e30) + 10
I think your problem was that you were defining local variables in your member functions, instead of creating member variables of a class object.
Here is some code based on your sample to demonstrate how member variables work:
class Class1
{
public:
int A;
void membfunc()
{
A=10;
}
};
class Class2
{
public:
int B;
int C;
int D;
void membfunc2(Class1& class1Object)
{
B = class1Object.A + 10;
}
void membfunc3(Class1& class1Object)
{
C = class1Object.A + 10;
D = B + C;
}
};
(Full code sample here: http://ideone.com/cwZ6DM.)
You can learn more about member variables (properties and fields) here: http://www.cplusplus.com/doc/tutorial/classes/.