初始化表单中的输入以提交



我正试图通过我的flutter应用程序发送一个API帖子:

用户通过一个文本字段插入log_weight,该文本字段位于表单内的table中。表的每一行都是来自一个列表的迭代。

在我的flutter应用程序中,我创建了一个表,通过表单内的列表迭代。所以我试图从每次迭代中获得日志提交。

我的api_service .dart:

class APIService {
static var client = http.Client();
Future<http.Response> addLog(
int logWeight) async {
var url = Uri.parse(Config.apiURL +
Config.userAddlogAPI);
final response = await http.post(url, headers: {
HttpHeaders.authorizationHeader:
'Token xxxxxxxxxxxxxxxxxx',
}, body: {
'log_weight': logWeight,
});
}

下面是exercises.dart:

class Exercises extends StatefulWidget {
@override
_ExercisesState createState() => _ExercisesState();
}
class _ExercisesState extends State<Exercises> {
late Map<String, int> arguments;
late int logWeight;

格式如下:

Builder(builder: (context) {
return Form(
key: key,
child: Table(
children: [
const TableRow(children: [
Text(
'',
style: TextStyle(
fontSize: 15,
fontWeight:
FontWeight.bold,
color: Colors.black,
),
),
Text(
'Weight',
style: TextStyle(
fontSize: 15,
fontWeight:
FontWeight.bold,
color: Colors.black,
),
),
Text(
'New Weight',
style: TextStyle(
fontSize: 15,
fontWeight:
FontWeight.bold,
color: Colors.black,
),
),
Text(
'Submit',
style: TextStyle(
fontSize: 15,
fontWeight:
FontWeight.bold,
color: Colors.black,
),
)
]),
// Iterate over the breakdowns list and display the sequence information
for (var breakdown in snapshot
.data![index].breakdowns)
TableRow(children: [
Text(
'${breakdown.order}',
style: const TextStyle(
fontSize: 15,
color: Colors.black,
),
),
Text(
'${breakdown.weight}',
style: const TextStyle(
fontSize: 15,
color: Colors.black,
),
),
TextFormField(
decoration:
InputDecoration(
border:
InputBorder.none,
),
style: const TextStyle(
fontSize: 15,
color: Colors.black,
),
validator: (value) {
if (value == null) {
return 'Please enter a valid number';
}
return null;
},
onSaved: (value) {
logWeight =
int.parse(value!);
},
),
OutlinedButton(
onPressed: () async {
final Map<String, int>
arguments =
ModalRoute.of(
  context)!
.settings
.arguments
as Map<String,
int>;
final int id =
arguments['id'] ??
0;
try {
if (Form.of(context)
?.validate() ==
true) {
Form.of(context)
?.save();
APIService.addLog(
id,
logWeight);
}
} catch (error) {
await showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text(
'Error'),
content: Text(error
.toString()),
actions: [
OutlinedButton(
child: Text(
  'OK'),
onPressed:
  () {
Navigator.of(
        context)
    .pop();
}, ),],);},);}},
child: Text('Submit'),
),]),],),);

我如何修复我的代码,以便我可以发送log_weight。我一直得到E/flutter ( 4353): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: LateInitializationError: Field 'logWeight' has not been initialized.

late关键字错误。迟来的变量必须初始化。尽量不要用那么多late。

解决方案1。使logWeight不可为空并初始化它。因为你的addLog方法只允许非空的logWeight值。

int logWeight = 0;

解决方案2。使logWeight为空,并且在调用api之前,检查它是否为空。(如果你的api需要logWeight参数)

int? logWeight;
---
if(logWeight != null) {
APIService.addLog(id,logWeight);
}
or use validation

解决方案3。(如果您的api不需要logWeight参数)只需使logWeight为空,并更改您的方法,以允许为空的logWeight值

int? logWeight;
---
class APIService {
static var client = http.Client();
Future<http.Response> addLog(
int? logWeight) async {
...
}

最新更新