Dart函数返回`Set`而不是`bool`。

huangapple go评论78阅读模式
英文:

Dart Function Returning Set<bool> Instead of bool

问题

I'm building a mobile client for a blog with a paid CMS that shows a number of articles all the time, plus a rotating article each week, and I've built a simple function to get the current week of the year and return a Boolean value if an article should be displayed this week:

 bool displayArticle(StoredArticle article){
    if (article.week < 0) {
      return true;
    }

    DateTime now = DateTime.now();
    DateTime janFirst = DateTime(now.year, 1, 1);

    int weekNum = (now.difference(janFirst).inDays / 7).ceil();
    if(article.week == weekNum || article.week == (weekNum - 1)){
      return  true;
    }

    return false;
}

I then use this function to filter a list of all the articles like so:

List<StoredArticle> articlesToDisplay = storedArticleObjs.where((article) {
        return displayArticle(article);
      }).toList();

This is all enclosed within a Stateful Widget.

However, using the function like this throws an error at the function call that "The return type 'Set' isn't a 'bool', as required by the closure's context."

My first thought was that there was an issue with the displayArticle() function being a static member function to a stateful widget, but moving the function directly into the closure as follows did not impact the error.

List<StoredArticle> articlesToDisplay = storedArticleObjs.where((article) {
        if (article.week < 0) {
      return true;
    }

    DateTime now = DateTime.now();
    DateTime janFirst = DateTime(now.year, 1, 1);

    int weekNum = (now.difference(janFirst).inDays / 7).ceil();
    if(article.week == weekNum || article.week == (weekNum - 1)){
      return true;
    }

    return false;
      }).toList();

Next, I thought it might be that the early return was confusing the inspector to believe it was returning multiple values, so I converted it to a single return function as follows, but that did nothing either.

 bool displayArticle(StoredArticle article){
    bool shouldDisplay = false;
    if (article.week < 0) {
      shouldDisplay = true;
    }

    DateTime now = DateTime.now();
    DateTime janFirst = DateTime(now.year, 1, 1);

    int weekNum = (now.difference(janFirst).inDays / 7).ceil();
    if(article.week == weekNum || article.week == (weekNum - 1)){
      shouldDisplay = true;
    }

    return shouldDisplay;
}

The only resources on similar issues have been referring to functions that return Future<T> instead of T. Putting aside the fact that my issue is with a Set<T> rather than a Future<T>, those errors have all been thrown by the return statement or the function definition rather than the function call.

I haven't been able to find any resources relating to this specific issue, though as I'm new to Flutter and Dart, I suppose I could be missing some specific terminology.

That being said, returning a set of the return type does not make any sense to me. Is this a quirk of implementation in a Stateful Widget?

英文:

I'm building a mobile client for a blog with a paid CMS that shows a number of articles all the time, plus a rotating article each week, and I've built a simple function to get the current week of the year and return a Boolean value if an article should be displayed this week:

 bool displayArticle(StoredArticle article){
    if (article.week &lt; 0) {
      return true;
    }

    DateTime now = DateTime.now();
    DateTime janFirst = DateTime(now.year, 1, 1);

    int weekNum = (now.difference(janFirst).inDays / 7).ceil();
    if(article.week == weekNum || article.week == (weekNum - 1)){
      return  true;
    }

    return false;
  }

I then use this function to filter a list of all the articles like so:

List&lt;StoredArticle&gt; articlessToDisplay = storedArticleObjs.where((article) =&gt; {
        displayArticle(article)
      }).toList();

This is all enclosed within a Stateful Widget.

However, using the function like this throws an error at the function call that The return type &#39;Set&lt;bool&gt;&#39; isn&#39;t a &#39;bool&#39;, as required by the closure&#39;s context.

My first thought was that there was an issue with the displayArticle() function being a static member function to a stateful widget, but moving the function directly into the closure as follows did not impact the error.

List&lt;StoredArticle&gt; articlessToDisplay = storedArticleObjs.where((article) =&gt; {
        if (article.week &lt; 0) {
      return true;
    }

    DateTime now = DateTime.now();
    DateTime janFirst = DateTime(now.year, 1, 1);

    int weekNum = (now.difference(janFirst).inDays / 7).ceil();
    if(article.week == weekNum || article.week == (weekNum - 1)){
      return  true;
    }

    return false;
      }).toList();

Next I thought it might be that the early return was confusing the inspector to belive it was returning multiple values, so I converted it to a single return function as follows, but that did nothing either.

 bool displayArticle(StoredArticle article){
    bool shouldDisplay = false;
    if (article.week &lt; 0) {
      shouldDisplay = true;
    }

    DateTime now = DateTime.now();
    DateTime janFirst = DateTime(now.year, 1, 1);

    int weekNum = (now.difference(janFirst).inDays / 7).ceil();
    if(article.week == weekNum || article.week == (weekNum - 1)){
      shouldDisplay = true;
    }

    return shouldDisplay;

The only resources on similar issues have been referring to functions that return Future&lt;T&gt; instead of T. Putting aside the fact that my issue is with a Set&lt;T&gt; rather than a Future&lt;T&gt;, those errors have all been thrown by the return statement or the function definition rather than the function call.

I haven't been able to find any resources relating to this specific issue, though as I'm new to Flutter and Dart I suppose could be missing some specific terminology.

That being said, returning a set of the return type does not make any sense to me. Is this a quirk of implementation in a Stateful Widget?

答案1

得分: 1

问题在于您使用了太多花括号,而{&quot;A&quot;}在Dart中是设置语法。

您原来的代码是:

storedArticleObjs.where((article) =&gt; {
        displayArticle(article)
      }).

请将其更改为:

storedArticleObjs.where((article) =&gt; 
        displayArticle(article)
      ).

请注意,=&gt;函数语法不使用花括号。

您甚至可以更紧凑地编写它,使用tear-offs,如下所示:

storedArticleObjs.where(displayArticle).
英文:

The problem is that you have a few too many braces, and {&quot;A&quot;} is set-syntax in Dart.

You have:

storedArticleObjs.where((article) =&gt; {
        displayArticle(article)
      }).

Change that to:

storedArticleObjs.where((article) =&gt; 
        displayArticle(article)
      ).

Note that the =&gt; function syntax doesn't use braces.

You could even probably write it more compactly using tear-offs like so:

storedArticleObjs.where(displayArticle).

huangapple
  • 本文由 发表于 2023年2月10日 03:41:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403666.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定