【问题标题】:Extract only year from complete date in Flutter仅从 Flutter 中的完整日期中提取年份
【发布时间】:2021-04-14 12:59:13
【问题描述】:

这是我的 json 数据结构:

[
   [
    {
     "nos": 0,
     "name": "A S MUSIC AND DANCE A CULTURAL ORGANIZATION",
     "unique_id": "AN/2020/0259067",
      "reg_details": [
                       {
                        "registered_with": "Registrar of Societies"
                        },
                        {
                        "type_of_ngo": "Registered Societies (Non-Government)"
                         },
                            {
                            "registration_no": "1534"
                              },
                           {
                              "copy_of_registration_certificate": "Available"
                           },
                            {
                              "copy_of_pan_card": "Available"
                             },                    
                              {
                               "date_of_registration": "25-05-2016"
                              }
],

我只想从日期键值与 年份 比较,即"date_of_registration": "25-05-2016"

从小部件中获取值的我的按钮是:

    RoundedButton(
                      text: "Next",
                      press: () async {
                        debugPrint(_currentValue.toString());
                        _NgoDetail(ngo,_currentValue.toString());
                       //Where _currentValue is actually the year I'm getting 
                        //from a spinner widget

                      },
                    ),

与这些值进行比较的函数是:

  Future<bool>  _NgoDetail(String name,String year) async { //Only Year as a 
                                                //  parameter and not whole date
    String jsonString = await _loadANgoAsset();
    final jsonResponse = json.decode(jsonString);
    int k=-2;
   
    List<dynamic> allNamesOfNGO=jsonResponse[0];


    allNamesOfNGO.takeWhile((value) => value != name).forEach((allNamesOfNGO){
      (allNamesOfNGO as Map<String, dynamic>).forEach((key, value) {
       ++k;
        if(key=='name' && value == name){
           if(key=='date_of_registeration' && value == year) //Here value is 
                                                          //complete date, I want 
                                                          //it to be year only
           {
            Navigator.push(context,MaterialPageRoute(builder: (context)=> R3(jsonIndexOfNGO:k)));
           }                          
        }
        else{
          Fluttertoast.showToast(
              msg: "Your given details doesn't match. Please try again",
              toastLength: Toast.LENGTH_SHORT,
              gravity: ToastGravity.CENTER,
              timeInSecForIosWeb: 3,
              backgroundColor: Colors.red,
              textColor: Colors.white,
              fontSize: 16.0);
        }

      } );
    });

  



  }

我应该先在上面的函数中做什么?从 json 中检索所有日期值并将其格式从 25-02-2015 转换为 2015 或执行其他方法?

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    试试这个

        List<dynamic> allNamesOfNGO=jsonResponse[0];
    
    
        allNamesOfNGO.takeWhile((value) => value != name).forEach((allNamesOfNGO){
          (allNamesOfNGO as Map<String, dynamic>).forEach((key, value) {
           ++k;
            if(key=='name' && value == name){
               if(key=='date_of_registeration' && value.substring(6) == year) //Here value is 
                                                              //complete date, I want 
                                                              //it to be year only
               {
                Navigator.push(context,MaterialPageRoute(builder: (context)=> R3(jsonIndexOfNGO:k)));
               }                          
            }
            else{
              Fluttertoast.showToast(
                  msg: "Your given details doesn't match. Please try again",
                  toastLength: Toast.LENGTH_SHORT,
                  gravity: ToastGravity.CENTER,
                  timeInSecForIosWeb: 3,
                  backgroundColor: Colors.red,
                  textColor: Colors.white,
                  fontSize: 16.0);
            }
    
          } 
    

    【讨论】:

    • 参数已经是年份格式。我的意思是它的 2015 年或 2016 年等。我想从 API 中提取和比较值,使得它在函数 _NgoDetail(String name, String Year) 中只有一年而不是 25-02-2015。请再次编辑您的答案。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2016-08-02
    • 1970-01-01
    • 1970-01-01
    • 2018-10-05
    • 2012-09-08
    • 2021-05-24
    • 2018-06-18
    相关资源
    最近更新 更多