try { await FirebaseFirestore.instance .collection('users') .doc(userID).collection('personalInfo') .add(_insertProfInfoToDBToDB.toJson());
这个功能是由sunmit按钮触发的,现在的问题是,如果用户没有互联网连接,例如,他提交了这个表单,那么他将得到一个错误,即没有来自另一个功能的互联网连接
Future<void> _tryConnection() async { try { final response = await InternetAddress.lookup('Example.com'); setState(() { _isConnectionSuccessful = response.isNotEmpty; }); } on SocketException catch (e) { print(e); setState(() { _isConnectionSuccessful = false; }); } if (_isConnectionSuccessful != true){ Utils.showSnackBar("Please check your internet connection"); } }
因此,一旦用户连接到互联网,就会在“personalInfo”集合中同时创建多个文档。
使用setData
Firestore.instance. collection('users'). document('userID').collection('collection name').document('docId') set(data, merge: true)
现在,如果它不存在,则创建一个新条目,如果它存在,则更新它
从文档中
setData(映射<字符串,动态>数据,{bool-merge:false})未来 写入此DocumentReference引用的文档。如果该文档尚不存在,则将创建该文档。 。如果“合并”为true,则提供的数据将合并到现有文档中,而不是覆盖;