我有一個代表按鈕的自定義小部件,該小部件位于“lib”檔案夾內名為“Widgets”的單獨檔案夾中,這就是按鈕小部件檔案
import 'package:flutter/material.dart';
Widget buttonWidget(String text, Function action) {
return ElevatedButton(
onPressed: () => action,
child: Text(text, style: const TextStyle(fontSize: 20),
);
}
現在在 main.dart 檔案中,我希望 buttonWidget 使用 Navigator.pushNamed 函式,所以我呼叫了 buttonWidget 就像
buttonWidget("Navigate", Navigator.pushNamed(context, '/screenTwo')),
但是 buttonWidget 呼叫給了我這個錯誤
The argument type 'Future<Object?>' can't be assigned to the parameter type 'Function'.
uj5u.com熱心網友回復:
您需要圍繞呼叫創建一個匿名函式,就像直接將其傳遞給onPressed引數一樣。
buttonWidget("Navigate", () => Navigator.pushNamed(context, '/screenTwo')),
或者
buttonWidget("Navigate", () {
Navigator.pushNamed(context, '/screenTwo');
}),
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/387079.html
