The argument type 'Future<>' can't be assigned to the parameter type ''

Issue

I am trying to build a GridView list. This all works when I create a simple list of ‘players’, but now I am trying to persist data using sqflite and I am getting an error:

Error: The argument type ‘Future’ can’t be assigned to the parameter type ‘int’

This is my list builder widget:

class PlayerList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Consumer<PlayerData>(
      builder: (context, playerData, child) {
        return GridView.builder(
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 2,
              mainAxisSpacing: 20,
              crossAxisSpacing: 20,
            ),
            itemCount: playerData.playerCount,
            itemBuilder: (BuildContext context, int index) {
              final player = playerData.players[index];
              return RawMaterialButton(
                  fillColor: Color(0x991c5597),
                  elevation: 20,
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(10),
                  ),
                  child: Padding(
                    padding: const EdgeInsets.all(10.0),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: <Widget>[
                        CircleAvatar(
                          backgroundColor: Colors.tealAccent,
                          radius: 30,
                          child: Icon(
                            Icons.star,
                            color: Color(0xFF16377a),
                            size: 30,
                          ),
                        ),
                        Text(player.name, style: smallTitleText),
                      ],
                    ),
                  ),
                  onPressed: () {
                    playerData.makeActive(player.name);
                    playerData.printList();
                    Navigator.pushNamed(context, PlayScreen.id);
                  });
            });
      },
    );
  }
}

This is how I get the list count:

Future<int> get playerCount async {
  final Database db = await getDBConnector();
  final List<Map<String, dynamic>> maps = await db.query('players');
  if (maps.length != null) {
    return maps.length;
  } else { return 0;}
}

Solution

Wrap your GridView with FutureBuilder

Something like this

return FutureBuilder(
       future: playerData.playerCount,
       builder : (context,snapshot){
         if(!(snapshot.hasData)){
          return Container();
         }
         else{
           return GridView.builder(
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2,
          mainAxisSpacing: 20,
          crossAxisSpacing: 20,
        ),
        itemCount: snapshot.data,
        itemBuilder: (BuildContext context, int index) {
          final player = playerData.players[index];
          return RawMaterialButton(
              fillColor: Color(0x991c5597),
              elevation: 20,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(10),
              ),
              child: Padding(
                padding: const EdgeInsets.all(10.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    CircleAvatar(
                      backgroundColor: Colors.tealAccent,
                      radius: 30,
                      child: Icon(
                        Icons.star,
                        color: Color(0xFF16377a),
                        size: 30,
                      ),
                    ),
                    Text(player.name, style: smallTitleText),
                  ],
                ),
              ),
              onPressed: () {
                playerData.makeActive(player.name);
                playerData.printList();
                Navigator.pushNamed(context, PlayScreen.id);
              });
         });
         },
       );
      }
    }
);

Answered By – Siddharth jha

Answer Checked By – Senaida (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.