已初始化传递给void函数的空列表



我正试图将一个列表传递给另一个类。我在我的构造函数中初始化了我的列表:

class _MyFormState extends State {
List<String> arrayOfKeywords = [];

在我的代码中,列表被成功填充,然后我需要将其传递给一个void函数:

void createPerson(List<String> arrayOfKeywords) async {

但在我的void函数中,列表是空的,flutter取的是初始化的列表,而不是更新的列表。我该怎么解决?

完整代码:


class MyForm extends StatefulWidget {
@override
_MyFormState createState() => _MyFormState();
}
class _MyFormState extends State {
final _formKey = GlobalKey();
final _newPerson = Person();
// initialize an empty array
List<String> arrayOfKeywords = [];

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Profile')),
body: Container(
padding:
const EdgeInsets.symmetric(vertical: 16.0, horizontal: 16.0),
child: Builder(
builder: (context) => Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
TextFormField(
decoration:
InputDecoration(labelText: 'Nom'),
validator: (value) {
if (value.isEmpty) {
return 'Please enter your first name';
}
},
onChanged: (val) =>
setState(() => _newPerson.firstName = val),
),
TextFormField(
decoration:
InputDecoration(labelText: 'Last name'),
validator: (value) {
if (value.isEmpty) {
return 'Please enter your last name.';
}
},
onChanged: (val) =>
setState(() => _newPerson.lastName = val)),

Container(
padding: const EdgeInsets.symmetric(
vertical: 16.0, horizontal: 16.0),
child: RaisedButton(
onPressed: () {
globals.update("nv_personne_nom", (value) => _newPerson.lastName);
globals.update("nv_personne_prenom", (value) => _newPerson.firstName);
print(_newPerson.lastName);
print(_newPerson.firstName);
//Building keywords, starting with *
String buildkeywords = "'*'";
//Getting the string length
int strlength = _newPerson.lastName.length;
print(strlength);
int i = 2;
//starting loop
while (i <= strlength) {
// building list
buildkeywords = buildkeywords +"," + "'" + _newPerson.lastName.substring(0, i) + "'";

i = i + 1;
};
//Making a list from the built keywords

List<String> arrayOfKeywords = buildkeywords.split(',');
print (arrayOfKeywords);
},
child: Text('check')
)
),
Container(
padding: const EdgeInsets.symmetric(
vertical: 16.0, horizontal: 16.0),
child: RaisedButton(
onPressed: () {
createPerson(arrayOfKeywords);
},
child: Text('save')
)
),
]
)
)
)
)
);
}
_showDialog(BuildContext context) {
Scaffold.of(context)
.showSnackBar(SnackBar(content: Text('Submitting form')
)
);
}
}
void createPerson(List<String> arrayOfKeywords) async {

final databaseReference = FirebaseFirestore.instance;
DocumentReference ref = await databaseReference.collection("personnes")
.add({
// 'personne': '',
// 'categorie': '',
// 'Intervenant': '',
// 'Intervenant': (globals["utilisateur"]),
// 'nom': (globals["lieu"]),
'prenom': (globals["nv_personne_prenom"]),
'nom': (globals["nv_personne_nom"]),
//'keywords': keywords,
});

print(ref.id);
}


List<String> arrayOfKeywords = buildkeywords.split(',');

这将创建一个列表,一个与您要写入的变量同名的本地变量。

这将设置您的现有列表:

arrayOfKeywords = buildkeywords.split(',');

最新更新