Lalit Chattar
Lalit Chattar

Reputation: 1984

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

I am trying to create a widget using FutureBuilder. When trying to assign List to future, it is throwing an error.

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Accounts'),
      ),
      body:FutureBuilder<List<Account>>(future: AccountService.retrieveAccounts(),),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Navigator.push(context,
              MaterialPageRoute(builder: (context) => const AddAccount()));
        },
        child: const Icon(Icons.add),
      ),
    );
  }

The argument type 'Future<List<Account>>' can't be assigned to the parameter type 'Future<List<Account>>?'.

enter image description here

Below is the function which retrieves Accounts from SQLite DB.

 static Future<List<Account>> retrieveAccounts() async {
    final db = await SQLHelper.db();
    var queryResult =
        await db.query('ACCOUNT', orderBy: "id");
    return queryResult.map((e) => Account.fromMap(e)).toList();
  }

I have seen many examples on google but could not find the mistake.

Upvotes: 1

Views: 978

Answers (1)

nvoigt
nvoigt

Reputation: 77304

The only way your error message makes sense is if you have two different classes named Account in your project. Maybe one from the package and one you created yourself?

Anyway, to pick the correct one, just let the compiler decide that. Omit the explicit generic parameters for the FutureBuilder and it should pick up the correct one from it's parameters:

FutureBuilder(future: AccountService.retrieveAccounts(),),

Upvotes: 1

Related Questions