<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: oganao</title>
    <description>The latest articles on DEV Community by oganao (@oganao).</description>
    <link>https://dev.to/oganao</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F973551%2Fedecbd66-3029-4655-b323-f960087fdac4.png</url>
      <title>DEV Community: oganao</title>
      <link>https://dev.to/oganao</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/oganao"/>
    <language>en</language>
    <item>
      <title>[Not solved]Difference among value, sync, microtask and async on Future</title>
      <dc:creator>oganao</dc:creator>
      <pubDate>Sat, 24 Dec 2022 08:12:33 +0000</pubDate>
      <link>https://dev.to/oganao/not-solveddifference-among-value-sync-microtask-and-async-on-future-2k34</link>
      <guid>https://dev.to/oganao/not-solveddifference-among-value-sync-microtask-and-async-on-future-2k34</guid>
      <description>&lt;p&gt;I am still learning Dart. Now I am focusing on Future this week. I made toy codes to understand Future. Syntax of Future is fine for me, but I still don't comprehend Future in detail.&lt;/p&gt;

&lt;p&gt;For example, following snippets seem to be the same, but they are not.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Future.value&lt;/code&gt; and &lt;code&gt;Future.sync&lt;/code&gt; destruct a local value, but &lt;code&gt;Future.microtask&lt;/code&gt; and &lt;code&gt;async&lt;/code&gt; function do not.&lt;/p&gt;

&lt;p&gt;I guess &lt;a href="https://www.reddit.com/r/dartlang/comments/pmdkzh/serious_future_vs_futurevalue_vs_futuresync_vs/"&gt;a reddit article&lt;/a&gt; could give me a clue. But it seems to me this is not the case for me.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void main() async{
  print('--work01--');
  await work01();
  print('--work02--');
  await work02();
  print('--work03--');
  await work03();
  print('--work04--');
  await work04();
}

work01() async {
  int c = 2;
  Future&amp;lt;int&amp;gt; x = Future.value(++c); // 3-&amp;gt;3
  print(c);
  print(await x);
}

work02() async {
  int c = 2;
  Future&amp;lt;int&amp;gt; x = Future.sync(()=&amp;gt;++c); // 3-&amp;gt;3
  print(c);
  print(await x);
}

work03() async {
  int c = 2;
  Future&amp;lt;int&amp;gt; x = Future.microtask(()=&amp;gt;++c); // 2-&amp;gt;3
  print(c);
  print(await x);
}

work04() async {
  Future&amp;lt;int&amp;gt; foo(int x) async {
    return ++x;
  }
  int c = 2;
  Future&amp;lt;int&amp;gt; x = foo(c); // 2-&amp;gt;3
  print(c);
  print(await x);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Console&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;--work01--
3
3
--work02--
3
3
--work03--
2
3
--work04--
2
3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>dart</category>
    </item>
    <item>
      <title>A compile error of await for in Dart</title>
      <dc:creator>oganao</dc:creator>
      <pubDate>Sat, 17 Dec 2022 07:44:39 +0000</pubDate>
      <link>https://dev.to/oganao/a-compile-error-of-await-for-in-dart-23de</link>
      <guid>https://dev.to/oganao/a-compile-error-of-await-for-in-dart-23de</guid>
      <description>&lt;p&gt;People love dart programming language because of its easy-to-use asynchronous feature. I definitely agree with it.&lt;/p&gt;

&lt;p&gt;I came across the following error the other day. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The type 'Stream' used in the 'for' loop must implement Iterable. (view docs)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;My code snippest was :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void main() async {

  for(String v in countStream('a b c d')) { // *** compile error ***
    print(v);
  };

 }

Stream&amp;lt;String&amp;gt; countStream(String text) async* {
  var words = text.split(' ');
  for (int i = 0; i &amp;lt;= words.length; i++) {
    await Future.delayed(const Duration(milliseconds: 300));
    yield words.getRange(0, i).join(' ');
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You'll soon notice I forgot "await" before "for". But I couldn't figure out that. I went the wrong way.&lt;/p&gt;

&lt;p&gt;The compiler requested Iterable, so I was wondering if Stream is Iterable and googled how to make Stream into Iterable. It was a wast of time.&lt;/p&gt;

&lt;p&gt;In dart, "await for" is said to be &lt;a href="https://dart.dev/guides/language/specifications/DartLangSpec-v2.10.pdf#Asynchronous%20For-in" rel="noopener noreferrer"&gt;"Asynchronous For-in"&lt;/a&gt; in the language specification. It is different from "for", which is used for asynchronous.&lt;/p&gt;

&lt;p&gt;The compiler can't infer which feature a programmer intends to use, even thought one codes async*.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>Applying reduce to a list with no element. Oops.</title>
      <dc:creator>oganao</dc:creator>
      <pubDate>Thu, 17 Nov 2022 01:55:21 +0000</pubDate>
      <link>https://dev.to/oganao/applying-reduce-to-a-list-with-no-element-oops-1l0e</link>
      <guid>https://dev.to/oganao/applying-reduce-to-a-list-with-no-element-oops-1l0e</guid>
      <description>&lt;p&gt;I have the following code. Getting datetimes from a list of a model, removing a datetime if the date is today, and gaining the max value, is simple functional programming. It works fine, but sometimes 'No element' exception occurs.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;br&gt;
var maxDate = list&lt;br&gt;
//removing today's data.&lt;br&gt;
.where((e) =&amp;gt; !Util.isSameDay(e.datetime, DateTime.now()))&lt;br&gt;
.map((e) =&amp;gt; e.datetime)&lt;br&gt;
// getting the max value.&lt;br&gt;
.reduce((v, e) =&amp;gt; v.isAfter(e) ? v : e);&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;At first, I didn't figure out what the problem is. I spent some time trying to solve it.&lt;/p&gt;

&lt;p&gt;The reason is simple. The reduce document says&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The iterable must have at least one element&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, following the above code, we sometimes have a list with no element before we apply the reduce function.&lt;/p&gt;

&lt;p&gt;I know how to use the reduce function, but I completely forgot the specification when I coded.&lt;/p&gt;

&lt;p&gt;I use Android Studio, which is really awesome, but I hope the code inspector warns of such the code.&lt;/p&gt;

</description>
      <category>dart</category>
      <category>flutter</category>
    </item>
  </channel>
</rss>
