'How to send many forms at one time to firebase with flutter?
I'm a Flutter programmer and I'm stuck halfway to completing my app, I created the following code that allows the user to add the number of units he wants, then it sends all the information to firebase at one time, the code works correctly but I have one problem when the user sends the form I receive a many form in the firebase. How do I change this code so that the user can send the information he entered to firebase at one time ?
Any suggestion or code would be greatly appreciated.
My code
class _UnitesPageState extends State<UnitesPage> with TickerProviderStateMixin {
var _myWidgets = <Widget>[];
int _index = 0;
final unitesnumber = [
' unite 1 ',' unite 2 ',' unite 3 ',
];
bool autoValidate = true;
GlobalKey<FormBuilderState> _formKey = GlobalKey<FormBuilderState>();
final Map<int, String> namesvalues = Map();
final Map<int, String> unitesvalues = Map();
final List<TextEditingController> _namesControllers = [];
final List<TextEditingController> _unitesControllers = [];
void _add() {
int keyValue = _index;
_myWidgets = List.from(_myWidgets)
..add(Container(
child: Column(
children: <Widget>[
_names(keyValue),
_unites(keyValue),
],
),
));
setState(() => ++_index);
}
@override
Widget build(BuildContext context) {
final bottomNavigationBar = Container(
child: Row(
children: <Widget>[
FlatButton.icon(
label: const Text(' SEND',
)),
onPressed: () async {
setState(() {
autoValidate = !autoValidate;
});
for (var i = 0; i < _myWidgets.length; i++) {
FirebaseFirestore.instance.collection('data').add({
"names": _namesControllers[i].text,
"unites":_unitesControllers[i].text,
});
}
}
),
],
),
);
return Scaffold(
// appBar
bottomNavigationBar: bottomNavigationBar,
body:Padding(
child: Form(
autovalidateMode: AutovalidateMode.disabled,
key: _formKey,
child: Stack(
children: <Widget>[
Column(
children: <Widget>[
_buildfaculte(),
Expanded(
child: SizedBox(
child: ListView(
addAutomaticKeepAlives: true,
children: _myWidgets,
),
),
),
],
)
],
),),
),
);
}
Widget _names(int keyValue) {
TextEditingController controller = TextEditingController();
_namesControllers.add(controller);
return FormBuilderTextField(
name: 'names',
controller: controller,
);
}
Widget _unites(int keyValue) {
TextEditingController controller = TextEditingController();
_unitesControllers.add(controller);
return Container(
alignment: Alignment.center,
child: Autocomplete<String>(
optionsBuilder: (TextEditingValue value) {
return unitesnumber.where((suggestion) =>
suggestion.toLowerCase().contains(value.text.toLowerCase()));
},
onSelected: (value) {
setState(() {
_selectedUnites = value;
});
},
),
);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
