Hernán Casillas
Hernán Casillas

Reputation: 79

Incrementing count inside List.map()

I have a list of weekdays var _weekdays = ['Monday', 'Tuesday','Wednesday', 'Thursday', 'Friday','Saturday','Sunday'];

And I have a list of 7 documents that I get from a Firestore query, each element is a Card... so my ListView shows 7 items with each item being equivalent to a weekday.

enter image description here

I have this function that returns a the mapped List and I just need to increment cont++ for each item on the documents list. I call this function inside my ListView so I just get a list of Widgets.

getExpenseItems(AsyncSnapshot<QuerySnapshot> snapshot, context) {
var cont=0;
var docs = snapshot.data.documents;
var favs = new List<DocumentSnapshot>();
var ind=0;

   return favs
        .map((doc) => new 
          Padding(       
            padding: const EdgeInsets.all(30),
            child: Container(
              height:100,
              width: SizeConfig.screenWidth,
              child: 
              Align(
                alignment: Alignment.centerRight,
                child:
              Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[

                  Column(
                    children: <Widget>[
                      Container(
                        height:100,
                        width: SizeConfig.screenWidth/4,
                        child:
                          Text(_weekdays[cont]+"\n\n" + doc['name'],style:TextStyle(fontSize: 19),),
                      ),
                    ],
                  ),
                  Column(
                    children: <Widget>[              
                      Container(
                        height: 100,
                        width: SizeConfig.screenWidth/4,
                        child: GestureDetector(
                          child: SizedBox(
                            width: MediaQuery.of(context).size.width,
                            //margin: EdgeInsets.symmetric(horizontal: 5.0),
                            height: 150,
                            child: Image.asset(doc['image'], fit: BoxFit.contain,)
                            //Text('text $i', style: TextStyle(fontSize: 16.0),)
                          ),
                          onTap: (){
                            Navigator.of(context).push(MaterialPageRoute(
                              builder: (BuildContext context) => ViewRecipe(
                                image: doc['image'], 
                                      titulo: doc['name'], 
                                      user: user, rating: 
                                      doc['rating'], 
                                      steps: doc['steps'],
                                      recipeId: doc['id'],
                                      )));
                                  },
                                ),

                        ),

                      ],
                  ),
                ],
              ),
            ),
          ),
        )
  ).toList();
}

I'd appreciate any help! Thank you.

Upvotes: 1

Views: 698

Answers (1)

lazos
lazos

Reputation: 1075

Instead of fat arrow (=>)

   return favs
        .map((doc) => new 
          Padding(  

if you write

return favs.map((doc){ 
    cont +=1;
    print('cont: $cont); //check the result
    return new Padding( 
...
} // 

probably will do the job

Upvotes: 2

Related Questions