【发布时间】:2021-07-10 21:06:13
【问题描述】:
在 Flutter 中,我使用 raisebutton 将数据发送到 firestore,
如何在发送数据之前设置条件(当用户推送 raisedbutton 时,如果某些字段为空或设备未连接到互联网,应用程序不会将数据发送到我的 Firestore 数据库)?
我的代码:
- 上网功能:
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 代码(从文本字段和 DropdownMenuItem 中收集数据):
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))),
【问题讨论】:
标签: firebase flutter dart google-cloud-firestore