Chad
Chad

Reputation: 685

Flutter - how to make an array of objects

Edit:

class Transactions {
  final String id;

  final double amount;
  final String date;
  

  const Transactions({
    @required this.id,
    @required this.amount,
    @required this.date,
   
  });}

I am trying to ad a transaction to the current month . When the user adds a transaction based on the current moth is I would like to add that transaction to the array,e

so I want to make a list of list of Transactions

this is what i have tried :

  List<List> _userTransactions = [
            List<Transactions> jan= [];
            List<Transactions> feb= [];
            List<Transactions> mar= [];
            List<Transactions> apr= [];
            List<Transactions> may= [];
            .....
            ];

Upvotes: 0

Views: 7053

Answers (3)

GVV Nagaraju
GVV Nagaraju

Reputation: 31

Here we have a class with name as Transactions So to make the list of Transactions objects we have to create list which is of type Transactions In the below code part we created a List of type Transactions and added objects to them by creating them with the help of new variable

List<Transactions> _userTransactions = [
        new Transactions(100, 5000, "4-10-2020"),
        new Transactions(101, 5000, "4-11-2020")
        
        ];

for more information about creation of objects and adding them to the List

please click the link https://kodeazy.com/flutter-array-userdefined-class-objects/

Upvotes: 0

Anis R.
Anis R.

Reputation: 6922

If all you want is to initialize a list of hard-coded integers, you can do:

var list = [
    [1, 2, 3],
    [4, 5, 6]
  ];

You can specify the type instead of var if you want, it is List<List<int>>.

Or, if you want to generate a grid, you can either use a for loop with the add function, or use generate as per mra9776's answer.

Upvotes: 0

mra9776
mra9776

Reputation: 26

Edit:

already answered

you might want try this one:

final grid = List<List<int>>.generate(
      size, (i) => List<int>.generate(size, (j) => i * size + j));

Upvotes: 1

Related Questions