【问题标题】:How to send existing data to firestore?如何将现有数据发送到 Firestore?
【发布时间】:2021-07-10 21:06:13
【问题描述】:

在 Flutter 中,我使用 raisebutton 将数据发送到 firestore, 如何在发送数据之前设置条件(当用户推送 raisedbutton 时,如果某些字段为空或设备未连接到互联网,应用程序不会将数据发送到我的 Firestore 数据库)?

我的代码:

  1. 上网功能:
 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';
      });
    }
  }
....
  1. 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


    【解决方案1】:

    您必须从 check() 函数返回一个布尔值:

      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;
      }
    

    并在发送数据前测试onPressed回调中check()函数的返回值:

      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();
                        },
                      )
                    ],
                  );
                },
              );
            }
      
          }
        },
    
    

    让我知道它是否有效。

    【讨论】:

    • 我有这个错误:一个否定操作数必须有一个静态类型的'bool'。 (non_bool_negation_expression at [myproject] lib\screens\Adding.dart:274) in if (!check()) 。
    • 抱歉,我没有看到 check() 返回 Future。你必须使用await check() 来获取Future 的值,我已经更新了代码。让我知道它是否有效。
    • 谢谢你的代码工作,但在离线模式下,当设备连接到互联网时,仍然会向firestore发送数据,我认为条件 if (!(await check())) 应该在 async { 之前。 ..}(如果没有互联网,用户不能发送任何数据)但我不知道该怎么做?
    • 只需删除await check() 之前的!。我已经更新了代码。它应该可以工作。
    • 非常感谢,终于成功了,如果可能的话,我有一个关于连接的最后一个问题,我可以更改功能以检测设备是否在不 ping google.com 的情况下连接?
    猜你喜欢
    • 1970-01-01
    • 2020-05-17
    • 2020-09-25
    • 1970-01-01
    • 2020-02-26
    • 1970-01-01
    • 1970-01-01
    • 2020-01-07
    • 1970-01-01
    相关资源
    最近更新 更多