我正在嘗試使用 FutureBuilder 創建一個小部件。嘗試將 List 分配給未來時,它會引發錯誤。
@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>>?'.

下面是從 SQLite DB 中檢索 Accounts 的函式。
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();
}
我在谷歌上看過很多例子,但找不到錯誤。
uj5u.com熱心網友回復:
您的錯誤訊息有意義的唯一方法是您的專案中有兩個不同的類Account。也許一個來自包和一個你自己創建的?
無論如何,要選擇正確的,只需讓編譯器決定。省略 FutureBuilder 的顯式通用引數,它應該從它的引數中選擇正確的引數:
FutureBuilder(future: AccountService.retrieveAccounts(),),
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/408822.html
標籤:
