Reputation: 57
In Flutter I used raisedbutton to send data to firestore,
how can I put the condition before sending data (when the user pushes raisedbutton
the app is not sending data to my firestore database if some fields are empty or the device is not connected to internet)?
My code:
Future<bool> check() async {
try {
final result = await InternetAddress.lookup('google.com');
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
setState(() {
internetdisponible = 'vous avez ajouté un nouveau medcin \n MERCI';
});
}
} on SocketException catch (_) {
setState(() {
internetdisponible = 'Vérifier Votre connexion internet Svp';
});
}
}
....
RaisedButton(
color: Color(0xff11b719),
textColor: Colors.white,
child: Padding(
padding: EdgeInsets.all(10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Text("Envoyer ",
style: TextStyle(fontSize: 18.0)),
],
)),
onPressed: () async {
check();
if (medicalType == null ||
validatetextfield(nomdata.text) == false ||
validatetextfield(mobiledata.text) == false ||
validatetextfield(addressedata.text) == false) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text("SVP Remplir tout les champs")));
} else {
Map<String, dynamic> data = {
"specialité": medicalType,
"Nom:": nomdata.text,
"Téléphone:": mobiledata.text,
"adresse:": addressedata.text,
"autre:": autredata.text,
};
{
var myStoredData = await readData();
FirebaseFirestore.instance
.collection(myStoredData)
.add(data);
return showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text('$internetdisponible',
style: TextStyle(fontSize: 14.0),
textAlign: TextAlign.center),
actions: <Widget>[
FlatButton(
child: Text('OK'),
onPressed: () {
Navigator.of(context).pop();
},
)
],
);
},
);
}
}
},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30.0))),
Upvotes: 0
Views: 105
Reputation: 1486
You have to return a boolean from the check()
function :
Future<bool> check() async {
var isConnected = false;
try {
final result = await InternetAddress.lookup('google.com');
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
setState(() {
internetdisponible = 'vous avez ajouté un nouveau medcin \n MERCI';
});
isConnected = true;
}
} on SocketException catch (_) {
setState(() {
internetdisponible = 'Vérifier Votre connexion internet Svp';
});
}
return isConnected;
}
And test the return value of the check()
function in the onPressed
callback before sending the data:
onPressed: () async {
if (medicalType == null ||
validatetextfield(nomdata.text) == false ||
validatetextfield(mobiledata.text) == false ||
validatetextfield(addressedata.text) == false) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text("SVP Remplir tout les champs")));
} else {
Map<String, dynamic> data = {
"specialité": medicalType,
"Nom:": nomdata.text,
"Téléphone:": mobiledata.text,
"adresse:": addressedata.text,
"autre:": autredata.text,
};
{
if ((await check())) {
var myStoredData = await readData();
FirebaseFirestore.instance
.collection(myStoredData)
.add(data);
}
return showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text('$internetdisponible',
style: TextStyle(fontSize: 14.0),
textAlign: TextAlign.center),
actions: <Widget>[
FlatButton(
child: Text('OK'),
onPressed: () {
Navigator.of(context).pop();
},
)
],
);
},
);
}
}
},
Let me know if it works.
Upvotes: 1