I've spend the day stuck on this so I thought I could ask for a little help: I need to write an array in firestore, from a string in flutter.
Basicaly I have MyString = "a,ab,abc,abcd" in flutter. I need to send that to firestore in a new array, each comma in the string separates the values:
#Array
- a
- ab
- abc
- abcd
Any idea how I could achieve that ?
Full code:
class MyForm extends StatefulWidget {
@override
_MyFormState createState() => _MyFormState();
}
class _MyFormState extends State {
final _formKey = GlobalKey();
final _newPerson = Person();
@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.fromLTRB(0, 50, 0, 20),
// child: Text('Subscribe'),
// ),
// // SwitchListTile(
// title: const Text('Monthly Newsletter'),
// value: _newPerson.newsletter,
// onChanged: (bool val) =>
// setState(() => _newPerson.newsletter = val)),
// // Container(
// padding: const EdgeInsets.fromLTRB(0, 50, 0, 20),
// child: Text('Interests'),
// ),
// CheckboxListTile(
// title: const Text('Cooking'),
// value: _newPerson.passions[Person.PassionCooking],
// onChanged: (val) {
// setState(() =>
// _newPerson.passions[Person.PassionCooking] = val);
// }),
// CheckboxListTile(
// title: const Text('Traveling'),
// value: _newPerson.passions[Person.PassionTraveling],
// onChanged: (val) {
// setState(() => _newPerson
// .passions[Person.PassionTraveling] = val);
// // }),
// CheckboxListTile(
// title: const Text('Hiking'),
// value: _newPerson.passions[Person.PassionHiking],
// onChanged: (val) {
// setState(() =>
// _newPerson.passions[Person.PassionHiking] = 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);
//construction des keywords
String buildkeywords = "";
//calcul longueur du nom
int strlength = _newPerson.lastName.length;
print(strlength);
int i = 0;
while (i <= strlength) {
print(_newPerson.lastName.substring(0, i));
buildkeywords = buildkeywords + "," + _newPerson.lastName.substring(0, i);
//globals.update("keywords",(value) => (buildkeywords));
i = i + 1;
};
//Making a list from the built keyword
//globals.update("keywords",(value) => (buildkeywords));
List<String> arrayOfKeywords = buildkeywords.split(',');
},
child: Text('check')
)
),
Container(
padding: const EdgeInsets.symmetric(
vertical: 16.0, horizontal: 16.0),
child: RaisedButton(
onPressed: () {
createPerson;
},
child: Text('save')
)
),
])))));
}
_showDialog(BuildContext context) {
Scaffold.of(context)
.showSnackBar(SnackBar(content: Text('Submitting form')
)
);
}
}
final databaseReference = FirebaseFirestore.instance;
void createPerson() async {
/* await databaseReference.collection("Personnes")
.document("BAX")
.setData({
'prenom': (globals["nv_personne_prenom"]),
'nom': (globals["nv_personne_nom"]),
'keyword': (globals["keyword"]),
});*/
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': arrayOfKeywords,
});
print(ref.id);
}