我想调用一个模型后,我点击支付按钮,我得到这个错误
type 'Null' is not a subtype of type 'PaymentViewModel' of 'function result".
检查下面的代码,请纠正我的错误。
这是我的付款表单页面
class PayfastPayment extends StatefulWidget {
PayfastPayment({Key? key}) : super(key: key);
@override
_PayfastPaymentState createState() => _PayfastPaymentState();
}
class _PayfastPaymentState extends State<PayfastPayment> {
TextEditingController amountController = TextEditingController();
TextEditingController itemNameController = TextEditingController();
late PaymentViewModel model;
var client = http.Client();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(centerTitle: true, title: const Text("Payment Page")),
body: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
TextFormField(
controller: amountController,
decoration: const InputDecoration(
border: UnderlineInputBorder(),
labelText: 'Amount',
),
),
const SizedBox(
height: 100,
),
TextFormField(
controller: itemNameController,
decoration: const InputDecoration(
border: UnderlineInputBorder(),
labelText: 'Item Name',
),
),
SizedBox(
width: 220,
height: 100,
child: InkWell(
onTap: () {
print(
"Amount: ${amountController.text} Item: ${itemNameController.text}");
model.payment(amountController.text, itemNameController.text);
},
child: Container(
alignment: Alignment.center,
color: Colors.blue,
child: const Center(
child: Text("Pay",
style: TextStyle(fontSize: 22, color: Colors.white))),
),
),
),
],
),
);
}
}
错误在onTap method: model.payment(amountController.text, itemNameController.text);
这一行这是我想调用的视图模型页面
class PaymentViewModel {
final TextEditingController amountController = TextEditingController();
final TextEditingController itemNameController = TextEditingController();
late API api;
String? errorMessage;
late String payFast;
void payment(String amount, String item_name) {
String? paymentErrorMessage = null;
if (paymentErrorMessage != null) {
errorMessage = paymentErrorMessage;
} else {
amount = amountController.text;
item_name = itemNameController.text;
api
.payFastPayment(amount: amount, item_name: item_name)
.then((createdPayment) {
if (createdPayment == null) {
errorMessage = "Something went wrong. Please try again.";
} else {
payFast = createdPayment;
}
print("It reaches here");
}).catchError((error) {
errorMessage = '${error.toString()}';
});
}
}
}
您在paymentModel中的条件检查之前分配它并将其分配给字符串错误消息String? paymentErrorMessage = null;
删除这一行同时移除amount = amountController.text;
线。你的方法应该像这样
void payment(String amount, String item_name) {
item_name = itemNameController.text;
api
.payFastPayment(amount: amount, item_name: item_name)
.then((createdPayment) {
if (createdPayment == null) {
errorMessage = "Something went wrong. Please try again.";
} else {
payFast = createdPayment;
}
print("It reaches here");
}).catchError((error)
errorMessage = '${error.toString()}';
});
}