簡體   English   中英

如何留下 function? 飛鏢/顫振

[英]How can I leave a function? Dart/Flutter

嘿,我有一個帶有 onPressed 的 ElevatedButton。 onPressed 內部是一些 if 語句,可確保一切正確,之后我想將這些信息推送到 firebase。 如果不滿足條件,我該如何離開 onPressed function? 我嘗試返回並返回 null,返回 0 但我的應用程序只是凍結。

ElevatedButton(
    onPressed: () async {
      if (date != null) {
        print(date);
      } else {
        dialog.showAlertDialog(context, 'Oops', 'You didn\'t choose a date!');
        return;
      }

      if (time != null) {
        print(time);
      } else {
        dialog.showAlertDialog(context, 'Oops', 'You didn\'t choose a time!');
        return 0;
      }

      if(combinedDateAndTime.isBefore(DateTime.now())) {
        dialog.showAlertDialog(context, 'Oops', 'Your date and time is in the past');
        return null;
      }
      
      _firestore.collection('foo').add({
        'dateTime': combinedDateAndTime,
      });
    },
    style: raisedButtonStyle,
    ),
  ),

我只是不知道如何離開 function。 我將不勝感激任何幫助。

一種方法是定義一個 boolean 變量並檢查它是否為真,然后調用 firestore.collection,如下所示:

bool var = true;

if (date != null) {
    print(date);
  } else {
    dialog.showAlertDialog(context, 'Oops', 'You didn\'t choose a date!');
    var = false;
  }

  if (time != null) {
    print(time);
  } else {
    dialog.showAlertDialog(context, 'Oops', 'You didn\'t choose a time!');
    var = false;
  }

  if(combinedDateAndTime.isBefore(DateTime.now())) {
    dialog.showAlertDialog(context, 'Oops', 'Your date and time is in the past');
    var = false;
  }

  if (var == true){

  _firestore.collection('foo').add({
    'dateTime': combinedDateAndTime,
  });
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM