Kee Rushar
Kee Rushar

Reputation: 316

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

I want to get data from count as int. When i call function ItemCount.getTotalOrder() it says this error. Please help.

My separate itemcount class function :

 class ItemCount {
      static final db = FirebaseFirestore.instance.collection('Orders');
      static Future<int?> getTotalOrder() async {
        final count = await db.get().then((value) {
          return value.docs.length;
        });
        print(count);
        if (count == null) {
          return 0 ;
        } else {
          return count;
        }
      }
    }

Function call where data has datatype as int :

Expanded(
         flex: 2,
         child: _buildTile(
         title: AppString.totalOrders,
         data: ItemCount.getTotalOrder(),
         color: blueColor,
       ),

Upvotes: 1

Views: 726

Answers (1)

Shourya Shikhar
Shourya Shikhar

Reputation: 1442

ItemCount.getTotalOrder() is an async function. Which means you need to wait for the future to return data. To do this wrap your widget with a FutureBuilder.

FutureBuilder(
  future: ItemCount.getTotalOrder(),
  builder: (context, snapshot) {
    if (snapshot.hasData) {  //true when the data loading is complete from the async method
      return Expanded(
         flex: 2,
         child: _buildTile(
         title: AppString.totalOrders,
         data: snapshot.data,  //snapshot data contains the data returned from future
         color: blueColor,
       );
    } else {
      return CircularProgressIndicator();  //FutureBuilder will be displaying this widget as long as the data returned from future is null (that is, still loading)
    }
  }
)

Upvotes: 2

Related Questions