{"id":5232,"date":"2024-05-28T11:27:07","date_gmt":"2024-05-28T11:27:07","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=5232"},"modified":"2024-05-28T13:10:48","modified_gmt":"2024-05-28T13:10:48","slug":"flutter-dart-new-features","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/flutter-dart-new-features\/","title":{"rendered":"These New Flutter\/Dart Features Make My Life Easier"},"content":{"rendered":"\n<p>My favourite cross-platform framework has gone to the next level!<\/p>\n\n\n\n<p>Flutter is a great choice for building cross-platform applications, not only mobile but also web and desktop. You can do the same with Javascript, using React Native, Electron and other frameworks, but the unification of the codebase and the developer experience of Flutter is unbeatable, especially for solo developers like me.<\/p>\n\n\n\n<p>Also the language, Dart, is a great choice for building applications. Some say it&#8217;s just a &#8220;better Typescript&#8221;, and although I partially agree with that, I found myself more productive with Dart than with Typescript.<\/p>\n\n\n\n<p>Despite these advantages, Flutter and Dart are still young compared to other frameworks and languages. But the Flutter team has been working hard to make it better every year. <\/p>\n\n\n\n<p>This year, in 2024, they have introduced some new features that make my developer life even easier. There are many new features (Impeller enhancement, Game dev support, etc), but I want to share my personal favorite 3 features that I found very useful in my projects.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1) Google AI Dart SDK<\/strong><\/h2>\n\n\n\n<p>We&#8217;re living in the era of AI! As a solo developer who helps startups build MVPs, I often get requests to build AI-powered features from my clients. But Dart&#8217;s AI ecosystem was not as mature as Python or Javascript, so I had to build an ad-hoc solution for each project.<\/p>\n\n\n\n<p>Due to this popular demand, Google has released an <a href=\"https:\/\/github.com\/google-gemini\/generative-ai-dart\" data-type=\"link\" data-id=\"https:\/\/github.com\/google-gemini\/generative-ai-dart\" target=\"_blank\" rel=\"noopener\">AI SDK for Dart<\/a> in 2024. This SDK makes it super easy to integrate AI features into your Flutter applications, using Google&#8217;s Gemini models. <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"750\" height=\"500\" src=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2024\/05\/8ruiv7.jpg\" alt=\"\" class=\"wp-image-5239\"\/><\/figure>\n<\/div>\n\n\n<p>Here&#8217;s an example of how you can use it:<\/p>\n\n\n\n<p>First, install the package:<\/p>\n\n\n\n<p><code>$ flutter pub add google_generative_ai<\/code><\/p>\n\n\n\n<p>Then, you can use it in your code. If you have used LangChain before, you&#8217;ll find this API very similar:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;dart&quot;,&quot;mime&quot;:&quot;application\/dart&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Dart&quot;,&quot;modeName&quot;:&quot;dart&quot;}\">import 'package:google_generative_ai\/google_generative_ai.dart';\n\nfinal apiKey = Platform.environment['GOOGLE_API_KEY'];\n\nvoid main() async {\nfinal model = GenerativeModel(model: 'gemini-pro', apiKey: apiKey);\nfinal content = [\nContent.text(&quot;Explain what's the difference between Dumplings and Gyoza&quot;),\n]\n\nfinal response = await model.generateContent(content);\n\nprint(response.text);\n}<\/pre><\/div>\n\n\n\n<p>Not only can you generate text, but you can also generate images, videos, and even code snippets. The possibilities are endless.<\/p>\n\n\n\n<p>Since Google has always made it easy to interoperate with their products &#8211; Flutter, Firebase, Google Cloud, etc. &#8211; I expect they&#8217;ll also make it easy to use the AI SDK with other Google products in the future. I personally think this will broaden the use of AI in the mobile\/web development space significantly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2) Flutter meets WebAssembly<\/strong><\/h2>\n\n\n\n<p>I see many people say &#8220;<em>Don&#8217;t use Flutter for web development<\/em>&#8220;. If they are talking about &#8220;website&#8221;, then I agree. In that case, you should use HTML, CSS, and Javascript. But if they are talking about &#8220;web applications&#8221;, then I strongly recommend using Flutter. <\/p>\n\n\n\n<p>Modern web applications are getting more complex, and Flutter is a great choice for building complex web applications with decent performance. The problem is, when we want even more performance, sometimes the WebJS generated from Flutter isn&#8217;t enough.<\/p>\n\n\n\n<p>That&#8217;s why I was so excited when I heard that Flutter has added support for <a href=\"https:\/\/docs.flutter.dev\/platform-integration\/web\/wasm\" target=\"_blank\" rel=\"noopener\">WebAssembly<\/a> in 2024.<\/p>\n\n\n\n<p>WebAssembly is a new standard that allows you to run high-performance code written in languages like C, C++, and Rust in the browser. It&#8217;s a great way to speed up your web applications, especially if you have computationally intensive tasks. <\/p>\n\n\n\n<p>Unfortunately, until recent years WebAssembly did not support garbage collection, which made it hard to use with languages like Dart that rely on garbage collection. Hence, to use WebAssembly features in Flutter projects, developers had to compile C, C++, or Rust code to WebAssembly and then call it from Dart using FFI (or use bridge libraries).<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"1280\" height=\"572\" src=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2024\/05\/image1-10.png\" alt=\"Flutter WebAssembly\" class=\"wp-image-5233\" srcset=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2024\/05\/image1-10.png 1280w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2024\/05\/image1-10-768x343.png 768w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2024\/05\/image1-10-750x335.png 750w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2024\/05\/image1-10-1140x509.png 1140w\" sizes=\"(max-width: 1280px) 100vw, 1280px\" \/><\/figure>\n<\/div>\n\n\n<p>Recently WebAssembly has added support for garbage collection, and many modern browsers have implemented this new version of WebAssembly. This means now it&#8217;s possible to compile Dart code into WebAssembly, which the Flutter build system will do. <\/p>\n\n\n\n<p><strong>According to the Flutter team, Flutter web apps compiled into WebAssembly are 2x faster than the ones compiled into Javascript.<\/strong> This is a huge improvement, and it opens up a lot of possibilities for building high-performance web applications with Flutter, without using system-level languages.<\/p>\n\n\n\n<p>Of course, if you really need the best-of-the-best performance, you should still consider using C, C++, or Rust, but for most web applications, Flutter WebAssembly should be more than enough.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3) Finally, Dart macros! (experimental)<\/strong><\/h2>\n\n\n\n<p>Almost in every Flutter project that communicates with backend API, you have to write codes for JSON serialization\/deserialization and data class equality. Since these features are very common, there are dedicated packages like <code>json_serializable<\/code>, <code>freezed<\/code> to help you with that. <\/p>\n\n\n\n<p>For example, you can use <code>freezed<\/code> to generate data classes with equality and serialization\/deserialization methods:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;dart&quot;,&quot;mime&quot;:&quot;application\/dart&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Dart&quot;,&quot;modeName&quot;:&quot;dart&quot;}\">@freezed\nclass User with _$User {\nconst factory User({\nrequired int age,\nrequired String name,\nrequired String username,\n}) = _User;\n\nfactory User.fromJson(Map json) =&gt; _$UserFromJson(json);\n}<\/pre><\/div>\n\n\n\n<p>Writing only this much code + running <code>build_runner<\/code>, you can get a lot of features like equality, serialization\/deserialization, and more. The problem is that running <code>build_runner<\/code> every time you make a change in your data class can be annoying. Also, many of the times I don&#8217;t need all the generated code, but it&#8217;s kinda hassle to build my own code generator.<\/p>\n\n\n\n<p>And of course, I wasn&#8217;t the only one who had complained about this. The community has been asking for meta-programming tools better than mere code generations, and finally, they have introduced an experimental &#8220;macro&#8221; feature. Unlike code generation tools, macros are &#8220;static metaprogramming&#8221; tools, which means they run at compile-time, not at runtime. <\/p>\n\n\n\n<p>Also, you do not need to run <code>build_runner<\/code> to generate the code, because macros are fully integrated into the Dart language and executed automatically in the background by Dart tools.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>How to use Macros in Dart?<\/strong><\/h3>\n\n\n\n<p>Currently, Macros are experimental, but you can try it out.<\/p>\n\n\n\n<p>The Dart team has implemented the <code>JsonCodable<\/code> macro in Flutter&#8217;s <code>json<\/code> library, and here&#8217;s how you can use it. Make sure you have the Dart version 3.5.0-152 or later.<\/p>\n\n\n\n<p>First, you have to enable it with a flag in your <code>analysis_options.yaml<\/code> file:<\/p>\n\n\n\n<p><code>analyzer:<br>enable-experiment:<br>- macros<\/code><\/p>\n\n\n\n<p>Next, install the package:<\/p>\n\n\n\n<p><code>$ flutter pub add json<\/code><\/p>\n\n\n\n<p>Run the project with the experimental flag:<\/p>\n\n\n\n<p><code>$ dart run --enable-experiment=macros bin\/my_app.dart<\/code><\/p>\n\n\n\n<p>Then, you can use it in your code. This example shows how you can use the <code>JsonCodable<\/code> macro to generate JSON serialization\/deserialization code for your data classes:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;dart&quot;,&quot;mime&quot;:&quot;application\/dart&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Dart&quot;,&quot;modeName&quot;:&quot;dart&quot;}\">import 'package:json\/json.dart';\n\n@JsonCodable() \/\/ Macro annotation.\nclass User {\nfinal int? age;\nfinal String name;\nfinal String username;\n}\n\nvoid main() {\n\/\/ Given some arbitrary JSON:\nvar userJson = {\n'age': 5,\n'name': 'Roger',\n'username': 'roger1337'\n};\n\n\/\/ Use the generated members:\nvar user = User.fromJson(userJson);\nprint(user);\nprint(user.toJson());\n}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>How to create your own macros?<\/strong><\/h3>\n\n\n\n<p>You can create your own macros by creating a <code>macro class<\/code>. Every macro class should implement a specific macro interface. For example, if you want to create a macro that generates class declarations, you should implement the <code>ClassDeclarationsMacro<\/code> interface:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;dart&quot;,&quot;mime&quot;:&quot;application\/dart&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Dart&quot;,&quot;modeName&quot;:&quot;dart&quot;}\">macro class MyMacro implements ClassDeclarationsMacro {\n\u00a0\u00a0\u00a0const MyMacro();\n  \n\u00a0\/\/ ...\n}<\/pre><\/div>\n\n\n\n<p>You can have fun with macros by comparing the original code and the &#8220;augmented code&#8221;, which is generated code from macros in the background.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>I&#8217;ve introduced only 3 features that I found very useful in my projects, but there are many more features that Flutter and Dart teams have introduced in 2024. If you&#8217;re a game developer, you might be interested in the new &#8220;Devtools plugin for Flame&#8221;. If you are a savvy developer who really cares about performance, you might be interested in the new &#8220;Impeller enhancement&#8221;.<\/p>\n\n\n\n<p>Many people used to say &#8220;Flutter will meet its end soon, just like many other Google projects&#8221;. But I don&#8217;t think so. Google has been investing a lot in Flutter, and the ecosystem is getting better every year. And also, Google is already using Flutter in many of their products, like Google Earth. So I can say the Flutter is most likely to stay for a long time.<\/p>\n\n\n\n<p>I&#8217;m excited to see what new features Flutter and Dart teams will introduce in 2025. What were your favorite Flutter\/Dart features in 2024? And what features do you expect in 2025? Let me know in the comments!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I talked out some of the best new features introduced in Flutter or Dart, including  Google AI Dart SDK,  WebAssembly, and Macros.<\/p>\n","protected":false},"author":25,"featured_media":5235,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jnews-multi-image_gallery":[],"jnews_single_post":null,"jnews_primary_category":{"id":"","hide":""},"footnotes":""},"categories":[42],"tags":[82,278,277,202],"class_list":["post-5232","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-trending","tag-coding","tag-dart","tag-flutter","tag-programming"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/5232","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/users\/25"}],"replies":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/comments?post=5232"}],"version-history":[{"count":4,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/5232\/revisions"}],"predecessor-version":[{"id":5248,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/5232\/revisions\/5248"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/5235"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=5232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=5232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=5232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}