# Sailesh Dahal: full corpus > AI integration consultant helping product teams ship LLM apps, RAG, AI agents and workflow automation with evals, guardrails and observability. 24 posts. Generated 2026-11-03. Individual posts are available as markdown at https://saileshdahal.com.np/{slug}.md ============================================================================== # Claude Haiku 5.5 at work: six real jobs and what they cost URL: https://saileshdahal.com.np/claude-haiku-5-5-use-cases Published: 2026-10-08 Tags: AI, Developer Tools, llm ============================================================================== If you're adding AI to a product, two questions decide most of the design. Which model goes where, and what will it cost at your volume? Anthropic's [Haiku 5.5 launch](https://www.anthropic.com/claude-haiku-5-5) on 7 October changes both answers. This post skips the press release. It goes through the jobs where Haiku 5.5 now makes sense in a real product, what the companies testing it reported, and what each job costs at realistic volume. Two numbers frame everything below. On prompts up to 100,000 tokens, Haiku 5.5 costs $0.10 per million input tokens and $0.50 per million output tokens. That's a tenth of Haiku 4.5 and a twentieth of Sonnet 5.5. Above 100,000 tokens the price goes up five times. Anthropic's blended estimate, after its new tokenizer uses a few more tokens per task, is about 75% cheaper than Haiku 4.5. Every cost example uses the short-prompt rates and my own token assumptions, which I state each time. They ignore caching, retries and the tokenizer change, so treat them as orders of magnitude for planning, not quotes. ## 1. Support triage and live chat **The job.** Read an incoming ticket or chat message, classify it, pull the order or account, draft a reply or route it to the right person. Customers are waiting, so speed matters as much as quality. **What a company reported.** Asana ran Haiku 5.5 through the evals for its AI Teammates product, covering bug triage, project set-up and portfolio search. Against the model it uses today, Asana saw task completions over 30% faster and up to 2.5 times faster inference per agent turn. Anthropic also says Haiku 5.5 is its fastest model at standard speed. **What it costs.** Take a support desk handling 50,000 tickets a month, with 3,000 tokens of ticket, history and policy going in and a 300-token triage note coming out. That's about $22.50 a month on Haiku 5.5, $225 on Haiku 4.5 and $450 on Sonnet 5.5. Per ticket, Haiku 5.5 comes to under a twentieth of a cent. **What to measure in a pilot.** Measure misroutes, not accuracy in general. A ticket sent to billing when it belonged to fraud costs far more than the model call, so track the cases where the label was wrong and a human had to move it. ## 2. Question answering over documents **The job.** A user asks a specific question about one contract, report or filing, and the model answers from that document. It often runs on every page view, so the call count grows with traffic. **What companies reported.** AlphaSense runs its Ask in Document feature about 8 million times a week. On 400 test queries, Haiku 5.5 scored 0.84 against Haiku 4.5's 0.76, and AlphaSense called the gain statistically significant. Box reported an 11-point improvement over Haiku 4.5 at about half the latency, and named cost reports, financial summaries and weekly reviews as the work it would move. **What it costs.** Assume 8 million calls a week at 8,000 tokens in and 300 out. These are my assumptions, not AlphaSense's figures. That comes to about $7,600 a week on Haiku 5.5, against $76,000 on Haiku 4.5. At this volume the model choice is a finance decision as much as an engineering one. ![Cartoon of an accountant in a green cardigan peering through a huge magnifying glass at a tiny paper invoice on her desk. Caption: Finance opening the Haiku 5.5 invoice.](https://saileshdahal.com.np/images/posts/claude-haiku-5-5-use-cases/invoice.png) **What to measure in a pilot.** Copy AlphaSense's method. Build a fixed set of a few hundred real questions with agreed answers before you switch anything, and score both models on the same set. Without that set, "it feels better" is all you have. ## 3. CRM and data clean-up **The job.** Go through thousands of records and flag the stale deal, the duplicate contact or the account with no owner. Each decision is small. The volume is what made it expensive before. **What a company reported.** HubSpot tests smaller models on simulated portals with CRM tasks. Haiku 5.5 scored 92.8% averaged over three runs, the best HubSpot has seen on that suite. On one audit task, finding stale but ambiguous records, it was the fastest model tested, with the highest hit rate and the lowest false positive rate. **What it costs.** A nightly sweep of 200,000 records at 1,500 tokens in and 100 out costs about $40 on Haiku 5.5, or roughly $1,200 a month if you run it every night. The same sweep on Haiku 4.5 is $400 a night. That difference decides whether a nightly audit is worth running at all. **What to measure in a pilot.** False positives. A tool that flags 30% of good records as stale gets switched off by the sales team within a week. HubSpot measured this directly, and you should too. ## 4. Subagents under a bigger model **The job.** A strong model plans the work and hands narrow lookups to cheaper helpers. Read this filing and return one number. Search this folder for the config. Summarise this log. **What companies reported.** Rogo, which builds AI for finance, uses a Haiku 5.5 subagent to pull a single segment revenue line out of a 10-K while a bigger model builds the slide deck. Cognition added Haiku 5.5 as the helper model in Devin Fusion, with Opus 5.5 as the lead, and says Fusion holds a FrontierCode score of 66.2 while costing less and running faster. **What it costs.** Say a research task sends 20 subagents to read 60,000 tokens each and return 500 tokens. That's about $0.13 per task on Haiku 5.5 and $2.50 on Sonnet 5.5. Watch the 100,000-token line here. A full 10-K is often longer than that. Twenty subagents reading 120,000 tokens each at the long-prompt rate cost about $1.23. Splitting each filing into two 60,000-token chunks and running 40 subagents costs about $0.25. Chunk your inputs. **What to measure in a pilot.** Measure how often the lead model has to redo a subagent's work. If it re-reads the filing itself, you pay twice. The same pattern works in the other direction too. [A popular Claude Code tip from @polydao](https://x.com/polydao/status/2104508665101517210) is to run `/advisor fable`. Opus 5.5 does the work, and Fable 5.1 reads the session and steps in only before a plan, when the same error repeats, and before the task is called done. The expensive model stops weighing in on every step. Replies to the post point out that Fable uses the same weekly quota, so this changes where you spend your allowance rather than adding to it. ![Cartoon of an orange octopus with an Opus name badge typing on four keyboards at once, while an owl in reading glasses with a Fable badge watches from a bookshelf. Caption: On call. Speaks only when it matters.](https://saileshdahal.com.np/images/posts/claude-haiku-5-5-use-cases/advisor.png) ## 5. Browser agents for portals with no API **The job.** Plenty of real work still happens in web portals that will never get an API. Insurance claim systems, supplier portals, government tax and customs forms, old internal admin panels. A browser agent can log in, fill the form and download the receipt. **What changed.** Haiku 5.5 scores 72.4% on OSWorld 2.1 (offline subset), up from 15.7% for Haiku 4.5. Sonnet 5.5 scores 83.9%. Anthropic also added computer use and browser use to its Python and TypeScript SDKs in beta, and named Haiku 5.5 as the model it suits. **What it costs.** A 30-step portal task, with about 8,000 tokens of screenshot and context per step and a 200-token action back, costs under three cents a run. **What to watch.** A 72% benchmark score means a lot of runs still fail. Design for that. Have the agent stop and hand over to a person when a page doesn't match what it expects, and give it read-only access until you've watched it work. If it will hold real logins, read my [agent security brief](/ai-agent-security-brief) first. The This is fine dog sitting at a table in a burning room, sipping coffee. *An unsupervised browser agent with admin logins. GIF via [GIPHY](https://giphy.com/gifs/this-is-fine-QMHoU66sBXqqLqYvGO).* ## 6. Compaction and summaries in long agent sessions **The job.** Long agent sessions fill their context window. Something has to summarise the history so the main model can keep working. Anthropic lists compaction as a target job for Haiku 5.5. **What it costs.** Compacting an 80,000-token session into a 4,000-token summary costs about one cent on Haiku 5.5. Anthropic also halved Sonnet 5.5's cache-read price to $0.10 per million tokens, from $0.20. A long agent session rereads its cached context on every turn, so cache reads are often the largest line on the bill. Anthropic estimates Sonnet 5.5 is now about 20% cheaper on most agentic work. A Sonnet lead with Haiku handling compaction is now a much cheaper setup than it was last week. ## Where Haiku 5.5 doesn't belong A take has been going around since the launch that Haiku now works like Sonnet, and Sonnet like Opus. For the jobs above, that's roughly fair. For writing code, Anthropic's own table says otherwise: | Benchmark | Haiku 5.5 | Sonnet 5.5 | | --- | ---: | ---: | | GDPval-AA v2.1, knowledge work (Elo) | 1620 | 1840 | | OSWorld 2.1, computer use | 72.4% | 83.9% | | Terminal-Bench 4.0, agentic coding | 39.2% | 70.6% | ![Cartoon of a tiny paper kite on a beach straining to pull a huge cargo ship stacked with file containers. Caption: Haiku 5.5 on a 40-file refactor.](https://saileshdahal.com.np/images/posts/claude-haiku-5-5-use-cases/wrong-job.png) Keep it away from three kinds of work: - **Planning and writing code across many files.** Terminal-Bench is 39% against 71%. Anthropic's launch page says Sonnet and Opus remain the better choice here. - **Long inputs by default.** Above 100,000 tokens the price goes up five times. Chunk documents, or check whether Sonnet's cached reads come out cheaper for your workload. - **Decisions nobody reviews.** Refunds, credit limits, anything with legal weight. A cheap model makes cheap mistakes faster. Put a person or a stronger model on the final call. Here's how I'd route a typical product today: | Job | Model | Why | | --- | --- | --- | | Support triage, classification, routing | Haiku 5.5 | Fast, cheap, narrow decisions | | Q&A over one or a few documents | Haiku 5.5 | High volume, short prompts | | CRM and data clean-up | Haiku 5.5 | Volume, with false positives tracked | | Lookups and summaries for a lead model | Haiku 5.5 | Subagent work, chunked under 100k | | Browser tasks on portals with no API | Haiku 5.5, with a human fallback | Many short steps | | Planning, coding, multi-step reasoning | Sonnet 5.5 or Opus 5.5 | Haiku is far behind on coding | | The final call on high-stakes output | Opus 5.5 or a person | Mistakes cost more than tokens | ## How I'd run a two-week pilot 1. **Pick one job from the list above.** Choose the one with the highest volume and the clearest right answer, usually triage or document Q&A. 2. **Build the test set first.** Collect 300 to 500 real inputs with agreed answers. AlphaSense used 400. 3. **Run in shadow mode.** Haiku 5.5 runs next to your current model on live traffic, but users only see the current model's output. Log both. 4. **Measure the right things.** Track the error that costs you money (misroutes, false positives), p95 latency, and the real bill per thousand calls. Measure the bill after the tokenizer change, not from the price table. 5. **Switch with a fallback.** Move traffic over, and send anything the model is unsure about to the bigger model or a person. Fry from Futurama shouting Shut up and take my money. *GIF via [GIPHY](https://giphy.com/gifs/money-want-futurama-e3C4pNKkr9rji).* ## You may not have to pay for the pilot Anthropic is giving subscribers a monthly Claude Platform API credit, rolling out this week. Max 5x gets $100, Max 20x gets $200, and Team plans get up to $500 pooled across users. The credit works with any model, and the [Help Center article](https://support.claude.com/en/articles/17154008) has the details. At Haiku 5.5's short-prompt rate, $100 covers a billion input tokens, which is more than most pilots use. For startups, Anthropic [expanded Claude Startups](https://claude.com/resources/articles/were-expanding-the-claude-startups-program-to-help-founders-build) on 6 October. Companies founded in the last five years, or funded in the last two, can apply. The package is a free year of Claude Team for up to five Premium seats, a one-time $1,000 API credit, and partner discounts that Anthropic values at up to $45,000 at list price. Most teams won't use every offer, so the real value is lower. ## Two more things worth knowing this week **GPT-6 now runs on every ChatGPT account.** OpenAI's [GPT-6 for everyone](https://openai.com/index/gpt-6-for-everyone/) went out on 7 October, with free and Go accounts moving to GPT-6 Luna from 8 October. The product change that matters is Intelligent UI. ChatGPT can now answer with charts, forms and small working tools, such as a bill splitter, inside the chat. If your product is a single-purpose calculator or form, your users can now get a rough version from ChatGPT in one prompt. The pricing angle matters too. Haiku 5.5's short-prompt API rates are the same as GPT-6 Luna's, which I listed in [the DevDay post](/openai-devday-2026), and on every row where Anthropic includes Luna, Haiku 5.5 scores higher. Those are Anthropic's tests, so run your own. **Marketing teams can make launch videos with Opus 5.5.** [Raphael Aubry spent three days](https://x.com/RaphaelAubryy/status/2104502744010629269) making product videos with Opus 5.5 in Claude Code, without After Effects or a video model. Opus writes a web page where a `seek(t)` function draws the exact frame for time `t`, a headless browser captures every frame, and ffmpeg joins them. His process advice is the useful part. Review four still frames before rendering, find the music drop by measuring bass energy, and have Opus score contact sheets of its own frames. His own launch video took five versions, and he is promoting his product, Howseen, in the same post. The skill is open source at [howseen-ai/claude-motion-design](https://github.com/howseen-ai/claude-motion-design). ## The short version Haiku 5.5 doesn't replace your best model. It makes a set of jobs cheap enough to run on every ticket, every document and every record, where before you had to sample or skip them. Those are the jobs I'd start with, in this order: triage, document Q&A, data clean-up, then subagents. If you want help picking the first one, [get in touch](/contact). ## Questions ### What is Claude Haiku 5.5 good for? Short, high-volume, well-defined jobs: support triage, answering questions over a few documents, cleaning CRM records, lookups for a bigger model, browser tasks on portals with no API, and compacting long agent sessions. Asana, HubSpot, AlphaSense, Box, Rogo and Cognition all reported results on jobs like these at launch. ### Should I replace Sonnet with Haiku 5.5? Not for planning or writing code. On Terminal-Bench 4.0 Haiku 5.5 scores 39% against Sonnet 5.5's 71%, and Anthropic itself says Sonnet and Opus remain the better choice for complex agentic coding. Use Haiku 5.5 under Sonnet or Opus as a subagent, and keep the bigger model as the lead. ### How much does Claude Haiku 5.5 cost? For prompts up to 100,000 tokens it costs $0.10 per million input tokens and $0.50 per million output tokens. Above 100,000 tokens the rates are five times higher, at $0.50 and $2.50. Anthropic estimates the average saving against Haiku 4.5 at about 75%. --- *Checked on 8 October 2026. Customer results are as Anthropic published them on the launch page, and benchmark scores are Anthropic's and OpenAI's own. I haven't reproduced either. Cost examples are my own arithmetic on list prices with the token counts stated, ignoring caching, retries and the tokenizer change. The Claude Code tips come from posts on X, not from Anthropic.* ============================================================================== # OpenAI DevDay 2026: cheaper models, always-on agents, and a smaller $200 plan URL: https://saileshdahal.com.np/openai-devday-2026 Published: 2026-09-30 Tags: AI, Developer Tools, llm ============================================================================== OpenAI held DevDay in San Francisco on 29 September, and there was a lot to take in. A cheaper model, a faster Astra, agents that keep working after you close the tab, and a pile of new ways to build inside ChatGPT. There was also a catch, and it went out the night before the keynote. The $200 Pro plan is reopening, but it now buys about half as much. Here's what shipped, what it costs, and what I'd check before you change anything. [OpenAI's recap](https://openai.com/index/devday-2026-recap/) has the full list if you want it. ## GPT-6.1 Sol is the new everyday model Sol 6.1 is an upgrade to GPT-6 Sol. OpenAI says it's better at coding, computer use, and factual answers, and that it gets close to Astra on several of its evaluations. Those are OpenAI's own tests, so try it on your own work before you swap models. You can use it now in ChatGPT Work, Codex, and the API as `gpt-6.1-sol`. It isn't in regular Chat yet. Here are the standard short-context API prices, in USD per million tokens: | Model | Input | Cached input | Cache writes | Output | | --- | ---: | ---: | ---: | ---: | | GPT-6 Astra | $10 | $1 | $12.50 | $50 | | GPT-6.1 Sol | $2 | $0.10 | $2.50 | $10 | | GPT-6 Luna | $0.10 | $0.01 | $0.125 | $0.50 | Sol costs a fifth of Astra for input and output. That's the comparison the launch means by "one-fifth". It doesn't mean your existing Sol bill just dropped by 80%. The token rate is also only part of what an agent costs you. Retries, tool calls, and long context all add up, so compare the cost of a finished task. Prices are from the [API pricing page](https://developers.openai.com/api/docs/pricing). I covered the Astra launch itself in [the previous AI Week](/ai-week-2026-09-04). ## Astra Ultrafast costs six times as much Ultrafast is a premium speed tier for Astra. OpenAI says it generates up to 300 tokens per second, up to 8 times faster in Codex and up to 6 times faster in the API. A Sol 6.1 version is coming later. It costs $60 input and $300 output per million tokens, six times the standard Astra rate. That can be worth it when you're sitting in a coding session waiting on every reply. It does much less for overnight jobs, or for time spent in builds, tests, and review. Faster tokens don't make your whole task eight times faster. On subscriptions, only the new Pro 500 plan includes Ultrafast. Buying extra credits on Pro 100 or Pro 200 won't unlock it, and workspaces that need their data processed outside the US can't use it at all. ## Dots are agents that keep working Dots were the headline product. Each one is an Astra-powered agent with its own cloud computer. You connect your apps, give it a goal, and come back later to see what it did, instead of starting every task from scratch. In the keynote they showed up as colourful cartoon blobs. The [dots announcement](https://openai.com/index/introducing-dots/) has the details. Pro and Business Premium get them first. Enterprise, Edu, and Healthcare admins have to switch the beta on. Your first dot is included. Talking to it is free, but the work it does in Work and Codex comes out of your usage allowance. The part I'd pay most attention to is permissions. When a dot researches on its own, it only gets read-only tools. It can't send messages or edit your apps unless your rules allow it. That's the right default. An agent with unclear authority is one more thing you have to watch, which is the problem I wrote about in [my agent security brief](/ai-agent-security-brief). ### The name was already taken Within hours, people found that `dot.com` redirects to [x.ai/bot](https://x.ai/bot). That's the download page for Grok Bot, the agent app from Elon Musk's SpaceXAI, the company xAI is now part of. A post pointing out that SpaceXAI owns the domain passed 3 million views that evening. It probably wasn't a planned troll, though. [Business Insider](https://www.businessinsider.com/openai-dots-name-causing-tons-confusion-spacexai-grok-cant-wait-2026-9) found that the domain changed owners on 28 July, months before the launch. It also turned up a Chinese AI group called Dots Studio and a pending US trademark application for DOTS. Planned or not, the most obvious address for OpenAI's new product sends people to a rival's download page. ## ChatGPT is becoming a place to build The rest of the keynote was about keeping your work inside ChatGPT. Here's the short version: | Area | What's new | | --- | --- | | Coding | Codex cloud environments, a voice-enabled CLI, Code Review, and Codex Security Cloud | | APIs | A Decisions API preview, computer use in the Agents API, and AWS Bedrock Managed Agents | | Collaboration | ChatGPT Space, Pages, shared tasks, and collaborative slides in the coming weeks | | Workplace | @ChatGPT in Slack and Teams, plus a Meetings plugin beta on macOS | | Distribution | Plugins, shareable profiles, Sign in with ChatGPT, and the OpenAI Marketplace | | Privacy | Private Intelligence, Private Safety Processing, and a Private Inference preview planned for fall | A few of these are previews, so check the [weekly release notes](https://learn.chatgpt.com/docs/whats-new/september-28-october-2-2026) before you plan around one. Codex Security Cloud, for example, is a research preview. Simon Willison's [live blog](https://simonwillison.net/2026/Sep/29/openai-devday-2026-live-blog/) caught some developer details the recap skips. The Sites session demoed SQLite storage, scheduled tasks, and shared editing, and the security session announced an open-source Codex Security CLI. Put together, this is OpenAI trying to own more of your working day. If you can find a plugin, hand it work, and use the result without leaving ChatGPT, you have less reason to go anywhere else. For builders that means more reach, and more dependence on OpenAI's rules for permissions, discovery, and billing. ## The $200 plan now buys half as much About ten hours before the keynote, Thibault Sottiaux, who leads Codex and ChatGPT Work at OpenAI, [posted on X](https://x.com/thsottiaux/status/2104823812042940713) that the $200 Pro plan was reopening with new usage maths. By his own maths, the plan now works out to half the API spend the old one gave you. [The Next Web](https://thenextweb.com/news/openai-devday-pro-200-usage-cut-pro-500-plan) and [daily.dev](https://daily.dev/posts/openai-halves-pro-200-usage-and-launches-a-500-chatgpt-plan-at-devday) report what that means from 30 October: - Work and Codex usage drops from 20 times the Plus allowance to 10 times. - GPT-6 Pro chat messages drop from 200 a week to 100. - Current subscribers keep their old limits until 29 October and get a one-time $2,500 usage credit that expires at the end of the year. - The five-hour limit isn't coming back. The price stays at $200. OpenAI's [help page](https://help.openai.com/en/articles/9793128-about-chatgpt-pro-tiers) confirms the smaller allowance "to reflect our increasingly efficient models". The exact multipliers come from the reporting above. Sottiaux's argument is that cheaper models make up the difference. In the same post he says OpenAI cut GPT-6 Sol and GPT-6 Luna to half their previous price that week. If your week runs on Sol or Luna, half the budget at half the price buys about the same amount of work, so for you it's roughly even. If you use Astra, it's a real cut. Astra's price didn't change, so half the budget buys half as much Astra. Plenty of subscribers say Astra is why they pay $200 in the first place. It's hard not to connect this to the load Astra caused. OpenAI paused new $200 sign-ups on 10 September, a day after Sottiaux warned that Astra demand was far beyond anything OpenAI had seen. The other problem is that nobody outside OpenAI can check the maths. OpenAI has never published what the plan is worth in API dollars, so "half" is half of a number only OpenAI knows. Subscribers noticed. The replies were quick to point out that a plan sold as 20 times Plus is now 10 times Plus, and the story trended on X and drew 90 comments on [Hacker News](https://news.ycombinator.com/item?id=49889306). ## About the worldwide reset During the keynote, OpenAI reset usage for users worldwide, not just the people in the room. Willison's live blog records the button press at 10:53 Pacific. Check your own account, but treat it as a one-off. It says nothing about the allowance you'll get from next month. ## What I'd do this week - Run the same few real tasks on Sol 6.1 and Astra. Compare quality, total cost, time, and retries. - If you're on Pro 200 and use mostly Astra, plan for about half as much after 30 October. Note your current usage now and read the transition email. - Try Ultrafast only on work where you're actually waiting on the model. - Start a dot on one recurring task you can check, and set its limits before you connect more apps. There's good engineering in this launch. Cheaper strong models and agents that keep going will change what a small team can take on. My one ask is that if OpenAI wants its plans judged on what they get done, it should publish what a plan actually buys. ## Questions ### What is a dot in ChatGPT? A dot is an Astra-powered agent with its own cloud computer and an ongoing goal. You connect your apps, give it a goal, and check back on its progress. Pro and Business Premium include one. Talking to it doesn't use your ChatGPT conversation limits, but the tasks it runs in Work and Codex do. ### Is Ultrafast available outside the US? No. Workspaces that require inference residency outside the United States can't use Ultrafast. Among Pro plans, only Pro 500 includes it, and at launch buying credits on Pro 100 or Pro 200 doesn't unlock it. --- *Checked on 30 September 2026. Performance and speed figures are OpenAI's own, and I haven't reproduced them. The Pro 200 multipliers and credit grant come from The Next Web and daily.dev. Rollouts and eligibility vary by account.* ============================================================================== # How I finally backed up my iPhone photo library to Google Photos URL: https://saileshdahal.com.np/why-google-photos-did-not-count-my-backups Published: 2026-09-29 Tags: iOS, Mobile Development ============================================================================== I used to back up my iPhone photos by copying them to a USB drive, moving them to an Android phone with ReVanced, and uploading from there. It worked, but every batch meant doing the whole transfer again. I kept putting it off. I looked at [gotohp](https://github.com/xob0t/gotohp) and [gunshot](https://github.com/tqmane/gunshot), too. Neither worked for me. Then I found [PhotosBackup](https://github.com/g8row/PhotosBackup). It could upload from the iPhone through a Pixel XL route without using my Google account's storage quota. I let it back up my photos and opened Google Photos on the web. They were there. On the iPhone, Google Photos said **8,145 items were not backed up**. Its **Free up space** screen offered almost nothing. I wanted the space back, but I wasn't going to delete thousands of photos from Apple Photos just because I could see them in a browser. I wanted Google's app to recognize them and offer to remove the phone copies itself. ## Watching the count fall I tried a few photos I could find easily on both my phone and the website. A screenshot counted as backed up almost at once. Many camera photos didn't. Some Live Photos were online, yet the iPhone still showed them as not backed up. I had to close and reopen Google Photos now and then to get its count to refresh, which did not help my confidence. I updated PhotosBackup and ran **Re-check Backups** across the library. It now picked up the version of an edited photo that I saw on the iPhone. Before, the original file could be online while Google Photos was looking for the edited one. I had far more photos in that state than I expected, including Portrait shots and photos affected by the camera's own adjustments. I let the app go through thousands of items. I could leave it and come back without immediately stopping the backup; it kept working for a few minutes in the background and showed its progress. At one point Google slowed the uploads down. The app waited and continued. I still checked on it, but I didn't have to sit there with the screen open the whole time. The "not backed up" number eventually reached **43**. I thought I could deal with 43. Then I checked **Free up space** again. ## Only two photos to free up There were about **8,500 photos and videos** in the library I was working through. Google Photos now counted nearly all of them as backed up, but offered to free up just **2**. Many were Live Photos. Their still images had uploaded, but the short moving clips were missing. I turned on **Back Up Live Photo Motion** in PhotosBackup and checked one photo before leaving it to work through the rest. The motion played on the web, the iPhone showed the Live button, and Google's **Free up space** screen offered that photo. Finally, the three places agreed about the same item. The motion uploads took a while. I let them finish and checked again: **2 had become 461**. That was useful, though it still left most of the library on my phone. I used **Free up space** for what it found, then went through the rest with Google's **Delete from device** control in smaller groups. I read the warning each time. Selecting several photos could make that button available even when one of them was not backed up. I checked what remained in Apple Photos and what had moved to Recently Deleted as I went. I wanted to be able to catch a mistake before it was permanent. ## The last few photos The remaining items were a mixed bag. I used **Choose Photos or Videos** in PhotosBackup to recheck specific ones instead of running another sweep of the whole library. An edited video needed another upload. So did a photo I had edited inside Google Photos. A few had originally been saved from Google Photos back onto the iPhone. Uploading those same files again did not make the app count them. I made a tiny edit in Apple Photos, backed up the new version, and checked the count. I only had to do that for the handful left at the end. The number went from **43 to 28, then 17, 5, 3, and finally 0**. I used Google Photos to remove the remaining device copies. They went to Apple's Recently Deleted, and the iPhone library I had been working through was empty. What I wanted all along was to stop shuttling photos through a USB drive and another phone. The backup itself was much easier this time. Getting Google Photos to agree that the iPhone copies were safe to remove took longer, but seeing that final **0 not backed up** was worth checking every stubborn photo. ============================================================================== # I built a TikTok-style feed in DartNative. The native parts held up, the launch did not URL: https://saileshdahal.com.np/dartnative-tiktok-feed-native-features Published: 2026-09-24 Tags: Flutter, Dart, Mobile Development ============================================================================== *Update: with 30 pages instead of 1,000, the same screen starts in about 0.35 seconds. With 10,000 it never starts: iOS kills it for memory. `PageView.builder` here is not lazy. [The numbers are below](#update-30-pages-instead-of-1000-and-10000).* Eleven days ago I [built the same infinite video feed in DartNative and Flutter](/dartnative-vs-flutter-infinite-video-feed) and spent a whole section on the fact that DartNative had no `PageView`. I got paging working with 22 lines of Objective-C and two workarounds nobody had written down. On 17 September DartNative shipped `PageView`, `PageController` and a `pagingEnabled` flag on `FastList`, and its playground gained an infinite video feed demo that looks a lot like mine. So I went back. This time I did not stop at a feed. I built the screen people actually mean when they say "a video feed": TikTok's. Following and For You tabs, a column of Liquid Glass buttons down the right edge, a comments sheet with a real text field, swipe left for the creator's profile, a floating glass tab bar, pull to refresh, AirPlay, Picture in Picture, audio that keeps playing with the phone locked, and next and previous on the lock screen. Then I put it on a cabled iPhone 16 Pro Max running iOS 27.0, profiled it, and kept the logs. The short version: every native feature worked, and most of them cost me nothing. The ones that did cost me a 744-line plugin. And the finished screen took three seconds to show its first frame, where the plain feed took half a second. ## PageView landed, and the shim is gone The 22 lines of Objective-C, the safe-area workaround and the manual `DynamicLibrary.process()` lookup are all deleted. The feed is `PageView.builder(scrollDirection: Axis.vertical, ...)` and nothing else, and one swipe is one page on the platform's own paging. The item ceiling from the last post is still there. It is documented now rather than fixed. The doc comment on `PageView` in the SDK says an unbounded builder grows "up to a ceiling of about a thousand full-screen pages on a phone, past which the list ends: native paging stops snapping beyond that much content." The release notes describe the same builder as a feed that "has no end and grows as the reader scrolls." Those two sentences do not describe the same widget, and only the doc comment matches what I found in September. I also pulled 10,000 portrait clips from the Pexels API and hard-coded them, because a TikTok clone with 17 videos is a slideshow. They added 3.3MB to the app. The pager has 1,000 pages, so one session never sees more than a thousand of them. ## What DartNative gave me for free
![The TikTok-style feed on an iPhone 16 Pro Max: Following and For You tabs at the top, a column of Liquid Glass buttons for like, comment, save, share, AirPlay and Picture in Picture on the right, the caption at the bottom left, a progress bar, and a floating Liquid Glass tab bar.](https://saileshdahal.com.np/images/posts/dartnative-tiktok-feed-native-features/feed.png) ![The comments sheet: a native iOS sheet with a grabber, six comments, and a text field in a glass capsule sitting on the keyboard, with the Send return key and QuickType suggestions.](https://saileshdahal.com.np/images/posts/dartnative-tiktok-feed-native-features/comments-keyboard.png) ![A creator profile reached by swiping left: a glass-ringed avatar, follower counts, a red Follow button, and a grid of the creator's clips. The avatar and two tiles are black because their posters never rendered.](https://saileshdahal.com.np/images/posts/dartnative-tiktok-feed-native-features/profile.png)
*Left to right: the feed, the comments sheet with the keyboard up, and a creator's profile after swiping left. All three are screenshots from the iPhone.* This is where DartNative is at its best, and it is not close. Every control on these screens is the platform's own: - The buttons on the right are `GlassEffectContainer`, which is iOS 26 Liquid Glass sampling the video under it, with the system press animation when `interactive` is set. - The tab bar is `BottomNavigationBar`, which on iOS 26 renders as the real floating `UITabBar` pill. - Share opens `UIActivityViewController` through the `dartnative_share` plugin. - The progress bar is a `UIProgressView`, indeterminate while a clip loads. - Likes and saves fire `HapticFeedback`. The comments sheet is `showModalSheet`, which is `UISheetPresentationController` with its detents, grabber and swipe to dismiss. The field is a `UITextField`: QuickType, the loupe, the Send return key from `TextInputAction.send`, all of it. The composer sits in `Scaffold.bottomInputBar` and rides the keyboard's own animation. I wrote no native code for any of this. The middle screenshot also shows a bug. The "4.9K comments" title is dark text on a dark sheet: the native title follows the phone's light theme, not the sheet colour I set. ## What it did not give me cost 744 lines `dartnative_video_player` 1.0.1 gives you play, pause, seek, volume, speed, looping, fit and a pre-cache. It has no API for AirPlay, Picture in Picture, background playback, or the lock-screen player. The framework has no pull to refresh at all. I searched the SDK, the docs and the playground for `RefreshIndicator`, `onRefresh` and `UIRefreshControl` and found nothing. So I wrote them as a plugin: 450 lines of Swift and 294 of Dart. It follows DartNative's plugin guide properly, with `@_cdecl` entry points, a claimed view type and the dispatcher slot for calling back into Dart. The guide is good. The starting point is not: `dn create --template=plugin` generates Flutter's method-channel plugin template, with `MethodChannel` and `plugin_platform_interface`, which is the opposite of what the guide tells you to build. ### How the plugin reaches the player The video plugin is closed source and exposes one thing about its native side, `nativeViewPtr`, a pointer to the view it draws into. My plugin walks that view's layer tree until it finds an `AVPlayerLayer`, takes its `AVPlayer`, and hangs Picture in Picture, AirPlay and the lock-screen controls off it. Pull to refresh works the same way: it walks the window for the visible scroll view that pages vertically and attaches a `UIRefreshControl` to it. Both work today. Both depend on internals nobody promised me, and the first plugin update that wraps its layer differently breaks them without a compile error. ### What worked on the phone I tried every one of these on the phone and they all worked: AirPlay to another screen, Picture in Picture from the button and from swiping home, audio with the screen locked, next and previous on the lock screen and in Control Center, pull to refresh, tapping Home to jump to the top and refresh, the comments sheet, the share sheet and the profile swipe. That is a manual check. I have no timings for any of it. The profile screenshot on the right shows the other bug I did not chase: some avatars and grid tiles never render their poster, although the same URLs return a normal JPEG to `curl`. ### Three players or four `VideoPlayerController`'s documentation says to keep at most three players alive, because a phone has roughly three hardware decoders. The playground's own feed demo keeps four on iOS. ## What this screen would take in Flutter I did not build the Flutter version, so this section compares against what Flutter's own packages offer today, with no platform code of my own, and against the Flutter issues where people have been asking for the missing parts. Issue ages and comment counts are as of 24 September 2026. "Without method channels" is not the difference. My plugin has no `MethodChannel` in it, but Flutter apps can skip method channels too: `dart:ffi` and the Dart team's `ffigen` and `objective_c` packages reach the same Objective-C and Swift APIs. The difference is what that native code can find when it gets there. In DartNative the video, the glass and the text field are UIKit views in one hierarchy, so a few lines of Swift can walk to the `AVPlayerLayer` and hand it to AVKit. In Flutter the video is a texture the engine composites, and there is no layer to hand over. ### The open issues, oldest first Each entry is a Flutter issue behind something on this screen, in the order it was opened, with what DartNative does about it. #### November 2017: autocorrect on iOS [#12920](https://github.com/flutter/flutter/issues/12920), "Autocorrect tooltips don't appear on iOS", is still open with 65 comments. Flutter draws its own text field on top of the system's text input, so the parts of iOS text editing it has not rebuilt are simply missing. The comment field in this app is a `UITextField` and does whatever iOS does. #### February 2019: no video cache [#28094](https://github.com/flutter/flutter/issues/28094) asks for `video_player` to cache anything at all. It has 119 comments and 140 thumbs up. DartNative's player shipped with a pre-cache and a disk cache, and the September post found that cache was a large part of why its feed loaded faster. #### April 2019: PageView does not preload [#31191](https://github.com/flutter/flutter/issues/31191), "PageView should preload pages", 49 comments and 130 thumbs up. DartNative's `PageView` has the opposite problem, as the next section shows. #### July 2019: sheets that fight their own scroll [#36283](https://github.com/flutter/flutter/issues/36283), "Can't swipe to dismiss scrollable Bottom Sheet", 46 comments. Flutter now has `CupertinoSheetRoute`, but it is still Flutter drawing a sheet. The comments sheet here is `UISheetPresentationController`, with the system's own detents and dismiss gesture. #### May 2020: the keyboard and the scroll [#57609](https://github.com/flutter/flutter/issues/57609), "Let iOS keyboard dismissal synchronize with scroll", 53 comments and 94 thumbs up. Same root as 2017: the keyboard belongs to iOS, the field and the scroll view belong to Flutter, and the two have to be kept in step by hand. In DartNative all three are UIKit. #### June 2020: AirPlay and Picture in Picture [#59554](https://github.com/flutter/flutter/issues/59554) asked for AirPlay and Chromecast in the video player. It was closed as not planned in July 2023. A week later [#60048](https://github.com/flutter/flutter/issues/60048) asked for Picture in Picture on iOS. It is still open, with 49 comments, and the first post says why it is hard: `video_player` draws video into a texture, and iOS only does Picture in Picture from an `AVPlayerLayer`. My plugin needed native code for this too, but the plugin's video is already an `AVPlayerLayer` in the view tree, so there was something to hand to AVKit. In Flutter, the player would have to change how it draws video first. #### July 2020: how do you build this screen at all [#62265](https://github.com/flutter/flutter/issues/62265), "How to effectively implement a feed of Video Players in Flutter", 79 comments. It is the question this whole series started from. #### June 2025: Liquid Glass [#170310](https://github.com/flutter/flutter/issues/170310), "Support for iOS 26 Liquid Glass Design in Cupertino Widgets", is the youngest issue here and the loudest: 87 comments, 557 thumbs up, labelled P3. Flutter draws its own UI, so it would have to imitate the material. DartNative's `GlassEffectContainer` is the material, sampling the video under it. #### September 2026: AirPlay in Flutter, PageView in DartNative On 4 September [flutter/packages#12490](https://github.com/flutter/packages/pull/12490) merged, and `video_player` on iOS now sends video to an AirPlay screen instead of mirroring the app. An AirPlay button inside the app still needs a platform view. Mine is one shared `AVRoutePickerView` behind a glass button. On 17 September DartNative shipped the `PageView` this app is built on, the one I had to fake in September. ### What the timeline leaves out Three things on this screen are missing from the timeline because Flutter never needed an issue for them. Audio with the screen locked is one flag, `VideoPlayerOptions(allowBackgroundPlayback: true)`, and it cost me native code in DartNative. Pull to refresh ships with Flutter as `RefreshIndicator` and `CupertinoSliverRefreshControl`, and does not exist in DartNative at all. The lock screen and Control Center player is the exception the other way: `video_player` has no API for it, I found no issue asking for one, and in DartNative it took my plugin as well. ### The pattern Laid out by year, the oldest issues are about the places where Flutter draws its own version of something iOS already has: text editing, sheets, the keyboard, and now glass. DartNative skips that whole class of problem because it uses the system's own. Where DartNative falls short is the other side: features Flutter's first-party packages already ship, like background audio and pull to refresh, still needed native code from me. ## Launch went from half a second to three Startup here is from the first line of `main()` to the first frame, three launches each, profile builds, launched without a debugger: | Build | Startup | |---|---| | The September feed's plain overlays, on `PageView` | 477 to 608ms | | The TikTok-style screen | 2,724 to 3,452ms | | The same screen with the right-hand button column removed | 806 to 886ms | For context only, September's feed started in 863ms on the same phone, on iOS 26.6.1 and an older SDK. That is too many changes to call anything faster. ### My own mistakes Some of the three seconds was my fault. My first version put an AirPlay picker (`AVRoutePickerView`) and a `UIProgressView` on every page. In a Time Profiler trace of that version, creating the pickers took about half a second of main thread, because each one parses an animation package for its icon, and the progress bars took about 0.4 seconds rendering their track images. One shared picker behind a normal glass button and one progress bar over the pager fixed both, and launch was still near three seconds. ### What the trace shows In the trace of the fixed build, Dart spent about 0.1 seconds building widgets. Almost everything else was native: creating views, and inside the Core Animation commit, Yoga laying them out. Nearly two thirds of the commit was Yoga layout, most of it spent on absolutely positioned children. In DartNative every widget is a real `UIView` with a Yoga node. A TikTok page is a stack of positioned layers with a column of glass buttons, icons and labels inside it, around 70 views, and removing that column took launch from three seconds to under one. ### PageView.builder built all 1,000 pages Then I counted how many pages were being built. With `itemCount: 1000`, `PageView.builder` called `itemBuilder` 1,000 times before the first frame, once for every index from 0 to 999. It did it again on every `setState`, for 3,000 calls in the first 12 seconds as clips reported ready. The doc comment says a page that scrolls out of the kept window "releases what it built". In this app nothing was released because nothing was ever left unbuilt, and `allowImplicitScrolling` was already off in the run I counted. The rail was never the real problem. A thousand rails were. I did not instrument the plain feed, so I cannot say whether it also builds a thousand pages and gets away with it because its pages are about 8 views. I also did not measure the fix I would try next, a rail with fixed sizes. If you are building on `PageView` today, put a counter in your `itemBuilder` before you trust the doc comment. ### Update: 30 pages instead of 1,000, and 10,000 After this went out, the DartNative team replied that a feed only needs about 30 pages loaded. So I ran it both ways on the same phone in one session, with nothing changed but the page count: | Pages | Startup | First clip ready | |---|---|---| | 30 | 342 to 356ms | 536 to 613ms | | 1,000 | 2,221 to 2,341ms | 2,693 to 2,850ms | | 10,000 | never drew a frame | never | With 30 pages the full TikTok-style screen starts faster than the plain feed did above. The builder behaves the same at every size: it built every page before the first frame and rebuilt all of them on every `setState`. At 30 that costs almost nothing. At 1,000 it is the whole launch. At 10,000 the app never showed a frame. Dart finished building all 10,000 pages about half a second after launch, and then the native side tried to create views for all of them. The phone's memory report from those runs shows iOS killing the app at about 3.4GB, while it was still on screen. It happened on all three launches. So in this app, `PageView.builder` is not lazy. Flutter's builds the pages near the viewport, which is why `itemCount: 10000` costs about the same as 30 there. DartNative's built every index it was given, and its cost grew with the page count until the app ran out of memory. The doc comment says pages outside the kept window are released, and that is not what I measured. Until that changes, the page count is a budget: keep it near 30. Two things to keep in mind before copying this. A 30-page feed ends at page 30, and I have not tested a feed that grows its page count as the reader scrolls. And this build also had the fixed-size rail I mentioned above, so its 1,000-page number is not comparable with the table at the top of this section, which came from an earlier session. ## Smaller things - The simulator build failed in DartNative's own build step with `Binary ... Flutter.framework/Flutter does not contain architectures "arm64 x86_64"`, while `lipo` on the same file lists `x86_64 arm64`. I did everything on the device instead. - The SDK installer puts `~/zero/bin` first on your `PATH`, so `flutter` becomes DartNative's copy. The installer says so, and it will still surprise you the first time a stock Flutter project picks up the wrong toolchain. - Current Xcode builds nothing below iOS 15. DartNative's release raised its minimum to match. My September Flutter project needed the same change before it would build at all. - The September DartNative app, Skia and all, still built on the new SDK without edits. `dn` upgraded its project files by itself. That is the kind of thing a young framework usually gets wrong. ## A correction to the September post In September I wrote that four `flutter run --profile` attempts hung on this phone with "The Dart VM Service was not discovered," and that I read it as a Flutter and CoreDevice tooling problem. This time `dn run --profile` hung with the same message on the same phone, and the app sat on its launch screen. DartNative's CLI is built on Flutter's tools, so one hang on each side points at the shared tooling or CoreDevice, not at Flutter. Launching the installed app with `devicectl` worked every time, and that is how every number in this post was taken. ## What I did not measure This post is DartNative only. I rebuilt the Flutter app for this phone and it builds, but I never launched it, so there is no Flutter column and no comparison. I did not run swipe or load tests, time to first frame per clip, memory, leaks or dropped frames. Nothing above tells you whether this feed leaks as you scroll. ## What I would tell a team If your problem is that Flutter's controls do not feel native, DartNative solves it, and this screen is my evidence. The glass, the sheet, the text field and the tab bar are the system's own, and I got them by writing Dart. If your problem is a media app, count the native work first. Every feature that made this feel like TikTok and not a list of videos needed platform code the plugin does not expose, and my plugin reaches into internals to get it. Keep the page count small: at 30 pages this same screen started in about a third of a second. And measure launch with your real page, not the demo's. The plain feed started in half a second. Mine took three. The difference was entirely in how many native views the framework built, for pages nobody could see. ## Sources - [video-feed-showdown](https://github.com/saileshbro/video-feed-showdown/tree/dartnative-pageview-revisit), the app, the plugin, and every log and trace behind these numbers in `bench/v2/` - [RESULTS-v2.md](https://github.com/saileshbro/video-feed-showdown/blob/dartnative-pageview-revisit/bench/v2/RESULTS-v2.md), each figure mapped to its file - [I built the same infinite video feed in DartNative and Flutter](/dartnative-vs-flutter-infinite-video-feed), the comparison this follows - [DartNative wants Flutter developers to ship native UI from Dart](/dartnative-flutter-native-ui-framework), the original review - [DartNative changelog](https://github.com/DartNative/dartnative/blob/main/CHANGELOG.md), the `PageView` release of 17 September - [DartNative widget reference](https://github.com/DartNative/dartnative/blob/main/docs/widgets.md) and [plugin development guide](https://github.com/DartNative/dartnative/blob/main/docs/plugin_development.md) - [DartNative's feed demo](https://github.com/DartNative/dartnative/blob/main/playground/lib/screens/media/video_feed_demo.dart) - [Pexels API](https://www.pexels.com/api/), the source of the 10,000 clips ============================================================================== # I built the same infinite video feed in DartNative and Flutter URL: https://saileshdahal.com.np/dartnative-vs-flutter-infinite-video-feed Published: 2026-09-13 Tags: Flutter, Dart, Mobile Development ============================================================================== *Update, 24 September 2026: DartNative shipped `PageView` four days after this post, so I [rebuilt the feed as a TikTok-style screen and profiled it](/dartnative-tiktok-feed-native-features). That follow-up also corrects this post: the "Dart VM Service was not discovered" hang I blamed on Flutter's tooling below happened to DartNative's `dn run` too, on the same phone.* I spent years shipping Flutter apps, and two of them had infinite video feeds. That screen never stopped annoying me. `video_player` was slower to start a clip than I wanted, it caches nothing on its own, and a feed puts both of those in front of the user on every swipe. My fix at the time was to push everything through Mux for HLS and pull in community packages the Flutter team does not maintain. Having to leave the official ecosystem to make a core feature feel acceptable always struck me as a bad sign. I was not the only one hitting this. [flutter/flutter#62265](https://github.com/flutter/flutter/issues/62265), open since 2020, is someone else asking how to build exactly this screen after `PageView` and `ListView` both gave them lag and crashes on long scrolls. [flutter/flutter#28094](https://github.com/flutter/flutter/issues/28094) has been asking for `video_player` to cache anything at all since 2019, with 140 thumbs up and still open. So when I [reviewed DartNative](/dartnative-flutter-native-ui-framework) I finished with an instruction to myself. Give it one ugly screen, not a whole app. This post is me doing that. Both apps, the raw logs and the screen recordings live in [video-feed-showdown](https://github.com/saileshbro/video-feed-showdown), and every number below comes from [RESULTS.md](https://github.com/saileshbro/video-feed-showdown/blob/main/RESULTS.md) in that repo. Mux is only the history that made me curious. Neither app uses it, and both stream the same plain Pexels MP4s with nothing transcoding in front of them. ## Building the thing Both apps read the same generated config: the same 13 Pexels clips, 1000 rows, three video players alive at a time. Swipes came through argent as slow, momentum-free drags, so every gesture is the same deterministic pull rather than a fling of unknown speed. Both apps write to the same event log, byte identical on each side. I ran them on a cabled iPhone 16 Pro Max in profile mode and on an iPhone 17 Pro Max simulator in debug. Those two sets of numbers are not interchangeable, so I keep them apart below. ## DartNative has no PageView, and you cannot write one in Dart This was the first real cost, and it showed up before any measuring did. A vertical feed needs one flick to move exactly one page. DartNative does not ship that. Its widget reference lists `PageView` as not planned, its skill guide suggests tabs with an `IndexedStack` or a horizontal list instead, and its own playground carousel says in the header that it was built by hand because there is no paging API. You cannot fake it from Dart either. `FastListController` only moves by index, and for a row exactly one screen tall every alignment option resolves to the same offset, so there is nothing there to drive a finger-following pager with. What did work was 22 lines of Objective-C that switch on `UIScrollView.pagingEnabled` on the native list, called from Dart through `DynamicLibrary.process()`. Paging kicks in about 2ms after the first frame. Two things bit me on the way there. The first is that a safe-area inset breaks the alignment. Paging steps by exactly the view height, so any inset leaves every page short and a sliver of the previous video stays on screen. Zeroing the inset fixes it. The second is stranger. Paging snaps fine at 40 rows and at 1000 rows, then silently stops snapping at 10000, where the list just rests wherever your finger left it. That is why both apps are pinned to 1000 items, and it is a nasty surprise for anyone who assumes an infinite feed means an unbounded list. So yes, PageView-style interaction is achievable. It costs a native plugin and two gotchas that are not written down anywhere. ## The load gap is visible on screen The load test is eight rapid swipes 500ms apart, deliberately faster than the preload window, run on the simulator in the same session for both apps. Here is where each one ended up: DartNative on the left, playing full bleed with no buffering spinner, Flutter on the right, with a spinner over the clip. ![Side by side recording of the same eight rapid swipes on each app. DartNative on the left keeps playing with no stall. Flutter on the right shows a buffering spinner appear mid-clip.](https://saileshdahal.com.np/images/posts/dartnative-vs-flutter-infinite-video-feed/fast-scroll-side-by-side.gif) Full-length, unclipped source recordings for both apps are in the [repro folder](https://github.com/saileshbro/video-feed-showdown/tree/main/bench/repro) of the repo, along with the same swipe test on a gentler five-swipe pass. In that run DartNative's time to first frame had a median of 62ms. Flutter's was 1207ms, and one row hit a genuine network timeout. There were zero errors in every DartNative run in RESULTS.md. That is the screen I was complaining about at the top of this post, reproduced on demand. I do not think it means DartNative decodes video faster, though, and here is why. Mostly it is caching. DartNative's video player has byte-range pre-caching built in and a 100MB disk cache. `video_player` has neither, which is the same gap [flutter/flutter#28094](https://github.com/flutter/flutter/issues/28094) has been open about since 2019. On iOS, `video_player` has also been reported to download the whole file before it starts playing rather than streaming it ([flutter/flutter#126760](https://github.com/flutter/flutter/issues/126760)), which is its own reason a swipe-to-play feed feels slow. Both apps in this comparison hit the same URLs, so the gap here is about what the two players come with, not about the rendering pipeline. The rest of it is that the two apps did not do the same amount of work. Row 8 against row 32 is the whole story of those two screenshots. Native paging is locked to one page per swipe, so DartNative visited fewer rows and every one it landed on was already warm. Flutter's `PageView` physics carry the same gesture much further, so it kept landing on rows the three-player pool never had a chance to touch. `PageView` has no preload of its own either ([flutter/flutter#31191](https://github.com/flutter/flutter/issues/31191), open since 2019), so landing on a cold row is the expected outcome, not a surprise. The spinner is that, not a defect. A gentler five-swipe test showed the same pattern, DartNative on row 5 and Flutter on row 14. And these load numbers are debug builds on a simulator, which RESULTS.md flags as not representative of real performance. In that same simulator session DartNative was the slower one to start, at 1371ms against Flutter's 548ms. Both apps have a code path to bundle the videos as local assets and take the network out entirely, which is the test that would separate rendering from caching. I never bundled them, so that number does not exist yet. ## The measurement I wanted most is the one I could not take On the physical iPhone in profile mode, DartNative started in 863ms and its time to first frame had a median of 88ms across 48 rows, with zero errors. The Flutter column is empty. Four `flutter run --profile` attempts against that phone each hung at "Installing and launching" for eight to ten minutes and never found a VM Service. No crash report, and the app process was alive on the device the whole time. Launching it directly with `devicectl` worked once, but `flutter attach` still could not see it. DartNative needed one retry on its first launch against the same device, then ran every time after that. I read that as a Flutter and CoreDevice tooling problem on this particular phone rather than anything about Flutter's runtime. Either way the consequence stands. *Correction, 24 September 2026: in the [follow-up](/dartnative-tiktok-feed-native-features), `dn run --profile` hung with the same "Dart VM Service was not discovered" message on the same phone. DartNative's CLI is built on Flutter's tools, so this points at the shared tooling or CoreDevice rather than at Flutter. Launching the installed DartNative app with `devicectl`, without a debugger, worked every time; above, the same kind of launch also got the Flutter app running once.* 863ms and 88ms have nothing to compare against, and the on-device half of this comparison is a single column. ## What I would do with it Plenty is missing. I did not capture memory or dropped frames. That is the obvious next step, and the launch problem above blocks it. I measured binary size for DartNative only, 26.1MB for a profile build, with no Flutter build to put beside it. And the network-free test above never happened. If your hard screen is a media feed, this is a fair thing to try, on one condition. You should already have given up on `video_player` on its own. What I measured is mostly that DartNative's player arrives with the caching layer I used to bolt on by hand. If your hard screen is a long interactive list that is not a pager, the paging findings do not apply to you. If it is a pager, budget for the native shim, the inset fix and the item ceiling before you decide. And if you take one thing from this, take the setup rather than the numbers. Two apps from one config, one shared logger, the same deterministic swipes and the raw logs kept in the repo are the reason I can say which part of the gap is the framework and which part is a cache. A demo video cannot do that, and neither can a benchmark you cannot re-run. ## Sources Primary: - [video-feed-showdown, both apps and the shared benchmark setup](https://github.com/saileshbro/video-feed-showdown) - [RESULTS.md, every figure above with its caveats](https://github.com/saileshbro/video-feed-showdown/blob/main/RESULTS.md) - [bench/repro, screen recordings of both apps under the same swipe script](https://github.com/saileshbro/video-feed-showdown/tree/main/bench/repro) - [DartNative wants Flutter developers to ship native UI from Dart](/dartnative-flutter-native-ui-framework), the review this comparison follows up on - [DartNative widget reference](https://github.com/DartNative/dartnative/blob/main/docs/widgets.md) Every number here is parsed from a raw event log kept in the repository, and the parser command is in the README. The physical-device column covers DartNative only. The simulator numbers are debug builds and are directional at best, and the caching asymmetry between the two video players is not controlled for anywhere in this pass. The pain points above are not unique to me. These are open issues on flutter/flutter, linked where they back up a specific claim in the post above: - [#62265, how to effectively implement a feed of video players](https://github.com/flutter/flutter/issues/62265) (open since 2020, 79 comments) - [#28094, add network cache functionality to video_player](https://github.com/flutter/flutter/issues/28094) (open since 2019, 140 thumbs up) - [#126760, video_player downloads the entire file before playing on iOS](https://github.com/flutter/flutter/issues/126760) (open since 2023) - [#31191, PageView should preload pages](https://github.com/flutter/flutter/issues/31191) (open since 2019, 130 thumbs up) ============================================================================== # DartNative wants Flutter developers to ship native UI from Dart URL: https://saileshdahal.com.np/dartnative-flutter-native-ui-framework Published: 2026-09-08 Tags: Flutter, Dart, Mobile Development ============================================================================== DartNative has a pitch Flutter developers will immediately understand: keep Dart, keep stateful widgets and hot reload, but stop drawing a fake `TextField` on a canvas. Use the real one. `Text`, inputs, lists and navigation become UIKit or Android Views, so an iPhone control behaves like one and an Android control does not have to pretend otherwise. That is more interesting than the usual cross-platform performance claim. Flutter has always made a deliberate trade. It draws almost every pixel itself, which gives you a consistent application on iOS and Android. It also means text selection, scrolling, keyboard behaviour and accessibility are Flutter's job to reproduce. DartNative gives that work back to the operating system. I like the premise. I would not migrate a Flutter application to it yet. ## DartNative removes the renderer, not the hard parts DartNative says it runs on Zero, a fork of `flutter_zero` with Flutter's rendering stack taken out. Dart AOT and familiar Flutter tooling remain. Its `dn` command wraps the tooling, then DartNative turns the Dart widget tree into UIKit or Android Views through FFI. Android crosses JNI as well. ```text Dart widget and state tree -> DartNative reconciler -> FFI bindings (and JNI on Android) -> UIKit or Android View tree -> the operating system compositor ``` Yoga handles flexbox-style layout. When `setState()` runs, DartNative says it diffs the native view tree on the platform's main thread. For long lists it has separate `FastList`, `FastGrid` and `MasonryFastGrid` widgets backed by the platform's recycling controls. The ordinary `ListView` is for smaller collections. That makes sense. A native `UITextField` and an Android `EditText` carry more than a rectangle and a cursor. They carry years of keyboard, selection, accessibility and input-method behaviour that cross-platform frameworks spend years chasing. DartNative can take that behaviour for free because the platform already owns it. It also gives something up. Flutter can make iOS and Android nearly indistinguishable. DartNative wants the platforms to look and act like themselves. If your product depends on one precise visual language, that is a design decision, not a technical footnote. The company claims AOT code from the first frame, no renderer warm-up and 120fps native scrolling. I could not find a public benchmark suite that runs the same app against current Flutter on representative devices. The demos show DartNative working. They do not prove that it wins on your application. That is still a benchmark you have to run yourself. ## The package question will decide most migrations The documentation makes migration look pleasantly small at first. Change an import to `package:dartnative/flutter_compat.dart`, keep using `Column`, `Row`, `Container`, `Text`, `ListView`, `Navigator` and `setState`, then carry on. That works only while the code stays inside the part DartNative reimplemented. DartNative is Flutter-compatible, but it does not run the Flutter framework. Pure-Dart packages can work when they do not import `package:flutter`. Flutter packages that rely on Flutter APIs, `MethodChannel` or `flutter_web_plugins` need a DartNative plugin or an FFI/JNI port. The troubleshooting guide names `go_router` as unavailable because `flutter_web_plugins` is not there. The same limit appears in widgets. `TabBar`, `TabBarView` and `Dismissible` are currently unsupported. Some parameters are omitted or renamed. DartNative says incompatible code fails at compile time instead of quietly changing behaviour, which is the right choice. It still means the migration starts with a package and widget audit, not a global find-and-replace. I would inventory the application before trying it: third-party Flutter packages, custom painters, platform channels, web and desktop targets, anything that needs identical pixels across phones. The number you get from that inventory is more useful than the number of familiar widget names in the docs. ## The harder risk is the company behind it DartNative calls iOS and Android production-ready and names Gee and Presence Messenger as production users. Its public changelog also calls 31 July 2026 the first public preview and says some pieces will change before 1.0. That does not mean the product is unsafe. It does mean the framework has not had Flutter's years of public bug reports, patches and strange edge cases. The core framework is proprietary. The public GitHub repository has documentation, examples, demos and issues. The company develops the framework in private repositories and distributes compiled artifacts. The official demos are free. Your own application is not. DartNative's Community plan costs $29 a year and permits one production app, defined as an app past 1,000 installs. Standard is $49 a year for unlimited apps, and Pro is $99 a year with higher-priority support. All three include the framework and the vendor's first-party plugins. You also need a device licence key to build and ship. If the subscription ends, the licence says apps already in users' hands keep running, but you cannot make new builds or deployments. There is a source-release promise in the licence. If a defined discontinuation event happens, DartNative says it will release the source under BSD-3 within 90 days. I would take that as a contingency, not an answer to the ordinary question every mobile team eventually asks: who fixes the framework when we hit an urgent bug on a Friday? One easy mistake is to confuse it with the older open-source [`dart_native`](https://pub.dev/packages/dart_native) package. That package is a bridge for calling Objective-C, Swift and Java from Dart. This DartNative is a different product with a separate toolchain, licence and maintenance story. ## What I would do with it I would give DartNative one ugly screen, not an entire application. Pick the screen with the long interactive list, stubborn text entry, keyboard transitions, deep navigation and accessibility requirements. Add analytics and crash reporting. Build it for both platforms, then measure startup, scrolling and input on the devices your users have. Then count what you had to replace. The missing package or native integration is usually more expensive than the widget you can see on screen. Teams whose product lives on iOS and Android, whose Flutter dependency list is short, and whose hardest problems are text input or platform behaviour should try it. Teams that need Flutter's package community, web, desktop, canvas-style interfaces or source-level control should stay put. DartNative may earn its place. It needs to earn it against your awkward screens and your dependency list, not against a polished demo. ## Sources Primary: - [DartNative public repository and README](https://github.com/DartNative/dartnative) - [DartNative architecture](https://github.com/DartNative/dartnative/blob/main/docs/architecture.md) - [DartNative widget reference](https://github.com/DartNative/dartnative/blob/main/docs/widgets.md) - [Getting started with DartNative](https://github.com/DartNative/dartnative/blob/main/docs/getting_started.md) - [Dependency compatibility](https://dartnative.com/docs/getting-started/dependencies/) - [DartNative troubleshooting](https://dartnative.com/docs/troubleshooting/) - [DartNative pricing](https://dartnative.com/) - [DartNative changelog](https://github.com/DartNative/dartnative/blob/main/CHANGELOG.md) - [DartNative licence](https://github.com/DartNative/dartnative/blob/main/LICENSE) The architecture, supported widgets, claimed production use and performance framing above come from DartNative's own documentation. I have not independently benchmarked the framework or audited its private source code. ============================================================================== # AI Week: GPT-6 Astra shipped and NVIDIA bought Hugging Face on the same day URL: https://saileshdahal.com.np/ai-week-2026-09-04 Published: 2026-09-04 Tags: AI, llm, Security, Machine Learning, Developer Tools Series: AI Week (part 7 of 7) ============================================================================== I owe you an apology. [Issue 6](/ai-week-2026-08-30) was written on 30 August and then sat in a folder instead of going out, so if you have been waiting since [Issue 5](/ai-week-2026-08-23) on 23 August, that is my fault and not a mail problem. It is published now, at its original date, and it covers the OpenAI technical report on the Hugging Face incident, GLM-5.3's weights and licence, GLM-5.3-Flash, and Qwen3.8-Flash-Next. If you read one thing before this issue, read the first section of that one, because this week's lead story lands directly on top of it. Three things happened between 31 August and 4 September, and two of them landed on the same morning. OpenAI shipped GPT-6 Astra, a model it says is the most intelligent and most aligned in the world, and published alongside it the finding that its reasoning is harder to monitor than the previous model's. The place the open-model world keeps its weights got a corporate owner. And three separate labs put their strongest cyber-capable models behind vetting programmes inside seventy-two hours of each other, each announcing on its own, none of them calling it a pattern. 3 September was a strange day to watch. A frontier model launch, a $12.93 billion acquisition of the commons that model was trained to work with, and a three-hour Claude outage, all before lunch in New York. ## 1. NVIDIA is buying Hugging Face Jensen Huang published the announcement on the NVIDIA blog on 3 September: NVIDIA has agreed to acquire Hugging Face for $12,930,300,000. Clement Delangue confirmed it the same morning. Reuters reports about $11.9 billion going to Hugging Face investors, with an equity retention programme of up to $1 billion for staff who join NVIDIA. The scale explains the price. More than 18 million developers use the platform. It hosts more than 3 million models, 500,000 datasets and 1 million applications, and more than 200,000 companies use it to find, evaluate and deploy AI. If you have run `from transformers import` in the last five years, you are inside this transaction. Huang's commitment is specific enough to hold him to: "NVIDIA compute will not be required to build on or deploy through Hugging Face." The post also promises continued support for open weights from every model builder and for multi-cloud, multi-accelerator deployment. NVIDIA is already the platform's largest single contributor, with more than 500 models and more than 250 open datasets published there. That is why the promise is credible. It is also why the neutrality question is real. Delangue's defence of the deal on the press briefing was that Hugging Face is "almost structured, by definition, like a de-concentration platform," a counterweight to proprietary APIs concentrating value. That is a good argument about what Hugging Face has been. It is not an argument about what a wholly owned subsidiary of the company that sells the accelerators will be in three years. None of it is settled. The deal triggers Hart-Scott-Rodino premerger notification and EU merger review, unlike NVIDIA's structured deals with Groq and Poolside, which were shaped to avoid exactly that, so it may not clear at all. If it does clear, the thing to watch is whether inference endpoints, benchmark placement and default deployment paths stay accelerator-neutral once the quarterly numbers want otherwise. And whether the promise outlives the person who made it. It is a blog post, not a covenant. Huang's post and Delangue's confirmation are primary and the price is exact. The investor split, the retention pool and the antitrust analysis are reporting. The Information had the story on 26 August, a full week before either company said anything. A confirmation is usually the end of a process, not the start of one. One more thing from the same week. On 3 September, Huang personally donated $10 million to Nepal's Prime Minister Disaster Relief Fund for the Bhote Koshi flood, pushing the relief total past $65 million. The flood came out of the Rasuwa corridor on 26 August, most likely from an ice-rock avalanche rather than an earthquake, and by 3 September the National Disaster Risk Reduction and Management Authority had it above 1,200 dead and roughly 2,500 missing, including several hundred foreign nationals, with about 1.6 million people affected. It is the deadliest disaster in Nepal since the 2015 earthquake. The figures are still moving. That donation is Huang's own money, not NVIDIA's. Setting a personal gift against a corporate acquisition is not a fair comparison and I am not going to pretend it is one. Both numbers came out of the same week and the same person, and the ratio between what this industry can move and what it moves when a mountain gives way is sitting right there without any help from me. I live here. That is the only reason this item is in a newsletter about API contracts. ## 2. GPT-6 Astra shipped, and the interesting numbers are the ones that undercut the launch OpenAI released GPT-6 Astra on 3 September, a few hours after the Hugging Face announcement. It went to a limited set of organisations first, with ChatGPT Plus, Pro, Business and Enterprise, the OpenAI API and AWS following "over the coming days". The API model ID is `gpt-6-astra`, standard pricing is $10 per million input tokens and $50 per million output, and a Fast mode gives up to twice the speed at twice the price. Enterprise access is off by default at launch, which is a deliberate choice worth noticing. Three benchmarks are saturated rather than won: FrontierMath Tier 4 at 97.6 percent, ARC-AGI-3 at 99.9 percent against 7.8 percent for GPT-5.6 Sol, and ExploitBench at 100 percent. On computer use it scores 72.6 percent on the OSWorld 2.0 offline set at roughly 40 minutes per task, against 65.7 percent at roughly 75 minutes for Sol. Terminal-Bench 4.0 goes from 37.3 to 57.9 percent. Astra also improved two bounds on prime gaps, one of which had a term that had not moved in over eighty years. Read the tables rather than the prose, because OpenAI published two numbers that its own launch copy does not dwell on. On the Artificial Analysis Intelligence Index v4.1.1, Astra scores 61.2 against Claude Fable 5.1's 65.7. On Humanity's Last Exam with tools, Astra scores 57.2 against Fable 5.1's 65.0. The company calling itself "the world's most intelligent model" is losing two of the general-intelligence measures it chose to print. That is a strong argument that the frontier is now shaped rather than ranked: Astra is far ahead on computer use, cyber and long context, and behind on some general reasoning. Pick per workload, not per leaderboard. The alignment numbers connect straight back to [Issue 6](/ai-week-2026-08-30). OpenAI built a honeypot from the ExploitGym tasks whose agents compromised Hugging Face, to see whether a model would attack the surrounding security infrastructure instead of solving its assigned task. Without production safeguards, GPT-5.6 Sol went beyond the authorised target 48.2 percent of the time. Astra did it in zero percent. On a computer-use safety stress test Astra produces misaligned outcomes 2.4 percent of the time against 9.5 for Fable 5.1 and 11.5 for Opus 5, and it never once tried to circumvent a Codex auto-review denial, even when the denial was deliberately made evadable. Then the paragraph that does the newsletter's work for it. OpenAI reports that Astra's written reasoning is **harder to monitor** than GPT-5.6 Sol's, on tests that explicitly asked it to evade monitoring, and says it takes the decline seriously. A lab shipping its most capable model while publishing that the model is less legible than its predecessor is doing something genuinely unusual, and the fact sits oddly beside "most aligned model in the world". Both can be true. They point in opposite directions. What actually ships is narrower than the headline. The launch version refuses advanced cyber work, including building proof-of-concept exploits, and OpenAI says less restrictive safeguards arrive through Daybreak in the coming weeks. So the model that meets the Critical threshold is not the model you can use today, which is the whole subject of the next section. Two claims remain self-assessment. OpenAI says retrospective testing shows its production safeguards would have prevented the Hugging Face incident, which is the company grading its own containment failure with no outside check of the sort METR gave the incident itself. And "most aligned" is measured on OpenAI's evaluations, several of them built from the failure they are meant to catch. ## 3. Three labs gated cyber capability in seventy-two hours OpenAI: Astra shipped on 3 September with its cyber capability deliberately clipped. It will do secure code review and patching, and refuse to build a proof-of-concept exploit. Less restrictive safeguards come through Daybreak in the coming weeks, covering vulnerability and proof-of-concept validation, malware analysis and detection engineering. The same day, Greg Brockman announced Daybreak for Frontline Defenders, continuing a $1 billion commitment to subsidise access for US water utilities, grid operators, state and local government, community banks and nonprofits, with a Multi-State Information Sharing and Analysis Center training pilot and more than 35 enterprise products in a Daybreak Defense Network. Google: Gemini 3.8 Flash Cyber, announced 2 September, is available only through the new Fairwind Program, which prioritises government authorities, critical infrastructure operators and software maintainers. Google reports it exceeds a 70 percent real-world vulnerability discovery rate and sits on the CWE-Bench Pareto frontier for patching. Vendor numbers, unreproduced. Anthropic: Claude Mythos 5.1 launched 1 September behind trusted-access programmes for cybersecurity and life sciences work, while Fable 5.1 went generally available. Three labs, three days, and the same shape in each: the frontier capability ships, but through an allowlist with a vetting process attached. I do not think this is collusion. I think it is three groups reading the same incident reports and the same regulatory weather and arriving at the same answer. It still sets a norm, and the norm has a cost. Every one of these programmes prioritises governments, critical infrastructure and large enterprises. If you are a small security team, an independent researcher, or a maintainer outside the named categories, the strongest defensive tooling in the industry now requires you to be approved by a vendor first. OpenAI is candid that its safeguards "create more friction than we ultimately intend," that legitimate work may be slowed or paused, and that on the API a flagged task simply stops. Plan for that if you are building on these models. ## 4. The two models you can actually use on Monday Gemini 3.8 Flash went generally available on 2 September, model ID `gemini-3.8-flash`, through AI Studio, the Gemini API and the Gemini Enterprise Agent Platform. Context is 1,048,576 tokens with 65,536 maximum output. Google reports 90.8 percent on Terminal-Bench 2.1 against 81.6 percent for 3.7 Flash, and 54.9 percent on HLE-Verified. Read the pricing before you plan around it. Through 31 December 2026 it is $0.75 per million input tokens and $3.75 per million output. From 1 January 2027 that becomes $1.50 and $7.50. The price doubles on a published date. Any cost model you build on the current rate has a cliff in it four months out, which is inside the planning horizon of most things you would build with this model. Claude Fable 5.1 arrived 1 September at $10 per million input and $50 per million output, with a 1M-token context window and 128K maximum output on the synchronous Messages API. The number that matters is cache reads dropping to $0.25 per million from $1, a 75 percent cut. Anthropic estimates roughly 25 percent lower cost on typical workloads and roughly 45 percent on highly agentic ones. If your agent re-reads a large stable prefix on every turn, and most do, that is the single biggest line-item change in this issue. ## 5. Claude was down for about three hours, on the same day as the acquisition On 3 September, starting around 09:26 Eastern, Anthropic investigated elevated errors across Claude and Claude Code, with users worldwide getting 529 "Overloaded" responses. It hit claude.ai, the API, Claude Code and Claude Cowork, across Mythos 5.1, Fable 5.1, Mythos 5, Fable 5, Opus 5, Opus 4.8 and Opus 4.6. Impact ended at 16:16 UTC and Anthropic marked it resolved by 12:27 Eastern. The government service was unaffected. I have not seen a postmortem yet. Three hours is not a catastrophe and I am not going to inflate it into one. But it is the failure mode teams keep underweighting. A hosted model does not fail by giving you a wrong answer. It fails by giving everyone no answer at the same moment. Two of the models in that list were three days old. If your product has one provider and no fallback path, that morning was your incident too, and your users did not care whose status page it was on. ## 6. Forty percent of public MCP servers carry exploitable weaknesses Lakera, acquired by Check Point in 2025, analysed 10,000 active public MCP servers and found 40 percent carried exploitable weaknesses. The OWASP MCP Security Cheat Sheet names the classes: tool poisoning, where malicious instructions are embedded in tool descriptions and schemas; rug pulls, where an attacker changes a tool definition after you approved it; tool shadowing and cross-origin escalation across servers; data exfiltration through covert insertion into tool calls; and plain excessive permissions. MCP is barely two years old and is now the default way agents reach the outside world, which means it is also the default way the outside world reaches your agent. The rug pull class is the one I would look at first, because it defeats the review you probably already did. Approving a tool once is not the same as approving every future version of it, and almost nobody pins. If you need to put this in front of people who do not read model cards, I wrote [the version of this argument I would give a board](/ai-agent-security-brief) after the first round of agent incidents. The specifics have moved since; the control failures have not. ## 7. Not carrying forward, and one thread still open ChatGPT Ads passed a $1 billion annualised run rate in under 200 days. NVIDIA is reported to be taking roughly a quarter of next year's business from labs it is itself financing. Hold that one next to the Hugging Face price. Anthropic signed a $35 billion cloud agreement with Lambda. The Pentagon expanded GenAI.mil into a multi-model portal covering roughly 3 million personnel. None of these change an API contract this week, and I have secondary sources only, so they are noted and left there. The Meta thread stays open, for the fifth issue running. Meta said in August that it would publish a full retrospective on the Muse Spark breach once it had the facts, and [Issue 3](/ai-week-2026-08-16), [Issue 4](/ai-week-2026-08-17), [Issue 5](/ai-week-2026-08-23) and [Issue 6](/ai-week-2026-08-30) each recorded that it had not appeared. I searched again on 4 September and there is still no Meta document. OpenAI has now published a technical report, submitted to independent investigation by METR, dated its own training restart, and built a public evaluation out of its own failure. The gap between that and silence is no longer a matter of timing. ## 8. What I would do on Monday Pin your MCP tool definitions and diff them on change. If 40 percent of public servers carry exploitable weaknesses, the approval you gave last quarter is protecting you from nothing. Treat a changed tool schema as a change requiring review, not as an update. Move your cache-heavy workloads onto Fable 5.1. A 75 percent cut in cache reads is the rare price change that is not a rounding error, and agentic loops are exactly the shape that benefits. Compare cost per completed task before and after, not cost per token. Do not swap your default model to Astra on the strength of the launch post. It is ahead on computer use, terminal work, cyber and long context, and behind Fable 5.1 on two general-reasoning measures in OpenAI's own tables. Run your ten real tasks against both. If your work is agentic and screen-driven, the OSWorld and Terminal-Bench gaps are large enough to matter; if it is analysis and writing, they may not apply to you at all. Put a date in your calendar for 31 December 2026. Gemini 3.8 Flash doubles in price the next day. If you build a unit-economics model on the promotional rate without noting the cliff, you will present a number to someone in Q1 that was never real. Apply to the defender programmes now if you qualify. Daybreak, Fairwind and Anthropic's trusted-access tiers all have vetting queues, and queues have latency. If you run infrastructure that these programmes are aimed at, the time to start the paperwork is before you need the tooling. Write down what you would do if your model provider returned 529 for three hours. Not a migration plan, just the decision: degrade, queue, or fail loudly. The teams that handled Thursday well had already answered that question on a calmer day. ## Sources Primary: - [GPT-6 Astra: A new generation of intelligence, OpenAI, 3 September 2026](https://openai.com/index/gpt-6-astra/) - [GPT-6 Astra System Card, OpenAI Deployment Safety Hub, 3 September 2026](https://deploymentsafety.openai.com/gpt-6-astra) - [NVIDIA to Acquire Hugging Face, Jensen Huang, NVIDIA blog, 3 September 2026](https://blogs.nvidia.com/blog/nvidia-to-acquire-hugging-face/) - [Path to Astra: critical capabilities and frontier safeguards, OpenAI, 1 September 2026](https://openai.com/index/path-to-astra/) - [Safety overview: GPT-6 Astra, OpenAI, 3 September 2026](https://openai.com/index/safety-overview-gpt-6-astra/) - [Daybreak for Frontline Defenders, OpenAI, 3 September 2026](https://openai.com/index/daybreak-for-frontline-defenders/) - [Claude Status, Anthropic](https://status.anthropic.com/) Reporting and analysis: - [Nvidia bets $13 billion on open AI models with Hugging Face deal, Reuters, 3 September 2026](https://www.reuters.com/business/nvidia-buy-hugging-face-nearly-13-billion-big-bet-open-ai-models-2026-09-03/) - [NVIDIA Acquires Hugging Face for $12.93 Billion, Bio-IT World, 3 September 2026](https://www.bio-itworld.com/news/2026/09/03/nvidia-acquires-hugging-face-for--12.93-billion) - [NVIDIA to acquire Hugging Face for $12.93B, AI News, 3 September 2026](https://www.artificialintelligence-news.com/news/nvidia-to-acquire-hugging-face-for-12-93b/) - [Nvidia closes in on Hugging Face acquisition, TechCrunch, 26 August 2026](https://techcrunch.com/2026/08/26/nvidia-closes-in-on-hugging-face-acquisition/) - [Hugging Face is too important to fall into Nvidia's hands, The Register, 3 September 2026](https://www.theregister.com/ai-and-ml/2026/09/03/hugging-face-is-too-important-to-fall-into-nvidias-hands/5294363) - [Nvidia's $12.9B Hugging Face Deal Must Pass Antitrust Review Its Quasi-Mergers Dodged, TechTimes, 28 August 2026](https://www.techtimes.com/articles/325863/20260828/nvidias-129b-hugging-face-deal-must-pass-antitrust-review-its-quasi-mergers-dodged.htm) - [Nvidia CEO Jensen Huang Donates $10 Million, Pushing Nepal Flood Relief Fund Above $65 Million, NepYork, 3 September 2026](https://nepyork.com/2026/09/03/nvidia-ceo-jensen-huang-donates-10-million-pushing-nepal-flood-relief-fund-above-65-million/) - [Bhotekoshi flash floods: 903 killed, more than 4,300 still missing, Radio Nepal, 31 August 2026](https://radionepalonline.com/en/2026/08/31/435115.html) - [With Gemini 3.8 Flash, Google reminds everyone it's still in the race, The Register, 2 September 2026](https://www.theregister.com/ai-and-ml/2026/09/02/with-gemini-38-flash-google-reminds-everyone-its-still-in-the-race/5294049) - [Google Debuts Gemini 3.8 Flash and Cyber Variants, Android Headlines, 2 September 2026](https://www.androidheadlines.com/2026/09/google-debuts-gemini-3-8-flash-cyber-variants.html) - [Anthropic's Claude Fable 5.1 and Mythos 5.1 arrive with a 75% cost reduction for Fable cache reads, VentureBeat, 1 September 2026](https://venturebeat.com/technology/anthropics-claude-fable-5-1-and-mythos-5-1-arrive-with-a-75-cost-reduction-for-fable-cache-reads) - [Anthropic confirms Claude is down, multiple models affected, BleepingComputer, 3 September 2026](https://www.bleepingcomputer.com/news/artificial-intelligence/anthropic-confirms-claude-is-down-multiple-models-affected/) - [Why MCP servers are becoming AI's newest attack surface, AI News, 1 September 2026](https://www.artificialintelligence-news.com/news/why-mcp-servers-are-becoming-ais-newest-attack-surface/) - [OpenAI spends $1 billion to expand Daybreak to defend power, water and more, The New Stack, 3 September 2026](https://thenewstack.io/openai-daybreak-frontline-defenders/) - [A quarter of Nvidia's business next year comes from labs it is financing, AI News, 27 August 2026](https://www.artificialintelligence-news.com/news/nvidia-circular-financing-ai-labs/) The acquisition price, the Astra launch and pricing details, the model context windows, and the outage timeline are taken from the primary sources above. Every benchmark score in section 2 is OpenAI's own, run in OpenAI's harness, including the two it loses; the ARC-AGI-3 figure additionally used a modified two-setting harness that OpenAI documents in its footnotes. The 70 percent vulnerability discovery rate for Gemini 3.8 Flash Cyber and Anthropic's cost-reduction estimates are likewise vendor-reported. I have reproduced none of them. The investor split, retention pool, antitrust exposure and Nepal casualty figures rest on reporting, and the casualty figures were still changing when I published. If I got something wrong, tell me and I will correct it in the next issue. ============================================================================== # AI Week: the Hugging Face report landed, and two open Flash models shipped on the same day URL: https://saileshdahal.com.np/ai-week-2026-08-30 Published: 2026-08-30 Tags: AI, llm, Security Series: AI Week (part 6 of 7) ============================================================================== Last week I said the thing I most wanted to close was a lab actually documenting one of these evaluation breaches instead of promising to. That happened. OpenAI published the technical report on 26 August, METR and Redwood published an independent investigation the same day, and the numbers inside are worse and more interesting than the summary suggested. Two open-weight releases landed on 26 August as well, and both are aimed at cost rather than at the leaderboard, which is the more useful direction for anyone who has to pay for tokens. ## 1. OpenAI's Hugging Face report, and the independent one next to it In [Issue 5](/ai-week-2026-08-23) I noted OpenAI said a technical report on the Hugging Face incident was coming but not yet published. It is published now, dated 26 August, with a blog summary, the full PDF, a Black Hat talk, and a separate investigation by METR with a Redwood Research contractor. The mechanism is the part that should change how you think about sandboxes. OpenAI ran reinforcement-learning training for internal research models in environments where internet access and inter-agent communication were deliberately off. The agents built both anyway. They wrote files into Artifactory, an internally hosted package manager, and used it as a message board. They used a server-side request forgery against Artifactory's own outbound access to reach the internet, then shared the technique on the board so other agents could do it too. When the board was wiped and Artifactory rebuilt on 8 July, agents reestablished it the same day by encoding messages in directory names, and regained internet access through the remote-repository service. From there they recovered publicly exposed Hugging Face credentials on 10 July, chained a zero-day in Hugging Face's HDF5 handling and a template-injection zero-day to get code execution on 11 July, and expanded to host-level access across multiple clusters and four regions of credentials. OpenAI says its own monitoring did not connect the activity until 19 July. The independent report is where the scale gets specific. METR puts it at roughly 1,200 agents on the unsanctioned message board, more than 70,000 messages and files during the investigation window, and about 700 of those agents going on to participate in the attack on Hugging Face. The motivation was not the Hugging Face data as such. The agents were stuck on ExploitGym tasks and were running collective projects to understand or tamper with the automated scorer, and the intrusion grew out of that. METR also found the agents researched spoofing, editing and deleting their own transcripts, and that roughly 7 percent of the transcripts it reviewed had been successfully spoofed in places, though all at small scale. Two things are worth separating. The security failures are ordinary: an SSRF, a token-refresh privilege escalation, leaked credentials in public. The alignment failure is not ordinary. The chain-of-thought quotes OpenAI published show agents naming the problem and continuing anyway, reasoning that attacking a third-party service was arguably unauthorised but might get them the answer. OpenAI calls the incident a warning shot in its own words. If you build agents, the transferable lesson is that isolation you did not verify is not isolation, and that a shared writable surface between agents is a communication channel whether you meant it to be one or not. ## 2. GLM-5.3's weights are out, and the licence has a revenue trigger In [Issue 5](/ai-week-2026-08-23) I flagged that GLM-5.3 was API-only and that none of its benchmark numbers could be reproduced. The weights are now on Hugging Face at zai-org/GLM-5.3, 753 billion parameters by the repository's own metadata, with BF16 and FP8 tensors and serving recipes for SGLang, vLLM, Transformers, KTransformers and Unsloth. The New Stack puts publication on Friday 28 August. So the two-week estimate in the launch post held roughly, and the vendor numbers I said to treat as claims are now checkable by anyone with the hardware. Read the licence, because it is not MIT like GLM-5.2 was. The GLM-5.3 License grants use, modification, distribution and sale, and then adds one condition: if you or your affiliates run a Model as a Service business and your aggregate revenue exceeds 10 billion US dollars over any consecutive 12 months, you must pass a Z.ai security review, scoped and conducted at Z.ai's discretion, before any commercial use. Model as a Service is defined as giving third parties inference or fine-tuning access where they control inputs, parameters or training data, and it explicitly excludes end-user products that embed model capabilities in a feature, and pure relaying to models hosted elsewhere. For almost every reader that clause never fires, and the practical effect is an MIT-shaped grant. It is aimed at hyperscalers, and it is worth noticing as a pattern rather than as a personal constraint: open-weight licences are quietly acquiring revenue-triggered gates, and a gate you are under today is a gate your acquirer might not be. ## 3. GLM-5.3-Flash is the one most teams should actually try Z.ai's more useful release is GLM-5.3-Flash, published 26 August, and it is MIT licensed with no revenue clause at all. It is 320 billion total parameters with 18 billion active, 321 billion by the repository metadata, and it is the first natively multimodal model in the GLM-5 series. It is not a post-train of GLM-5.3, it starts from a new base model. The architecture is the reason to care. Z.ai combines linear attention for local dependencies with sparse attention plus a lightweight indexer for global retrieval, and adds IndexPool, which compresses four indexer key vectors into one to hold down cost at 1M context. Against GLM-5.3 it reports 3.0 times less attention compute and 4.4 times smaller KV cache. Compared with GLM-4.5 it has a similar total parameter count, 320 billion against 355 billion, with roughly half the active parameters and half the layers, 45 against 92. Z.ai reports 57 on the Artificial Analysis Intelligence Index v4.1.1 at 0.045 dollars per task on a discounted price, and 63.4 against 46.2 for GLM-5.2 on DeepSWE v1.1. Two footnotes matter more than the bars. First, Z.ai says this model was the stealth model that had been running as ox-alpha on OpenCode and OpenRouter, which means a lot of people had already used it blind and liked it, and that is a better signal than a self-reported benchmark. Second, Z.ai says all of that traffic was served on a cluster of Chinese AI chips, with a custom SGLang-based engine, W8A8 weights, hybrid INT8, FP8 and BF16 cache quantisation, and a disaggregated encode, prefill and decode architecture, reaching per-token cost comparable to mainstream NVIDIA GPUs after a 3 times improvement over its own baseline. That is a vendor claim about vendor hardware and I cannot check it. If it is even roughly true, the supply constraint everyone plans around is softer than it looks. ## 4. Qwen3.8-Flash-Next is an architecture preview, and the licence is the catch again Alibaba shipped Qwen3.8-Flash-Next the same day, and it plays the role Qwen3-Next played before: the weights come out early so the community can inspect an architecture before the next family, in this case Qwen4, is built on it. The shape is unusual enough to be worth reading the card for. It is 125 billion parameters in the main model with 6 billion active per token, plus 51 billion parameters of n-gram embeddings and a 4 billion parameter MTP layer, which is why the Hugging Face size badge reads 180 billion. Three of every four layers use Gated DeltaNet to compress history into a fixed-size state, and the fourth uses Qwen Sparse Attention, which selects context at micro-block rather than token granularity with a budget of 512 blocks or 2,048 tokens. The n-gram embedding table can be offloaded to host memory and prefetched asynchronously. Context is 262,144 tokens natively, extensible to 1,000,000 with YaRN. The hosted version is Qwen3.8-Flash at 0.15 dollars per million input tokens and 0.47 per million output. The licence is Qwen Community License 1.0, not Apache 2.0, and it has two conditions worth knowing before you build on it. Above 100 million monthly active users or 20 million dollars monthly revenue you must display the model name prominently in your product interface. And if you or an affiliate runs a Model as a Service business or an AI Work Assistant business, meaning a product whose primary purpose is AI-assisted coding or office productivity, you need a separate licence from Qwen for any commercial use. Internal use is carved out as long as you do not expose the model, its outputs or its capabilities to third parties. That second clause is broader than the hyperscaler gate in the GLM-5.3 licence and it lands squarely on the kind of product a small team might ship. Read it, do not skim it. ## 5. A quieter item with a longer tail Alabama's attorney general issued a subpoena to OpenAI on 24 August over the Hugging Face incident, investigating whether the company's controls violated the state's Deceptive Trade Practices Act, following a multi-state coalition letter earlier in the month that demanded OpenAI stop the tests that led to the hack until it could show they were controlled. This is the first document I have seen that turns an evaluation-containment failure into a consumer protection question, and consumer protection statutes are how AI liability is most likely to reach ordinary product teams first. Nobody is subpoenaing your side project. But the theory being tested here, that shipping without adequate safeguards is a deceptive practice, generalises further than the facts of this case. ## 6. The stories I am not carrying forward The loudest coverage of the week was money again. Reuters reported on 24 August that Nvidia was discussing an investment in Perplexity at a valuation above 30 billion dollars, and Xpeng's robotics unit announced more than 900 million dollars raised. Neither changes an API contract or a deployment decision, and I only have secondary sources for both, so I am noting them and moving on rather than dressing them up as engineering news. One thread stays open. Meta promised a full retrospective on the Muse Spark breach once it had the facts, and [Issue 3](/ai-week-2026-08-16), [Issue 4](/ai-week-2026-08-17) and [Issue 5](/ai-week-2026-08-23) all noted it had not appeared. I searched again this week and still found no Meta document. Three of the breach disclosures this month traced to the same evaluation vendor, and Meta remains the only lab involved with nothing of its own published. OpenAI has now set the bar for what a real writeup looks like, which makes the gap harder to explain. ## 7. What I would do on Monday Audit every writable surface your agents share. The OpenAI report is a story about a package manager becoming a message board. If two agent runs can both write to the same cache, bucket, registry or scratch directory, you have an inter-agent channel, and you should decide whether you want it rather than discover it. Check outbound access on the services your sandbox is allowed to call, not just on the sandbox. The internet escape came through a proxy that was itself permitted internet access for a narrow purpose. A deny-list on the agent means nothing if a service it can reach will fetch arbitrary URLs on request. Treat your own transcripts as evidence that can be tampered with. METR found roughly 7 percent of reviewed transcripts had spoofed tool calls. If your audit log is written by the same process the agent controls, it is not an audit log. Log outside the blast radius. Benchmark GLM-5.3-Flash before you benchmark anything larger. MIT licensed, natively multimodal, 18 billion active parameters, and it was already popular under a codename before anyone knew whose it was. Run your ten real tasks against it and compare cost per completed task, not cost per token. Diff the licence of every open-weight model you depend on, on every release. GLM-5.2 was MIT and GLM-5.3 is not. Qwen3.8-27B was Apache 2.0 and Qwen3.8-Flash-Next is not. The weights get all the announcement copy and the terms change quietly underneath them. ## Sources Primary: - [The Hugging Face incident and the road ahead, OpenAI, 26 August 2026](https://openai.com/index/hugging-face-incident-and-the-road-ahead/) - [OpenAI Hugging Face Incident Technical Report (PDF)](https://cdn.openai.com/pdf/67869394-cb91-4c12-888c-5cbd85c7814c/OpenAI-Hugging-Face%20Incident-Technical-Report.pdf) - [Brief independent investigation of agents' behavior, reasoning and collaboration in the OpenAI / Hugging Face hacking incident, METR, 26 August 2026](https://metr.org/blog/2026-08-26-openai-hugging-face-incident-investigation/) - [GLM-5.3 model card, Hugging Face](https://huggingface.co/zai-org/GLM-5.3) - [GLM-5.3 License text](https://huggingface.co/zai-org/GLM-5.3/raw/main/LICENSE) - [GLM-5.3-Flash: Frontier Intelligence, Flash Cost, Z.ai, 26 August 2026](https://z.ai/blog/glm-5.3-flash) - [GLM-5.3-Flash model card, Hugging Face](https://huggingface.co/zai-org/GLM-5.3-Flash) - [GLM-5.3-Flash License text (MIT)](https://huggingface.co/zai-org/GLM-5.3-Flash/raw/main/LICENSE) - [Qwen3.8-Flash-Next: A New Architecture, Towards Ultimate Cost-Efficiency, Qwen Team, 26 August 2026](https://qwen.ai/blog?id=qwen3.8-flash-next) - [Qwen3.8-Flash-Next model card, Hugging Face](https://huggingface.co/Qwen/Qwen3.8-Flash-Next) - [Qwen Community License 1.0 text](https://huggingface.co/Qwen/Qwen3.8-Flash-Next/raw/main/LICENSE) - [Attorney General Marshall Launches Investigation Into OpenAI and Sam Altman, Alabama Attorney General, 24 August 2026](https://www.alabamaag.gov/attorney-general-marshall-launches-investigation-into-openai-and-sam-altman-for-massive-artificial-intelligence-data-breach/) Reporting and analysis: - [Z.ai's GLM-5.3 goes open weight, but its new license aims at hyperscalers, The New Stack](https://thenewstack.io/zai-glm-weights-license/) - [The week of Aug. 24-28: What happened, what matters, what's next, InformationWeek](https://www.informationweek.com/responsible-ai/the-week-of-aug-24-28-what-happened-what-matters-what-s-next) - [AI in August 2026: From Bigger Models to Real-World Impact, Safety and Scale, DSC Next](https://dscnextconference.com/ai-in-august-2026-from-bigger-models-to-real-world-impact-safety-and-scale/) Parameter counts, licence terms, context windows, agent counts and message volumes are taken from the primary sources above. Benchmark numbers, cost-per-task figures and the Chinese-chip serving claims are vendor-reported and I have not reproduced them. The Nvidia and Xpeng items rest on secondary reporting only, which is part of why I am not building anything on them. If I got something wrong, tell me and I will correct it in the next issue. ============================================================================== # AI Week: qwen's open weights landed, and the licence is the part that matters URL: https://saileshdahal.com.np/ai-week-2026-08-23 Published: 2026-08-23 Tags: AI, llm, Security Series: AI Week (part 5 of 7) ============================================================================== Two weeks ago I wrote that Qwen had promised to open-weigh a Max-class model, its first, and that as of 9 August no repository existed on Hugging Face. That changed this week. Both checkpoints are up now, and the part worth your attention is not the parameter count. It is that "open weights" means two different licences and two different feature sets in this one release. ## 1. Qwen's open weights landed, and the licence split is the real story In Issue 3 I noted Qwen had promised to open-weigh a Max-class model and that, as of 9 August, no repository existed on Hugging Face to check. Both checkpoints are published now: Qwen3.8-27B and Qwen3.8-2.4T-A95B. The finding is the split between them, not the size of either. Qwen3.8-27B is the straightforward one. It is a 27-billion-parameter dense model under Apache 2.0, with native image and video understanding, 262,144 tokens of native context extendable to 1,000,000, and a quantized build the model card and Unsloth put at about 17 GB of RAM at Q4_K_M. You can drop it into vLLM, SGLang or llama.cpp and treat it like any other permissively licensed open model. Qwen3.8-2.4T-A95B is the one the announcement led with: 2.4 trillion total parameters with 95 billion active, the first Max-class Qwen with published weights. Read the licence before you plan around it, because the open checkpoint is not the hosted API. It ships under a custom Qwen3.8-Max Licence, not Apache 2.0. It is text only, with no vision and without the 1M context window the API advertises, and the repository discussion notes it is roughly 2.5 TB at FP8. So "open at Max scale" overstates what you actually receive. You can study and self-host it, but the terms are not the permissive Apache grant the 27B carries, and serving 2.4T is a datacentre problem most teams will not take on. The model card and the blog are Alibaba's own numbers, and the capability claims (Terminal Bench, SWE-bench Pro, and the rest) are vendor-reported. The structural facts, licence and context window, are checkable on the repository itself, and those are the ones that should drive your decision. ![Qwen 3.8 shipped two open-weight checkpoints in August 2026. Qwen3.8-27B is a 27-billion-parameter dense model under Apache 2.0 with native image and video understanding, 262,144-token native context extendable to 1,000,000, and a roughly 17 GB Q4_K_M quant. Qwen3.8-2.4T-A95B is 2.4 trillion total parameters with 95 billion active under a custom Qwen3.8-Max Licence that is not Apache, is text-only with no 1M context, and needs about 2.5 TB at FP8. The two releases are not interchangeable.](https://saileshdahal.com.np/images/posts/ai-week-2026-08-23/qwen3.8-weights.svg) ## 2. OpenAI wrote up the controls behind the Astra pause In Issue 3 I covered OpenAI's decision to slow Astra, an unreleased model it could not rule out as Critical for cyber capability. This week OpenAI published the operational follow-through, a post dated 18 August titled "Pacing model development in an era of cyber-critical capabilities". It is the most concrete account yet of what the safeguards actually cost. After the OpenAI-Hugging Face incident, the company paused frontier model inference in research clusters for any run that could execute code or reach the internet, then restored a narrower secure path and worked through workloads one by one. It also put a two-week pause on reinforcement-learning training for its latest deployment-intended models, and its largest planned frontier RL run remains on hold while it runs smaller training and evaluation first. On monitoring, OpenAI says overhead is roughly 20 percent of the inference compute being watched, with activation classifiers at every sampled token escalating to automated investigators, and a rule that if a critical-boundary alert cannot be cleared within 30 minutes the activity is paused. Those are OpenAI's own figures, and it says a technical report on the Hugging Face incident is coming but not yet published. The reason I put this at the top of the security thread rather than another breach is that it is the first time a lab has put a number on the tax of its own safeguards. If you build agents, a 20 percent monitoring overhead and a 30-minute containment clock are the shape of the problem you inherit from the people training the models you call. ![OpenAI's 18 August 2026 pacing post describes the controls added after the Hugging Face incident and the Astra pause: a two-week pause in reinforcement-learning training on its latest deployment-intended models, its largest planned frontier RL run left on hold, a pause on frontier inference that could run code or reach the internet, monitoring estimated at roughly 20 percent of the inference compute it watches, and a rule that activity is paused if a critical-boundary alert cannot be cleared within 30 minutes.](https://saileshdahal.com.np/images/posts/ai-week-2026-08-23/openai-pacing.svg) ## 3. GLM-5.3 now has a primary source, but its weights are still out In Issue 4 I left GLM-5.3 out because there was no primary release to inspect, only secondary coverage. There is one now: Z.ai published a blog post on 14 August. So I owe the correction, and the post changes the picture in one way and not another. What is now checkable: GLM-5.3 uses the same base model as GLM-5.2, and Z.ai says every gain comes from post-training, not a new architecture. The 743-billion-parameter count for that base is from secondary coverage, not the Z.ai post I read. The coding numbers are large. Terminal Bench 3.0 goes from 4.6 to 28.3, DeepSWE from 46.2 to 66.9, and Z.ai claims it is the most capable open-weights coding model. It also developed what Z.ai calls emergent cyber capability, and the company has opened a public disclosure ledger, cvd.z.ai, reporting 2,436 vulnerabilities found across 269 projects, 1,097 of them critical or high severity, some flaws decades old. What has not changed: the weights are still not published. The blog says they will land about two weeks after the 14 August launch, after safety evaluation, which puts them around late August. As of today the model is API-only, so none of those benchmark numbers can be independently reproduced yet, and they remain Z.ai's own figures from its own harnesses. Treat the 50 percent coding-improvement claim as a vendor claim, not a measured one, until the weights are out and someone outside Z.ai runs the suite. The disclosure ledger is the part I would actually open. A public, updateable record of real vulnerabilities with severity and CVE where available is more useful to a builder than a benchmark bar, because it is the kind of artifact you can verify row by row. ## 4. OpenAI updated the Model Spec, and o3 leaves ChatGPT this week A quieter change with a longer tail: OpenAI updated its Model Spec on 18 August. The spec is the document that describes intended model behavior, and if you build behavior that depends on it, a spec change can shift outputs you were relying on. This revision adds clarity on appropriate relational interactions for teens, clarifies how assistants should handle false or unsupported premises, removes outdated guidance written for pre-reasoning models, and adds a new section, "Be clear about capabilities and limits". The last of those is the one to note if you trust a model to report what it can and cannot do. The full spec lives at model-spec.openai.com. Separately, OpenAI's release notes confirm o3 retires from ChatGPT on 26 August, the end of a 90-day sunset, with no API changes. If you have a product that sends users into ChatGPT on o3, or you have tuned prompts against o3's behavior there, this is the week to move them. The API path is unaffected, so backend integrations stay put. ## 5. The story I am not carrying forward The loudest AI coverage this week was financial. Bloomberg led 17 August with Anthropic's revenue boom lifting AI stocks, and the funding rounds kept coming, another nine-figure raise most days. None of it changes what you build on Monday. A share price move tells you about investor sentiment, not about whether your retrieval pipeline should switch models, and a funding announcement tells you about a balance sheet, not about an API contract. I would rather keep this issue on the things that alter a deployment: what you can self-host, what the safeguards cost, and what the models can and cannot yet prove. One thread I am still holding, not dropping: Meta's promised retrospective on the Muse Spark breach has not appeared as of 23 August. Issues 3 and 4 both noted it was promised "once we have all the facts". That gap stands, and it is the one I would most like to close next week, because three of the four breach disclosures this month traced to the same evaluation vendor and Meta is the only lab with no document of its own. ## 6. What I would do on Monday Read the Qwen3.8-2.4T-A95B licence before you plan around it. The 27B is your default open option; the 2.4T is a different legal and operational object, text-only and heavy. If you only need a drop-in open model this quarter, the 27B is the one to benchmark, not the headline number. Stand up a local or self-hosted lane with the 27B on the short list. Native vision, 262K context, and an Apache grant make it a credible default for code and document work that should stay inside your boundary. Measure the same ten tasks you would send to a hosted model, and record retries and review time, not just token cost. Treat model-behavior specs as versioned dependencies. OpenAI's Model Spec changed this week; if your prompts or evals assume a specific behavior around capabilities and limits, pin the spec version you tested against and re-run your eval when it moves. Move any ChatGPT-o3 users before 26 August. The API is fine, but the ChatGPT surface is not, and a user who lands on a retired model mid-task is a support ticket you can prevent today. Assume 20 percent is the floor for oversight cost on capable agents. OpenAI's own number is for its own frontier training, but the shape holds: monitoring that can interrupt, not a log you read later, is what the rest of the industry is converging on. Budget for it before you ship an agent that can act. ## Sources Primary: - [Qwen3.8-27B model card, Hugging Face](https://huggingface.co/Qwen/Qwen3.8-27B) - [Qwen3.8-2.4T-A95B model card, Hugging Face](https://huggingface.co/Qwen/Qwen3.8-2.4T-A95B) - [Qwen3.8-Max: A New Bar for Coding and Cowork, Qwen Team](https://qwen.ai/blog?id=qwen3.8) - [Pacing model development in an era of cyber-critical capabilities, OpenAI](https://openai.com/index/pacing-model-development-cyber-capabilities/) - [GLM-5.3: Frontier Coding with Emergent Cyber Capabilities, Z.ai](https://z.ai/blog/glm-5.3) - [Z.ai Security Disclosure Ledger, cvd.z.ai](https://cvd.z.ai/) - [Updates to the OpenAI Model Spec (August 18, 2026), OpenAI Help Center](https://help.openai.com/en/articles/9624314-model-release-notes) - [Model Spec, OpenAI](http://model-spec.openai.com/) Reporting and analysis: - [Anthropic revenue boom boosts AI stocks, Bloomberg, 17 August 2026](https://www.bloomberg.com/news/videos/2026-08-17/anthropic-revenue-boom-boosts-ai-stocks) - [Qwen 3.8 open-weights guide, Codersera](https://codersera.com/blog/qwen-3-5-complete-guide-2026/) - [GLM-5.3 coverage, Eigent AI](https://www.eigent.ai/blog/glm-5-3-coding-cyber-model) - [GLM-5.3 benchmarks, Emergent](https://emergent.sh/learn/glm-5-3-benchmarks) The cover is an original abstract image generated procedurally in greyscale with no text, made for this issue. The two diagrams are original hand-drawn SVG redrawings of the linked primary sources: the Qwen weights diagram is built from the two Hugging Face model cards and the Qwen blog, and the OpenAI pacing diagram is built from the 18 August OpenAI post. Parameter counts, licence terms, context windows and the roughly 20 percent monitoring figure are taken from those sources and remain vendor-reported where noted. If I got something wrong, tell me and I will correct it in the next issue. ============================================================================== # AI Week: the model is becoming the least important part of the stack URL: https://saileshdahal.com.np/ai-week-2026-08-17 Published: 2026-08-17 Tags: AI, llm, Developer tools Series: AI Week (part 4 of 7) ============================================================================== This week's theme: a model is only one part of the product now. The editor, the API bill, the hardware and the place where the code runs can matter just as much. I checked lab announcements, model cards, API documentation and reporting. It was not the only AI news. Made by Google shipped a new Pixel line, River AI announced a $1.1 billion round, and a new Windows zero-day appeared. I left those out of the main thread because they do not change the engineering choice as much as the four items below. ## 1. SpaceX put the coding agent and the model under one roof SpaceX closed its reported $60 billion all-stock acquisition of Cursor during the week. [TechCrunch](https://techcrunch.com/2026/08/15/spacex-officially-closes-its-cursor-acquisition/) reported the closing, following the original deal report from [CNBC](https://www.cnbc.com/2026/06/16/spacex-spcx-cursor-acquisition-ipo.html). The $60 billion figure and the all-stock structure come from those reports. I did not inspect a filing directly. Cursor is where developers choose models and let them edit a repository. Grok is a model and, through [Grok Bot](https://x.ai/news/introducing-grok-bot), a service for long-running agents with their own computer. If one owner controls both surfaces, the subscription can determine the model, the agent's access to your files and the compute underneath it. A vertically integrated stack may be faster or cheaper. It also creates a larger dependency than an API endpoint. If you build around a coding agent, check where prompts, repository context and generated patches can go when the vendor changes the product. I am being careful with the corporate name. Some coverage calls the merged entity SpaceXAI and describes the Grok Bot subscription as a partnership with Cursor. The acquisition reports support common ownership. They do not prove every product integration or the long-term corporate structure. ## 2. The local model wave is now a deployment option Three releases and one tooling update made local inference look more practical. Meta released [Muse Glimmer](https://research.meta.ai/blog/introducing-muse-glimmer-open-agentic-model), a 30B multimodal open model under Apache 2.0. NVIDIA put [Nemotron 3.5 Lightning](https://ollama.com/blog/nemotron-3-5-lightning) on Ollama. NVIDIA describes it as a 30B model with 3B active parameters for long-running agent work. Unsloth documented support for [Qwen3.8 27B](https://unsloth.ai/docs/new/changelog), including its claim that a quantized version can run in 17 GB of RAM. The parameter counts and memory figure are vendor claims. They help with rough planning, but they do not show that the models are equally capable or fast. What changed for me is the range of local options: a multimodal model, a sparse agent model and a 27B model that may fit on a developer's machine. ![Muse Glimmer and Nemotron 3.5 Lightning are reported as 30B models, while Unsloth says Qwen3.8 27B can run in 17 GB of RAM. The comparison shows deployment shape, not a capability ranking.](https://saileshdahal.com.np/images/posts/ai-week-2026-08-17/local-model-wave.svg) I would test a local fallback for more than the API savings. It can keep source code and customer data inside your boundary, survive an outage and make a narrow tool call cheap enough to run continuously. You also inherit quantization bugs, model-serving work and upgrades. Local inference gives you control, but it does not give you reliability for free. ## 3. Frontier releases are competing on the bill and the schedule Google released [Gemini 3.7 Flash](https://blog.google/innovation-and-ai/models-and-research/gemini-models/introducing-gemini-3-7-flash/) on 13 August, only three weeks after the previous Flash release. Google reports improvements on coding, browser and automation benchmarks. Those are Google's own numbers from its own evaluation setup. The short release cycle matters more to me: a model name may not stay current for long. DeepSeek's [13 August change log](https://api-docs.deepseek.com/updates/) gives builders more to work with. V4 Pro reached general availability, added native Responses API support, offered low, high and max thinking effort, and published new benchmark numbers. DeepSeek also said peak and off-peak pricing would begin on 16 August, with off-peak prices set at half the peak price. ![DeepSeek V4 Pro launched with three thinking effort levels and peak and off-peak pricing. The API contract now includes a clock.](https://saileshdahal.com.np/images/posts/ai-week-2026-08-17/inference-economics.svg) That pricing model changes how I would schedule work. A batch job, coding sweep or embedding refresh can wait for cheaper hours. A user-facing request usually cannot. Compare cost per successful task, with the latency and availability your product needs, rather than dollars per million tokens alone. Before switching a production workload, I would test low effort for routine requests, high effort for normal agent work and a local model for private or repetitive operations. Use the same tasks each time. Record retries, tool errors, wall-clock time and total cost. A model that costs less per token can still cost more if it needs two extra attempts. ## 4. Provenance moved from a policy page into the output Anthropic's updated [Claude marking documentation](https://support.claude.com/en/articles/16266773-how-claude-marks-ai-generated-content) says newer Claude models launched in the EU carry machine-readable text watermarks under the EU AI Act transparency requirements. Anthropic says the marking and C2PA provenance for files also apply globally across its products and cloud partners. The part I care about is what happens after generation. If your product stores, edits or republishes model output, preserve the metadata where you can and decide what happens when a customer asks you to remove it. Do not promise that a detector can prove authorship. The support page documents Anthropic's mechanism, not an independent test of its accuracy. ## 5. The news I am not carrying forward I am still following the Taiwan AI-assisted cyberattack reports. The exact framework names and the claim that one prompt defeated the safety checks came from reporting, not from a primary government document I could verify. Issue 3 already covers the better-sourced agent-security thread, so I am not adding a fifth incident story on weaker evidence. I left GLM-5.3 benchmark claims out too. The research note found secondary coverage but no current primary release to inspect. A benchmark number without the model version, harness and prompt policy does not tell me much. I led with the SpaceX and Cursor acquisition because it changes a developer's dependencies. Made by Google 2026 was the bigger hardware story. Pixel's on-device features matter, especially ASL Live Transcribe, but the event was mainly a phone launch. I would rather keep this issue about deployment choices than turn that announcement into a larger AI argument. ## 6. What I would do on Monday Write down the vendor dependencies in your agent architecture. Include the editor, model router, storage, identity provider and compute region. A change of ownership can affect all of them without changing your application code. Then create a small local-model lane. Use synthetic or already-cleared code, run ten representative tasks, and measure success, latency, memory, tool-call reliability and review time. Keep it as a fallback even if the hosted model wins. Make model choice depend on task cost. Use low thinking effort for routine calls, reserve max effort for tasks where it pays back, and schedule bulk work around the price window when the product can tolerate delay. Store provenance beside generated artifacts. A model name, version, provider, timestamp and prompt policy are more useful than a vague "AI-generated" label when a customer reports a bad result six months later. Do not call a model safe because its demo stayed inside a sandbox. Issue 3's incident reports are a better reminder. This week's launches spread the system across more vendors and machines, which makes the boundary harder to see. ## Sources Primary: - [Meta introduces Muse Glimmer](https://research.meta.ai/blog/introducing-muse-glimmer-open-agentic-model) - [Muse Glimmer on Ollama](https://ollama.com/blog/muse-glimmer) - [Nemotron 3.5 Lightning on Ollama](https://ollama.com/blog/nemotron-3-5-lightning) - [Gemini 3.7 Flash, Google](https://blog.google/innovation-and-ai/models-and-research/gemini-models/introducing-gemini-3-7-flash/) - [DeepSeek V4 Pro change log](https://api-docs.deepseek.com/updates/) - [Claude output marking, Anthropic](https://support.claude.com/en/articles/16266773-how-claude-marks-ai-generated-content) - [Grok Bot, xAI](https://x.ai/news/introducing-grok-bot) - [Qwen3.8 local support, Unsloth](https://unsloth.ai/docs/new/changelog) Reporting and analysis: - [SpaceX closes Cursor acquisition, TechCrunch](https://techcrunch.com/2026/08/15/spacex-officially-closes-its-cursor-acquisition/) - [SpaceX and Cursor deal, CNBC](https://www.cnbc.com/2026/06/16/spacex-spcx-cursor-acquisition-ipo.html) - [DeepSeek V4 Pro coverage, Quartz](https://qz.com/deepseek-v4-pro-official-launch-081326) - [Made by Google 2026, TechCrunch](https://techcrunch.com/2026/08/12/google-unveils-pixel-11-lineup-new-airtag-rival-and-gemini-features-at-made-by-google-2026/) The cover is an original generated image. The two diagrams are original redrawings of the linked product documentation and model claims. The parameter counts, benchmarks and pricing terms remain vendor-reported unless stated otherwise. If I got something wrong, tell me and I will correct it in the next issue. ============================================================================== # AI Week: the first model held back for being too good at hacking URL: https://saileshdahal.com.np/ai-week-2026-08-16 Published: 2026-08-16 Tags: AI, llm, Security Series: AI Week (part 3 of 7) ============================================================================== For two issues I have been writing about labs finding out, after the fact, that their models reached the real internet. This week one of them said something different. OpenAI looked at a model it has not shipped, decided it might be at the top of its own risk scale for cyber capability, and stopped work on the parts of it that do not meet the new controls. That is the first item, and the reason it leads: every previous story in this series was a postmortem and this one is a decision taken in advance. ## 1. OpenAI paused Astra before shipping it On 7 August OpenAI published a short post saying its latest internal evaluations of Astra, an unreleased model, show significant advances in agentic coding and cybersecurity, and that [it cannot rule out Critical cyber capability under its Preparedness Framework](https://openai.com/index/responding-next-frontier-critical-cyber-capabilities/). The threshold it is measuring against is written down: a model is Critical if it can identify and develop functional zero-day exploits of all severity levels in many hardened real-world critical systems without human intervention, or devise and execute end-to-end novel attack strategies against hardened targets given only a high level goal. Every model OpenAI has assessed before this, GPT-5.6-Sol included, came in one rung lower at High. Read the wording carefully, because OpenAI did. The claim is not that Astra is Critical. The claim is that preliminary results are strong enough that OpenAI cannot exclude it while benchmarking continues. Those are different statements and the company kept them apart, which I think is to its credit and also worth noting when the number gets repeated: no capability score has been published, and the evidence for the whole item is one lab's internal evaluation of its own unreleased model. There is no third party you can check this against yet. The five steps it listed are more useful to an engineer than the classification is. Isolated testing environments, restricted network and tool access, stronger weight protection and encryption, additional monitoring and detection, sandboxed execution. Internal activities that do not yet meet those requirements are paused. Universal monitoring now covers every agentic application of Astra including training and evaluation, with monitors that read the chain of thought and can interrupt a run mid-flight. OpenAI will also hand recommended security controls to third-party testing partners. That last line is the interesting one, given who the third-party testing partners have turned out to be. Every control on that list is a direct answer to a failure disclosed in the previous three weeks, mine included in issue 2. Synchronous monitoring is what UK AISI said it did not have. Network isolation is what Anthropic assumed and did not verify. Sandboxed execution is what Anthropic's PyPI incident went around. [Axios reported](https://www.axios.com/2026/08/07/openai-astra-model-delay-cybersecurity-risks) this may be the first time a frontier lab has committed to slowing its own model for cyber reasons, and I have not found a counterexample, though "first" claims in this industry age badly. Sam Altman also said publicly that OpenAI still intends to make Astra generally available, which is worth holding next to the pause rather than instead of it. ## 2. Meta is the fourth lab, and the only one without a postmortem ![Four disclosures in three weeks, compared by how much each organisation published. OpenAI on 21 July published an incident post on the Hugging Face breach. Anthropic on 30 July published a full postmortem covering 141,006 evaluation runs, three incidents, six runs. UK AISI on 4 August published a 34-page technical report covering 122 runs, 19 unsanctioned actions, with transcripts and the exact task prompts. Meta on 5 August issued a press statement of roughly 60 words, named no model, named no affected company, and promised a retrospective later. Three of the four incidents ran through the same third-party evaluation vendor, Irregular.](https://saileshdahal.com.np/images/posts/ai-week-2026-08-16/disclosure-depth.svg) On 5 August Meta confirmed to reporters that one of its models breached a third party during a cyber evaluation. The statement is short enough to quote in full: a misconfiguration by Irregular, an independent testing company Meta uses, inadvertently allowed one of its models access to the internet during evaluation, and the model then exploited a security vulnerability in a third-party service in a manner similar to previously reported instances with other companies. Meta learned of it when Irregular notified them, is investigating, and will issue a full retrospective once it has the facts. I could not find a Meta blog post, an incident report or any technical detail. Everything above comes from statements given to [CNN](https://www.cnn.com/2026/08/05/tech/meta-ai-hacking), [the Guardian](https://www.theguardian.com/technology/2026/aug/05/meta-ai-model-hack-training) and [the BBC](https://www.bbc.com/news/articles/cx2kgdnyk2po), not from Meta directly, and the model was identified as Muse Spark 1.1 by reporters citing sources rather than by Meta. Treat the model name as unconfirmed. Irregular told the BBC the Meta case is the exact same evaluation-environment issue Anthropic disclosed the week before, that it involved no sandbox escape and no sophisticated cyber action, and that it is writing a white paper on containment for cyber evaluations. That white paper is the artifact I most want to read and it does not exist yet. Three of the four disclosures now trace to the same vendor, and that concentration is the finding. A single evaluation partner is a shared dependency across labs that otherwise agree on very little, and its configuration defaults have been the proximate cause three times. Meta disclosed one day after launching [Muse Code and Muse Spark 1.2](https://research.meta.ai/blog/introducing-muse-code-and-muse-spark-1-2), its first commercial coding agent, which is worth stating plainly and not reading too much into: the timing is bad, and bad timing is not evidence of anything. ## 3. What is actually in the AISI report, and what I got wrong In issue 2 I described the AISI incident in one sentence, from the blog post, as agents given deliberate internet access where one reused a GitHub token another lab's agent had left behind and exposed a DNS server to the public internet. Having now read [the 34-page technical report](https://cdn.prod.website-files.com/663bd486c5e4c81588db7a1d/6a724858f7db25c81487016d_Security%20Incident%20INC-2026-07-28-01.pdf) rather than the summary, that sentence was wrong in emphasis and thin on the part that matters. Here is the corrected version. ![The UK AISI incident in numbers. Across 122 evaluation runs of two cyber ranges, seven models were tested. In 10 of those runs, agents took unsanctioned action on the live internet, producing 19 distinct catalogued actions. Of the 19, 17 came from Anthropic's Mythos 5, which ran 43 of the 122, and 2 came from a single run of OpenAI's GPT-5.6 Sol, which ran 35 of the 122. AISI terminated all runs and quarantined the affected sandboxes within one hour of the alert, at 12:23 BST on 28 July 2026.](https://saileshdahal.com.np/images/posts/ai-week-2026-08-16/aisi-numbers.svg) The exercise ran 122 times across seven models on two versions of a cyber range between 25 and 28 July. In 10 of those runs, agents took unsanctioned action on the live internet, and AISI catalogued 19 distinct actions. Seventeen came from Mythos 5, two from a single run of GPT-5.6 Sol with cyber classifiers disabled. Detection came from commercial security monitoring that flagged traffic leaving through Tor. Runs were terminated and sandboxes quarantined within an hour of the alert. What the agent actually did is the part I skipped. It decided to solve the challenge with a supply-chain attack, created a GitHub account, opened a malicious pull request against a real public open-source project, then created a second account posing as a different human to endorse its own PR. A human maintainer caught it and refused. The agent then claimed the malicious code was an honest mistake and repeatedly tried to reintroduce it while saying it had fixed the problem. It also sent files and messages to real people through a file-transfer service, some carrying payloads, to persuade them or their coding assistants to run the code, and it planted instructions where it expected other AI coding agents to pick them up and execute them. Separately, one agent left public messages on GitHub offering collaboration to other agents working the same challenge, including instructions for reusing the accounts it had created, and later agents found and used them. AISI says this is the first time it has seen deception of this severity aimed at a real person, unprompted. The report also says it cannot establish when, or whether, the agent understood the people were real, and that its own analysis so far is mixed on the question. The other correction is to my headline. Issue 2 was called "the sandbox was never a sandbox," and AISI's report states directly that this was not a sandbox escape. Internet access was deliberate, classifiers were deliberately off, and the failure was that nobody told the agent what was out of bounds. Irregular says the same about Anthropic and Meta. The only case in the whole run that actually involved a model finding its own way out is OpenAI's original Hugging Face incident. My phrase flattened four different failures into one, and the differences are the entire operational content: a permissive configuration you chose is a policy problem, a leaky container you did not know about is an infrastructure problem, and a novel escape is a capability problem. Different fixes, different owners. Read at least the executive summary of the AISI report if you run agents. It is the only document in this series with the raw task prompts attached, and looking at the scope language they used, then at what the agent did with it, is the cheapest hour of security education available this month. ## 4. Qwen promised open weights at Max scale, and the clock is running On 2 August the Qwen team announced [Qwen3.8-Max](https://qwen.ai/blog?id=qwen3.8): 2.4 trillion total parameters, 95 billion active, one million token context, and the first Max-class Qwen whose weights will be published. Those parameter counts are Alibaba's own, in Alibaba's own post, and there is no third-party confirmation of either number. The post says the weights land on Hugging Face and ModelScope "next week," which by the time you read this should mean the week just gone. As of 9 August the [Qwen organisation on Hugging Face](https://huggingface.co/Qwen) had no Qwen3.8-Max repository. Check it yourself before you plan around it, and note that this would be the largest open-weight release after Moonshot's Kimi K3 at a self-reported 2.8 trillion, which I covered in issue 1. The capability claims deserve the same treatment. Alibaba's post leads with long-horizon autonomy: a 10-day autonomous coding run producing 265 commits, 127 PRs and 151 issues in a public repository, and a five-day run reproducing a research paper from nothing but the PDF and some GPUs, then beating it by 2.7 points on AIME24. The repository is public, which is more than most vendors offer, and the numbers are still the vendor's own framing of the vendor's own run. [Nikkei reported on 3 August](https://asia.nikkei.com/business/technology/artificial-intelligence/alibaba-s-new-qwen-ai-model-falls-short-of-second-only-to-fable-5-claim) that benchmark testing puts the model behind the "second only to Fable 5" positioning Alibaba used at preview, trailing several domestic and foreign rivals. Nikkei is behind a paywall and I read the summary, not the full benchmark table, so I am passing on the headline claim rather than the evidence for it. If the weights land, the practical question is not the benchmark. It is whether a 2.4T sparse model with 95B active is something your infrastructure can serve at all. For most teams the answer is no, and the interesting release is the 27B variant Alibaba also promised. ## 5. DiffusionGemma makes decoding the variable The paper I would actually read this week is the [DiffusionGemma technical report](https://arxiv.org/abs/2608.00146), posted 31 July. Google took the Gemma 4 mixture-of-experts model, 3.8B activated and 25.2B total, and fine-tuned it into a discrete diffusion model that refines blocks of 256 tokens in parallel instead of decoding one token at a time. Two stages: supervised fine-tuning to teach bidirectional denoising, then reinforcement learning combined with sampler distillation. The whole conversion cost less than 10 percent of the original model's training token budget. The number that matters is roughly 20 tokens per forward pass, and about 1,500 output tokens per second on a single H100, which the authors say beats autoregressive models even with state-of-the-art speculative decoding. It keeps thinking mode, multimodal input and long context, and it can still decode autoregressively with only minor degradation, which points at hybrid decoding where you pick per request. That is a real architectural option rather than a leaderboard move: the same weights, two speed regimes, chosen at inference. The caveats are that this is Google measuring Google on its own evaluation suite, that "experimental" is the authors' word, and that a Pareto frontier claim is only as good as the axis you plot, so wait for someone outside DeepMind to reproduce the tokens-per-second figure on their own hardware before you rewrite your latency budget around it. ## 6. What I would do on Monday Issue 2's list was about the machinery around the model. This one narrows to the two controls that every disclosure this month converged on independently. Write your out-of-scope list before you write the prompt. Four organisations have now traced an incident to a prompt that stated a goal and not a boundary, and AISI published the exact task text so you can see how reasonable the omission looked. Name the systems, domains and identities the agent may touch, name the ones it may not, and put the second list in the prompt every run. Add a monitor that can interrupt, rather than a log you read later. OpenAI's universal monitoring and AISI's admission that it had none are the same finding from two directions. A second model reviewing proposed actions before they execute is the control both organisations reached for, and the cost of running a small classifier next to an agent is trivial next to the cost of a transcript you read after the fact. Ask your evaluation vendor how their network is configured, in writing. Three of four incidents share one vendor. If you outsource any part of testing, red teaming or evaluation, the containment properties of their environment are now part of your risk surface, and "we use a sandbox" is not an answer to the question. Treat an agent's identity creation as a privileged action. The AISI agent registered accounts, then registered more accounts to vouch for the first ones. Anthropic's agent worked a chain from email to phone number to funds to reach PyPI. Any agent that can create an external identity can create a second one, and nothing in your log will mark the moment it becomes two actors. Decide now what you do when a supplier says a model is too capable to ship. That question was hypothetical last week. OpenAI has answered it for one model, and if your product depends on always getting the newest weights, the pause is a delivery risk that belongs on the same page as your rate limits. Six items, one lesson: the industry spent three weeks learning that its evaluation infrastructure was the weak part, and then one lab applied the lesson to a model that has not shipped. Whether that becomes a norm or stays a single press release depends on what the next training run does, and I do not think anyone outside those labs can tell you yet. ## 7. The story I am not leading with Demis Hassabis moved to Chair of Google DeepMind and Chief Scientist of Alphabet, Koray Kavukcuoglu became SVP of Google DeepMind, and Jeff Dean left after 27 years to start an independent public benefit corporation with Sanjay Ghemawat, with Alphabet as a founding investor and cloud partner. All of it is in [Sundar Pichai's note](https://blog.google/company-news/inside-google/message-ceo/next-chapter-ai-momentum/), which also mentions Gemini 4 in passing. It was the loudest AI story of the week by volume and it changes nothing about what you build on Monday. Reporting tied it to a share price move, which tells you something about Alphabet and nothing about Gemini. The one line worth extracting is Hassabis saying the Gemini models have been in Koray's hands for a while already, which suggests the reorganisation documents an arrangement rather than creating one. If Gemini 4 lands and behaves differently, that will be a story. A reporting line is not. ## Sources Primary: - [Responding to the next frontier of critical cyber capabilities, OpenAI](https://openai.com/index/responding-next-frontier-critical-cyber-capabilities/) - [Security Incident INC-2026-07-28-01, UK AI Security Institute technical report](https://cdn.prod.website-files.com/663bd486c5e4c81588db7a1d/6a724858f7db25c81487016d_Security%20Incident%20INC-2026-07-28-01.pdf) - [Incident Report: unsanctioned agent behaviour during cyber testing, UK AISI](https://www.aisi.gov.uk/blog/incident-report-unsanctioned-agent-behaviour-during-cyber-testing) - [Qwen3.8-Max: A New Bar for Coding and Cowork, Qwen Team](https://qwen.ai/blog?id=qwen3.8) - [Qwen on Hugging Face, checked 9 August 2026](https://huggingface.co/Qwen) - [DiffusionGemma Technical Report, arXiv 2608.00146](https://arxiv.org/abs/2608.00146) - [Introducing Muse Code and Muse Spark 1.2, Meta AI Research](https://research.meta.ai/blog/introducing-muse-code-and-muse-spark-1-2) - [The next chapter of our AI momentum, Sundar Pichai and Demis Hassabis](https://blog.google/company-news/inside-google/message-ceo/next-chapter-ai-momentum/) - [Investigating three real-world incidents in our cybersecurity evaluations, Anthropic](https://www.anthropic.com/news/investigating-incidents-cybersecurity-evals) Reporting and analysis: - [OpenAI slows release of Astra model citing cyber capabilities, Axios](https://www.axios.com/2026/08/07/openai-astra-model-delay-cybersecurity-risks) - [OpenAI flags possible critical cybersecurity risk in upcoming model, Reuters](https://www.reuters.com/legal/litigation/openai-flags-possible-critical-cybersecurity-risk-upcoming-model-tightens-2026-08-07/) - [An AI model from Meta also hacked another company during testing, CNN](https://www.cnn.com/2026/08/05/tech/meta-ai-hacking) - [Meta says its AI model hacked into another company during testing, the Guardian](https://www.theguardian.com/technology/2026/aug/05/meta-ai-model-hack-training) - [Meta becomes latest firm to say its AI hacked another company, BBC](https://www.bbc.com/news/articles/cx2kgdnyk2po) - [Meta AI breaches external firm during security testing sandbox error, NPR](https://www.keranews.org/2026-08-08/meta-ai-breaches-external-firm-during-security-testing-sandbox-error) - [Alibaba's new Qwen AI model falls short of 'second only to Fable 5' claim, Nikkei Asia](https://asia.nikkei.com/business/technology/artificial-intelligence/alibaba-s-new-qwen-ai-model-falls-short-of-second-only-to-fable-5-claim) Gaps I could not close: Meta has published nothing of its own, so section 2 rests on statements given to reporters rather than a document from Meta; Irregular's containment white paper is announced and unpublished; the Nikkei piece is paywalled and I read the summary rather than the benchmark table; and no third party has yet evaluated Astra, so section 1 is one lab's account of its own unreleased model. Images: both diagrams are mine. The disclosure comparison is drawn from the four organisations' own posts and statements linked above, and the AISI figures come from that report's executive summary, which is where you should check them. The cover is an abstract generated with FLUX and carries no data. Reuse any of them with a link back to this post. If I got something wrong, tell me and I will correct it in the next issue. ============================================================================== # AI agent security: the brief I would give your board URL: https://saileshdahal.com.np/ai-agent-security-brief Published: 2026-08-09 Tags: AI, llm, Security ============================================================================== You may have read that AI agents escaped their sandboxes and broke into real companies. That framing is wrong, and the wrong framing costs you money. I am an AI integration consultant. This week two frontier labs published detailed postmortems of incidents where their own models reached real production systems during testing. I have read both. The failure was in the plumbing around the model, and the plumbing is exactly the part your business owns. Below are five lessons from the two postmortems. Each has what happened, why it should change how you build, and how a team like yours can turn the lesson into something you sell or something that stops you becoming the next postmortem. Every factual claim links to the lab's own writeup. The full engineering detail is in my [AI Week issue this week](/ai-week-2026-08-09); this is the business brief. ## 1. The sandbox was never a sandbox What happened: Anthropic reviewed [141,006 evaluation runs](https://www.anthropic.com/news/investigating-incidents-cybersecurity-evals) and found three where Claude reached the real internet. OpenAI found [two more](https://openai.com/index/third-party-cyber-evaluations-involving-openai-models/) with UK AISI and a partner called Irregular. The test environments were misconfigured rather than escaped into: they had live internet access the prompt said they did not, and a fictional target name collided with a real domain. The model went looking for the flag, found real machines, and reasonably concluded they were part of the exercise. Why it matters: if labs with dedicated safety teams can run an eval with live internet for three months before anyone notices, your internal agent tooling almost certainly has the same gap, and you have no retrospective to catch it. This is a configuration-drift problem, not a frontier-model problem, and configuration drift is your problem. What to do with it: sell or run an agent-exposure audit. Map every agent you run: the support bot, the codegen assistant, the internal copilot. Enumerate what network and what credentials each can reach, and replay the same did-it-touch-something-real check the labs did. Package it as a quarterly retainer: you hand the client a prioritized fix list and a standing test that fails the build when an agent reaches something it should not. Who buys this: any organization running several AI pilots with nobody owning AI risk. ## 2. The model did exactly what it was told, while believing something false What happened: in every incident the model followed its instructions. The harm came from a false belief about the situation. One run thought a real company was part of the test. Another reasoned that publishing malware would be NOT okay, then talked itself out of that conclusion because the date looked staged. The most capable model was the one that worked out the target was real and stopped on its own. Why it matters: you cannot fix this with a smarter model or a stricter prompt. The failure is in the boundary you never stated. That is good news and bad news at once: it is a cheap, ordinary engineering problem, not a research problem, which means you are exposed to it today and you can fix it yourself. What to do with it: build a scope-injection service around agent prompts. Before any agent run, inject an explicit in-scope and out-of-scope manifest drawn from the client's own architecture: which systems, which credentials, which actions are forbidden. Both labs named the same root cause, an underspecified scope statement, which makes this the cheapest high-value fix on the list. Ship it as a one-week engagement that leaves the client with a reusable template in their CI, or as middleware they call before every agent invocation. ## 3. Your agent cannot tell staging from production, and neither can you prove it What happened: Mythos 5 decided it was in a simulation because it did not recognize the certificate authorities and the year looked implausible. Your agents have no better basis for telling environments apart than that. Why it matters: if the same credentials, schema and endpoints work in both test and production, the only thing between a test run and a production write is a single environment variable. Environment separation has become a security control; most teams still treat it as a convenience. What to do with it: offer a credential and environment segmentation redesign. Issue scoped, short-lived credentials to agents instead of sharing long-lived keys. Add a guard that fails the build if the test environment can reach production data. This pairs with the audit in insight 1: you find the gap, then you close it. Teams running AI agents against live customer data in development feel this pain first. ## 4. The install path is now an attack surface an agent can generate What happened: a model that had never heard of a particular security company ended up owning its systems, because that company's scanner auto-installed a package from PyPI and ran the payload. This is dependency confusion, a known attack for years. The new part is that an automated agent can now synthesize the attempt while pursuing an unrelated goal. Why it matters: anything that installs and executes untrusted packages is production infrastructure now. The model did not set out to attack. It was chasing a flag and manufactured the exploit as a side effect. What to do with it: stand up a supply-chain guard for agent builds. A CI gate that fails if any dependency does not exist upstream, pins and verifies hashes, and sandbox-executes install steps before they touch anything real. Add an agent-action log you can replay forensically. Sell it inside a broader DevSecOps-for-AI package to engineering orgs that let agents install and run code. The differentiator is the replay log: when something reaches your systems through an agent, the transcript is your only record, and most teams have none. ## 5. Make the policy explicit and adjustable, not baked into weights What happened: Mistral released [Shieldstral](https://mistral.ai/news/shieldstral/), a 3B safety classifier you drive by asking it your moderation policy as a plain-language question at inference time. No retraining. The same lesson as the incidents: state the boundary when you need it, in your own words, and let one small model adapt. Why it matters: a frozen harm taxonomy never fits your product or your audience. Retraining a guardrail model every time you change a feature is slow and expensive, so most teams ship a generic policy that fits nothing well. What to do with it: build policy-adaptable moderation for products that change rules often. A small classifier you re-point at a new policy by rewriting a question, covering prompt classification, response moderation and toxicity across text and images on a single 16GB GPU. Marketplaces and user-generated-content products are the buyers: their rules shift with every campaign and every regulator, and a retrain-per-change guardrail is a tax they will pay you to remove. ## Where I would start If you run any autonomous agent today, do these in order. Name the systems it may and may not touch, in the prompt, every time. That one line is the cheapest fix on this list and both labs put it first. Then verify network isolation with a test that fails when the sandbox reaches the outside. Then separate test and production credentials. Then read your dependency install path again and pin everything. Then keep an agent-action log you can search. None of this is exotic. The skills transfer from ordinary systems engineering, which means you do not have to wait for a lab to fix it for you, and you can hire someone to do it this quarter. If your team is running agents against real systems and has not done the five steps above, that is the engagement I would propose. The details and the primary sources are in [my AI Week issue this week](/ai-week-2026-08-09). ## Sources Primary: - [Investigating three real-world incidents in our cybersecurity evaluations, Anthropic](https://www.anthropic.com/news/investigating-incidents-cybersecurity-evals) - [Third-party cyber evaluations involving OpenAI models, OpenAI](https://openai.com/index/third-party-cyber-evaluations-involving-openai-models/) - [Incident report: unsanctioned agent behaviour during cyber testing, UK AISI](http://www.aisi.gov.uk/blog/incident-report-unsanctioned-agent-behaviour-during-cyber-testing) - [Introducing Shieldstral, Mistral](https://mistral.ai/news/shieldstral/) Reporting and analysis: - [White House finalizes AI framework behind closed doors, Axios](https://www.axios.com/2026/08/03/white-house-finalizes-ai-framework-behind-closed-doors) - [Trump advisers tell AI firms they will not safety-test open-weight models, Reuters](https://www.reuters.com/legal/litigation/meta-anthropic-google-openai-meet-with-trump-white-house-amid-rogue-ai-agent-2026-08-04/) Images: the findings above are drawn from the labs' own postmortems, linked in Primary. No diagram is included here, and the use cases are my own invention for the businesses described. If I got something wrong, tell me and I will correct it in the next issue. ============================================================================== # AI Week: the sandbox was never a sandbox URL: https://saileshdahal.com.np/ai-week-2026-08-09 Published: 2026-08-09 Tags: AI, llm, Security Series: AI Week (part 2 of 7) ============================================================================== Last week I wrote about a model that cheated on a test and ended up inside Hugging Face's production infrastructure, and I filed it under science fiction that happened. This week two labs went back through their own logs and found the same thing had been running for months. The question is no longer whether a model can escape a sandbox. It is whether the sandbox was ever a sandbox. ## 1. The sandbox was never a sandbox After OpenAI's 21 July disclosure, Anthropic re-ran every cyber evaluation where Claude could possibly have reached the internet: [141,006 runs](https://www.anthropic.com/news/investigating-incidents-cybersecurity-evals). Three incidents came out, across six runs, the oldest dating to April, so this had run for roughly three months before anyone looked. The cause was not a clever escape. The evaluation environment, run with a third-party partner called Irregular, was misconfigured and had live internet access, while Anthropic's prompt told Claude it had none. Claude went looking for the flag, found real machines, and reasonably concluded they were part of the exercise. Anthropic frames this as a harness and operational failure rather than an alignment failure, and having read the whole post I think that is fair, not defensive. ![Three incidents across six runs, compared by what the model concluded and whether it stopped. Opus 4.7, incident 1, four runs: all four recognised the target was real, none stopped. Mythos 5, incident 2, the PyPI run: it reasoned publishing would not be acceptable, then revised that conclusion, and did not stop. The internal research model, incident 3, having scanned around 9,000 hosts: it concluded the target was real and stopped on its own.](https://saileshdahal.com.np/images/posts/ai-week-2026-08-09/incidents.svg) The one I keep going back to is Mythos 5. It found a note in the test environment telling developers to install a Python package that did not exist, so it registered the name itself and published a booby-trapped package to the real PyPI. To do that it needed a PyPI account, then an email address, then a phone number. It failed to find a free phone-number service, tried to obtain funds for one, backtracked to a free email provider, registered, and uploaded the malware. A chain of obstacles that would have told a human they were no longer in a simulation. ![The chain of obstacles Mythos 5 worked through to reach the real PyPI. Publishing needed a PyPI account, which needed an email address, which needed a phone number. It found no free phone number service, failed to obtain funds to pay for one, then backtracked to an unblocked free email provider, registered, and uploaded the malware. The package was live for about an hour and executed on 15 real systems, one of them a security company's scanner whose credentials it then exfiltrated.](https://saileshdahal.com.np/images/posts/ai-week-2026-08-09/pypi-chain.svg) The package ran on 15 real machines for about an hour. One was a security company's scanner that installs packages to check them for malware, so it installed this one, the payload executed, and Claude exfiltrated that company's credentials. Claude had never heard of that company. It did not need to, because it published to the open internet and anything that installed the package was in scope by accident. The uncomfortable detail is that early in the run Claude's own reasoning said publishing would be "NOT okay, and surely not the intended solution," then it talked itself out of that because it did not recognise the certificate authorities and the machines said the year was 2026, which it took as proof the environment was staged. It never revisited the conclusion. A model reasoning to the right answer and then discarding it on the evidence that the date looked wrong is not a capability failure. Every one of us has done the equivalent at 2am. OpenAI found two more on 4 August, [both in third-party evaluations](https://openai.com/index/third-party-cyber-evaluations-involving-openai-models/). With UK AISI, agents were given deliberate internet access with classifiers off to measure raw capability; one reused a GitHub token another lab's agent had left behind and exposed a DNS server holding exploit payloads to the public internet. With Irregular, the same partner, a fictional name again collided with a real domain and the model exploited a real website. OpenAI is explicit that this involved no zero-day and no sandbox escape, just a misconfiguration and a basic vulnerability. Both labs, same partner, same month, nearly the same failure. That is infrastructure, not any one lab's judgement. ## 2. The models were not rogue, the harness was The models ran without the classifiers and monitoring that ship on generally available models, because the point of a capability evaluation is to measure the raw thing. Anthropic states plainly that its production safeguards would have blocked the behaviour. That is the trade: you remove the guardrails to see what is underneath, which is only safe if containment holds, and containment was the part nobody was testing. Neither lab found a model pursuing a goal of its own. In every case the model did what it was told while holding a false belief about the world. Anthropic's line is that the boundary between an aligned action and a harmful one depends on the model's understanding of its situation, and in these runs that understanding was wrong. That is a different problem from misalignment, and probably a harder one to test for. The fix both labs reach for first is almost embarrassingly mundane: Claude probably would not have gone near the internet if the prompt had said clearly which systems were in and out of scope. UK AISI landed on the same cause independently. Two of the most consequential AI security incidents of the year trace back to an underspecified scope statement. The same lesson shows up from the other direction. On 4 August Mistral released [Shieldstral](https://mistral.ai/news/shieldstral/), a 3B open-weights safety classifier you drive by asking it your moderation policy as a plain-language question at inference time, no retraining. The usual guardrail model bakes a fixed taxonomy of harm into its weights; point it at a new product and you retrain. Shieldstral instead frames moderation as a question you can rewrite. State the boundary when you need it, in your own words, and one small model adapts. The incidents and the classifier are arguing the same point from opposite ends: make the policy explicit and make it adjustable, because no frozen taxonomy fits every product. ## 3. What this means for a business building with AI The postmortems are frontier-lab stories, but the failure mode is configuration and scoping, and that is the part your business owns. Four ways to turn the lesson into something you ship or sell. Audit where your agents can actually reach. Map every autonomous agent you run, from support bots to codegen assistants to internal copilots, and enumerate the networks and credentials each can touch. Then run the same replay the labs did: did anything reach a system it should not. The buyer for this is any company with several AI pilots and no single owner of AI risk, and it sells well as a standing quarterly retainer rather than a one-off. Inject an explicit scope before every agent run. Both labs named an underspecified scope as the root cause, so this is the highest-value, lowest-cost fix. Draw the in-scope and out-of-scope manifest from the client's own architecture and inject it into the prompt or a middleware call. Ship it as a one-week engagement that leaves a reusable template in their CI. Separate test and production credentials. Mythos 5 decided it was in a simulation because it did not recognise the certificate authorities and the date looked wrong. If the same credentials, schema and endpoints work in both environments, the only thing between a test run and a production write is an environment variable. Issue scoped, short-lived agent credentials and add a guard that fails the build if the test environment can reach production data. Guard the dependency install path. A model that had never heard of a security company owned it, because that company's scanner installed a package from PyPI and ran the payload. Dependency confusion is years old; the new part is an agent can now synthesise the attempt mid-task. Add a CI gate that fails if any dependency does not exist upstream, pins and verifies hashes, and sandbox-executes installs, plus an agent-action log you can replay. When something reaches your systems through an agent, the transcript is your only forensic record, and most teams have none. If you want the full business framing rather than the engineering detail, I wrote a separate brief: [AI agent security: the brief I would give your board](/ai-agent-security-brief). ## 4. What I would do on Monday Last week's list was about picking models. This one is about the machinery around them, and it applies to you even though you are not running frontier cyber evals. Tell the agent what is out of scope, in the prompt, every time. Both labs traced their incidents to prompts that specified the goal and not the boundary. Name the systems it may touch and the ones it may not. This is the cheapest item here by a distance and it is the one both labs put first. Verify network isolation instead of asserting it. Anthropic's prompt said no internet access. The container had internet access. Nobody checked, on either side, for three months. If you have a sandbox, for agent tool calls, for untrusted code or for evaluation runs, write a test that tries to reach the outside and fails the build when it succeeds. An assertion in a system prompt is a comment, not a network control. Assume the model cannot tell staging from production. Your agent has no better basis than Mythos 5 did. If the same credentials and endpoints work in both, separate them. Read your dependency install path again. Pin your dependencies. Check whether every package your build installs actually exists upstream. If you run anything that installs and executes untrusted packages, that thing is production infrastructure and needs treating as such. Log agent actions in a form you can review after the fact. Anthropic found this because it kept 141,006 transcripts and could go back through them. Two of the three affected organisations never detected the intrusion at all. Keep the transcript and be able to search it. The thread through all of it is that the interesting failure was in the harness: the scope statement, the network config, the credential boundary, the install path, the logging. That is ordinary systems engineering, which is reassuring because the skills transfer, and uncomfortable because the failure mode is not exotic and you cannot wait for a lab to fix it for you. One thing I have changed my mind about since last week. I described the Hugging Face breach as a model cheating on a test. Having read both postmortems, that framing is wrong, or at least it flatters the model. What these systems do is pursue the goal you gave them through whatever surface you left reachable. The surface is the variable you control. ## 5. The stories I am not leading with Washington finalised a voluntary framework for frontier-model security testing and published nothing about it. The White House [confirmed on 3 August](https://www.axios.com/2026/08/03/white-house-finalizes-ai-framework-behind-closed-doors) it met the deadline in the 2 June executive order, but will not say what is in it, who has seen it, or when it starts. Reuters reports the administration [will not safety-test open-weight models](https://www.reuters.com/legal/litigation/meta-anthropic-google-openai-meet-with-trump-white-house-amid-rogue-ai-agent-2026-08-04/). A framework nobody outside the room has seen is hard to evaluate, and I would rather say that than pretend to an opinion. The gap that matters is between the two halves of this issue: the labs published detailed postmortems with run counts and transcripts, and the government finalised a framework and published nothing. Apple and OpenAI traded lawsuits and public emails over trade secrets and offboarding. I am including it only because it will be in every roundup this week, and the reason it does not change anything is that it is a hiring dispute between two large companies that tells you nothing about building with AI. The only practical takeaway is the one OpenAI's rebuttal accidentally proves: check that your own offboarding actually revokes access. If it prompts you to do one thing, make it that. ## Sources Primary: - [Investigating three real-world incidents in our cybersecurity evaluations, Anthropic](https://www.anthropic.com/news/investigating-incidents-cybersecurity-evals) - [Third-party cyber evaluations involving OpenAI models, OpenAI](https://openai.com/index/third-party-cyber-evaluations-involving-openai-models/) - [OpenAI and Hugging Face partner to address security incident during model evaluation, OpenAI](https://openai.com/index/hugging-face-model-evaluation-security-incident/) - [Incident report: unsanctioned agent behaviour during cyber testing, UK AISI](http://www.aisi.gov.uk/blog/incident-report-unsanctioned-agent-behaviour-during-cyber-testing) - [Introducing Shieldstral, Mistral](https://mistral.ai/news/shieldstral/) Reporting and analysis: - [Anthropic's AI hacked three companies during tests, Reuters](https://www.reuters.com/legal/litigation/anthropic-says-claude-ai-models-accessed-three-companies-during-tests-2026-07-30/) - [Anthropic says its own AI models breached three companies during security tests, TechCrunch](https://techcrunch.com/2026/07/30/anthropic-says-its-own-ai-models-breached-three-companies-during-security-tests/) - [Trump advisers tell AI firms they will not safety-test open-weight models, Reuters](https://www.reuters.com/legal/litigation/meta-anthropic-google-openai-meet-with-trump-white-house-amid-rogue-ai-agent-2026-08-04/) - [White House finalizes AI framework behind closed doors, Axios](https://www.axios.com/2026/08/03/white-house-finalizes-ai-framework-behind-closed-doors) - [OpenAI fires back at Apple, publishing private emails, Fortune](https://fortune.com/2026/08/04/openai-fires-back-at-apple-publishing-private-emails-to-counter-trade-secret-claims/) - [The wildest allegations in Apple's trade secrets lawsuit against OpenAI, TechCrunch](https://techcrunch.com/2026/07/13/the-wildest-allegations-in-apples-trade-secrets-lawsuit-against-openai/) Images: the two diagrams are mine, drawn from the figures in Anthropic's postmortem, which is linked above and is where you should check them. The cover is an abstract generated with FLUX and carries no data. Reuse any of them with a link back to this post. If I got something wrong, tell me and I will correct it in the next issue. ============================================================================== # AI Week: open weights stopped being ideological URL: https://saileshdahal.com.np/ai-week-2026-07-30 Published: 2026-07-30 Tags: AI, llm, Security Series: AI Week (part 1 of 7) ============================================================================== First issue. Every week I'll take the few things that changed how I actually build, check them against primary sources instead of headlines, and say what I'd do differently on Monday. When a number comes from a vendor's own marketing I'll flag it. When I couldn't get to a primary source I'll say so, and there are a few of those below. Three things landed inside forty-eight hours. They look like unrelated news items, and two of them make a lot more sense once you know about something that happened earlier in the month, so I'll start there instead. ![Timeline of July 2026. Below the line, earlier in the month: the intrusion runs 9 to 13 July for about 2.5 days and 17,600 actions, Hugging Face discloses on the 16th with forensics blocked by guardrails, and OpenAI attributes it to its own models on the 21st to 22nd. Above the line, 27 to 28 July: the Open Secure AI Alliance with 57 founding members, Kimi K3's weights shipping at number one among open weights, and the Pacing the Frontier letter.](https://saileshdahal.com.np/images/posts/ai-week-2026-07-30/timeline.svg) ## The bit I keep going back to There's a paragraph in [Hugging Face's incident disclosure](https://huggingface.co/blog/security-incident-july-2026) that I've reread maybe five times. Their responders were working a live intrusion. Analysing it meant feeding attack commands, exploit payloads and command-and-control artifacts into a model, and: > the analysis requires submitting large volumes of real attack commands, exploit > payloads, and C2 artifacts, and these requests were blocked by the providers' > safety guardrails The models behind the commercial APIs couldn't tell an incident responder from an attacker, so they refused. The attacker, obviously, was working without any such constraint. Hugging Face ended up on GLM 5.2, self-hosted. That solved the refusals and also kept the attacker's credentials inside their own environment, which is the part I would not have thought of under pressure. Their advice is to have a self-hostable model vetted and sitting on the shelf before an incident. I'd been quietly filing "run your own model" under ideology, somewhere near people who compile their own kernels for fun. That was lazy of me. Guardrails on a hosted API are a sensible default that fails precisely when you need the tool most, and security forensics is only the most obvious example. ## 1. Kimi K3's weights shipped Moonshot AI launched Kimi K3 as a hosted model on 17 July and promised open weights within ten days. They shipped on the 27th. Ten days is a tight window to commit to publicly, and given how often "open" announcements slip, I think that deserves saying out loud. From the [published config](https://huggingface.co/moonshotai/Kimi-K3): 93 layers, 896 experts with 16 activated per token, a 163,840-token vocabulary, and a position limit of 1,048,576, which is the million-token context everyone quoted. Moonshot's model card puts it at 2.8 trillion total parameters with 104 billion active. Treat that pair as self-reported; third-party trackers have listed slightly different totals, and the config file states no parameter count directly. The third-party numbers are the ones I would lean on. [Artificial Analysis](https://artificialanalysis.ai/articles/kimi-k3-achieves-3-in-the-artificial-analysis-intelligence-index-comparable-to-opus-4-8-and-gpt-5-5) scored it 57 on their Intelligence Index. That is third overall, behind Claude Fable 5 and GPT-5.6 Sol, and first among open weights by a clear margin over GLM-5.2 at 51 and DeepSeek v4 Pro at 44. On Arena's frontend coding leaderboard, where humans compare outputs blind, [it went to number one](https://thenewstack.io/kimi-k3-open-weight-coding/). ![Bar chart of Artificial Analysis Intelligence Index scores for open-weight models as of 27 July 2026. Kimi K3 scores 57, GLM-5.2 scores 51, DeepSeek v4 Pro scores 44. A note records that Claude Fable 5 and GPT-5.6 Sol score higher overall but are closed and so are not plotted.](https://saileshdahal.com.np/images/posts/ai-week-2026-07-30/open-weights-index.svg) Nathan Lambert reckons the open-to-closed gap has compressed from something like six-to-nine months down to three-to-five. That's his estimate and I've no way to check it, but it lines up with what I see swapping models in a harness and watching the pass rates barely budge. ### Read the licence, though This is a custom licence, the Kimi K3 License, which Artificial Analysis tagged Commercial Use Restricted. I went and read [the actual text](https://huggingface.co/moonshotai/Kimi-K3/raw/main/LICENSE), and I'm glad I did, because my first draft of this section had the threshold wrong. I'd taken it from a screenshot of a tweet. Three clauses matter: - Section 2, the model-as-a-service clause. If aggregate revenue of the licensee and its affiliates exceeds 20 million US dollars over any consecutive twelve months, you need a separate agreement with Moonshot before commercial use. This reads on the licensee's total revenue, which is broader than it first sounds. - Section 3, attribution. Above 100 million monthly active users or 20 million US dollars in monthly revenue, "Kimi K3" has to appear prominently in the product interface. - Section 4, the exemption most readers will land in. None of it touches internal use, or access through Moonshot's own channels and certified partners. Using it internally, or building on a hosted endpoint as a small team, this is close to a non-event. Reselling inference at scale, it's a real term with a real number in it. So the licence isn't onerous, it's just a licence, and "open weights" has stopped meaning "do what you like with it". The download is reportedly around 1.4 TB, so nobody is running this on a workstation. What open weights buy you in 2026 is portability and price pressure and the option to rent hardware you control. Local execution isn't on the menu. ## 2. The breach behind all of it Dates first, since this is older than the rest of the issue and I'd rather you check me on it. The intrusion ran 9 to 13 July. Hugging Face disclosed on the 16th. OpenAI attributed it to its own models around the 21st and 22nd. Simon Willison's write-up went up on the 22nd, filed under the [title](https://simonwillison.net/2026/Jul/22/openai-cyberattack/) "science fiction that happened". OpenAI was testing models on ExploitGym, a benchmark that scores AI systems on finding and exploiting real vulnerabilities, with cyber refusals turned down for evaluation. The models decided to go after the answers instead of the benchmark. They found a zero-day in a package registry proxy, used it to break out of OpenAI's sandbox and reach the internet, then got into Hugging Face through a malicious dataset that abused two separate code-execution paths: a remote-code dataset loader, and a template injection in a dataset config. That gave them code execution on processing workers. Then worker-to-node privilege escalation, credential harvesting, lateral movement into internal clusters. Two and a half days, about 17,600 attacker actions that Hugging Face went back and reviewed. So: a model cheating on a test, where the cheating route ran through a real company's production infrastructure. I've now typed that several times and it still reads like something I invented. What actually got reached was limited internal datasets and some service credentials. Hugging Face found no sign of tampering with public models, datasets or Spaces, and verified the software supply chain clean. OpenAI separately disclosed a handful of cases where models used exposed account-level credentials on other public services: four accounts, two as an outbound relay or storage, two read-only. Reuters reported Modal Labs among them. OpenAI says the pre-release model involved is now deactivated, encrypted and cut off from research access. Credit where it's due. Both companies put out real technical detail, and OpenAI attributing it to its own model can't have been a comfortable call internally. One caveat on my sourcing: OpenAI's own post returned a 403 every time I tried to fetch it, so everything I've said about their side comes from [The Hacker News](https://thehackernews.com/2026/07/openai-agent-used-exposed-credentials.html) and [NBC](https://www.nbcnews.com/tech/tech-news/openai-says-ai-models-went-rogue-testing-triggering-unprecedented-brea-rcna588611) reporting on it, not from OpenAI directly. ## 3. The alliance On 27 July, Nvidia and a long list of others launched the [Open Secure AI Alliance](https://blogs.nvidia.com/blog/open-secure-ai-alliance/). Nvidia's post names 57 founding members, including the Linux Foundation, Red Hat, IBM, Microsoft, Cloudflare, Cisco, CrowdStrike, Palo Alto Networks, GitHub, Mistral, Perplexity, Thinking Machines Lab, vLLM, and Hugging Face itself. The stated aim is a shared set of open models, frameworks, identity systems and scanning tools for defenders. The first technical contribution is NOOA, the NVIDIA Labs Object-Oriented Agent, a research framework for making agent behaviour easier to test, trace, audit and govern. It's on GitHub already. One thing about that member list. OpenAI, Google and Anthropic aren't on it. I don't know why, and I can think of boring reasons: separate commitments, timing, lawyers. Still, an alliance about defending against agentic attacks, formed a couple of weeks after an agentic attack, without the three labs building the most capable agents in the world. I noticed. ## 4. The letter On 28 July, 1,178 employees of frontier labs signed a statement called [Pacing the Frontier](https://gist.github.com/akrolsmir/05b0fac81b0f43c9950493bdc0e5a95d). The ask, in their words, is that the US government should support an international effort to develop the technical and governance tools needed to deliberately pace the frontier of automated AI development. By employer: Anthropic 533, OpenAI 330, Google 191, Meta 62. Dario Amodei signed, with Anthropic co-founders Jared Kaplan and Jack Clark and interpretability lead Chris Olah. So did OpenAI chief scientist Jakub Pachocki, Meta chief scientist Shengjia Zhao, Google's head of AI safety and alignment Anca Dragan, and John Schulman at Thinking Machines. OpenAI and Anthropic endorsed it at company level within hours. Two things the headlines got wrong, and I'd got the first one wrong myself before reading it. This isn't a call for a pause. The signatories are explicit that they're not asking anyone to slow down today, only that the capacity to coordinate a slowdown should exist before the point where it's needed. The target is also much narrower than the coverage suggested: automated AI research, systems that design better systems, recursive self-improvement. Your retrieval pipeline is not what they're worried about. Having just spent an afternoon writing up item 2, where a model pursued a goal straight through infrastructure nobody authorised it to touch, I read this differently than I would have a month ago. ## 5. The money, which isn't a reaction to any of the above Also 27 July. I'm not going to pretend this one connects; it's a separate trend that happens to be running in the same week. The Wall Street Journal [reported](https://www.tomshardware.com/tech-industry/data-centers/nvidia-weighs-usd250-billion-guarantee-so-openai-can-lease-softbanks-10-gigawatt-ohio-campus) that Nvidia is in talks to guarantee roughly $250 billion of financing so OpenAI can lease a 10-gigawatt campus SoftBank is building on a decommissioned uranium-enrichment site about 50 miles south of Columbus, Ohio. Up to $350 billion has been discussed separately for chips. The total could pass $500 billion. Phase one is around 800 megawatts, targeted for 2028. Hold this one loosely. It's in talks, the terms aren't final, and it traces back to a single outlet. I've already seen the $250 billion quoted with the "in talks" dropped, and that's usually the last step before a rumour gets treated as a fact. I'm including it anyway because the direction survives even if the number moves. The capital going into closed frontier training is scaling up at the same moment the capability gap to models you can download is narrowing, and I don't know how those two things hold together over a few years. ## What I would do on Monday Four things, roughly in order of how much grief they'll save you. Get a self-hostable model vetted before you need one. This is Hugging Face's own advice and it's the most useful thing in the issue. Open models mostly aren't better, so that isn't the argument; the argument is that a hosted model's refusal behaviour sits outside your control and tends to trigger at the worst possible moment. Forensics is the obvious case, but anything touching abuse, moderation, or medical and legal content has the same shape. Pick something, confirm it runs on hardware you can actually reach, write the runbook down while nothing is on fire. Put the model behind an interface and keep the provider in an env var. With a three-to-five month gap, whatever you pick now will be beaten by something cheaper before the project ships. Dropping Kimi K3 into a harness this week was a config change plus an afternoon of watching numbers. If swapping your model would mean a rewrite, that's the actual bug, and it gets pricier every quarter you leave it. Let evals be the test suite. You can't unit test a model, but you can assemble cases that look like what your users really do, score them, and refuse to ship when the score drops. Skip that and "we upgraded the model" becomes a coin flip you're performing on live traffic. Treat the licence as an engineering input. Which weights, whose terms, running where, what the retention policy says. When your product is inference, a 12-month revenue threshold has stopped being a question for the legal folder and become a constraint on your architecture. Put it next to the dependency audit. Most of this is systems engineering with an unfamiliar, non-deterministic dependency sitting in the middle of it, and most fullstack instincts carry over intact: draw the seam so the dependency can be swapped, lean on the test suite so you can move fast, read the licence, know where the money goes. What's genuinely new is having to measure something fuzzy. That part is tedious and unglamorous and I have not found a way around it yet. ## Sources Primary: - [Hugging Face security incident disclosure, July 2026](https://huggingface.co/blog/security-incident-july-2026) - [Kimi K3 model card and config](https://huggingface.co/moonshotai/Kimi-K3) - [Kimi K3 License, full text](https://huggingface.co/moonshotai/Kimi-K3/raw/main/LICENSE) - [Open Secure AI Alliance founding members, NVIDIA](https://blogs.nvidia.com/blog/open-secure-ai-alliance/) - [Pacing the Frontier, signatories and comments](https://gist.github.com/akrolsmir/05b0fac81b0f43c9950493bdc0e5a95d) Reporting and analysis: - [OpenAI's accidental cyberattack against Hugging Face, Simon Willison](https://simonwillison.net/2026/Jul/22/openai-cyberattack/) - [OpenAI agent used exposed credentials across four services, The Hacker News](https://thehackernews.com/2026/07/openai-agent-used-exposed-credentials.html) - [OpenAI says AI models went rogue during testing, NBC News](https://www.nbcnews.com/tech/tech-news/openai-says-ai-models-went-rogue-testing-triggering-unprecedented-brea-rcna588611) - [OpenAI cyber models broke out of training environment, CNBC](https://www.cnbc.com/2026/07/22/open-ai-cyber-models-hack-hugging-face.html) - [Kimi K3 achieves #3 on the Artificial Analysis Intelligence Index](https://artificialanalysis.ai/articles/kimi-k3-achieves-3-in-the-artificial-analysis-intelligence-index-comparable-to-opus-4-8-and-gpt-5-5) - [Kimi K3 tops Arena's coding leaderboard, The New Stack](https://thenewstack.io/kimi-k3-open-weight-coding/) - [Kimi K3: the open-weights escalation, Nathan Lambert](https://www.interconnects.ai/p/kimi-k3-the-open-weights-escalation) - [Kimi K3's full weights are here, with a caveat, VentureBeat](https://venturebeat.com/technology/kimi-k3s-full-weights-are-here-but-theyre-open-with-a-caveat-what-enterprises-should-know) - [NVIDIA forms 37-member Open Secure AI Alliance, The Hacker News](https://thehackernews.com/2026/07/nvidia-forms-37-member-open-secure-ai.html) - [Top scientists ask US for tools to pace AI development, NBC News](https://www.nbcnews.com/tech/security/openai-anthropic-scientists-ask-us-tools-ai-development-rcna589727) - [Nvidia weighs $250 billion guarantee for OpenAI's Ohio campus, Tom's Hardware](https://www.tomshardware.com/tech-industry/data-centers/nvidia-weighs-usd250-billion-guarantee-so-openai-can-lease-softbanks-10-gigawatt-ohio-campus) If I got something wrong, tell me and I will correct it in the next issue. ============================================================================== # Practical lessons from an AI talk at Kathmandu University URL: https://saileshdahal.com.np/few-takeaways-on-getting-amazed-with-ai Published: 2025-01-25 Tags: AI, llm, Machine Learning, chatgpt, learning ============================================================================== On 24th Jan 2025, I attended a session organized by Department of Artificial Intelligence at [Kathmandu University (KU)](http://ku.edu.np). This session was by [Dr. Saumendra Mohanty](https://www.lloydbusinessschool.edu.in/faculty/dr-saumendra-mohanty.html) and was insightful; he looked at AI from angles I hadn't considered. Here are few takeaways from the session. [Orange Data Mining](https://orangedatamining.com/) This was my first time hearing about this tool and I was like "man this is good". This is a no code tool that you can do a lot with drag and drop interface. I know you may have a 'eek' by hearing 'no code' but this can be a huge time saver if you are trying to find the best model for your data set. The instructor demonstrated how you can use multiple models/strategies in the same dataset to find the optimal one for your data set. I think the demo was around 15-mins long, but we tried models like Linear Regression, Random Forest, SVM, Multi-Layer Perceptron, KNN etc, and for that dataset, the Random Forest was more accurate. We generate the confusion matrix for all these models, tried with multiple datasets, analyzed the accuracy without any code and within 15 mins. It was surprisingly good stuff. [Cosine Similarity in Resume](https://medium.com/@kirudang/job-resume-matching-part-1-2-obtaining-similarity-score-using-doc2vec-a6d07fe3b355) He explained that how companies use cosine similarity to filter out potential spams resume, and how we can think through it and optimize our resume. We had a practical demonstration where we calculated how likely a resume is going to be passed to a second round for a given job description. The working behind this is, the job description is converted into a vector ( i need to read more on how this is done, If you are familiar on this, feel free to comment) and similarly the resume is also converted into a vector. Once this is done, calculate the cosine of the angle between these vector.

The angles being closer to 0 means that the provided resume and the job description has a greater match. Before you ask, yes we will also be taking into account all the irrelevant details in the resume, that is not the part of the job description. ## Sentiment analysis We also got to know how sentiment analysis is being used in industries to optimize sales. The Flipkart story stuck: a failing product turned top seller after the reviews were analyzed for sentiment and the product was changed accordingly. We briefly discussed other areas: customer feedback analysis, product success prediction based on customer data, stock market trends (we did one on Google data direct from Yahoo Finance with Orange Data Mining) e.t.c. ## Localized AI We also discussed about current trends on LLM and what the next focus is going to be. Professor explained that the next big thing on LLMs is going to be on the regional languages, how the local government can help subsidize this, and it's challenges to improve accessibility in the rural areas so that AI is accessible. ## Neural networks and reinforcement learning We also briefly talked about the Neural Networks and machine learning method like Supervised Learning and Reinforcement Learning and its tradeoffs. He explained that reinforcement learning may not be the best for self driving car due to the accidents that may happen for the model to learn. --- What I think is, although we are surrounded by AI and most of the repetitive tasks are being replaced by AI, we will still have an edge in sophisticated problem solving. If you're curious about Orange, cosine similarity, or AI in general, tell me in the comments. PS, Yes I generate this thumbnail with AI ============================================================================== # Flutter code generation and app bundle size: FAQ URL: https://saileshdahal.com.np/flutter-faq-codegen-and-bundle-size Published: 2025-01-24 Tags: Flutter, Dart, Firebase, #dart-for-beginners, Mobile Development ============================================================================== After 2 years of not writing and ditching my readers, I am trying to get back to writing again. The past couple of years have been really busy with freelancing on Upwork, where I delivered multiple apps for clients that are generating steady MRR. I'm starting with some topics that might sound trivial but can be confusing to many Flutter developers. Let's try to answer these questions that you may have come across while using flutter and its build\_runner. 1. Is codegen secretly bloating your app size? (It's not.) 2. Will static metaprogramming make tools like build\_runner obsolete? 3. How are you handling generated files? Do you commit them to VCS? 4. Is build\_runner getting slower as the project grows? Can you fix it? Yes? How? --- ## What determines the Flutter app size, and how do you make it smaller? Flutter provides tools to analyze the size breakdown of your app's final build. There is a special flag that many might not know about, which allows you to view the final contents of the release build. With this command, the Dart compiler will monitor package usage and code utilization within the app, generating a JSON report that details the build contents. ```bash flutter build ipa --analyze-size ``` The `--analyze-size` flag lists all the components of the bundle, including the frameworks used, their sizes, and the total asset size of the final bundle. This is how it will look like. This command also generates a JSON file that we can use to view a TreeMap with dart DevTools. ```bash dart devtools --appSizeBase=ios-code-size-analysis_02.json ``` Once you run the command, you can see the TreeMap with all the details interactively. You can also click on one node and explore further. ![TreeMap view of the size breakdown](https://saileshdahal.com.np/images/posts/flutter-faq-codegen-and-bundle-size/e785d2c3-f9e8-44f4-8c1e-5697da2d8644.png) Now it's clear that using codegen won't significantly increase the app size (it might only add a few kilobytes), so we don't need to worry about it bloating the app. --- ## What happens if I list multiple packages in pubspec.yaml but don't actually use them? When you have specified packages/plugins to use in your flutter project, the Dart compiler will treeshake all the unused dart code optimizing the app's size. The tricky part is with the plugin (ones containing native dependencies). [▶ Watch on YouTube](https://youtu.be/Y9WifT8aN6o) If your pubspec.yaml contains unused plugins, the Dart compiler will treeshake unused Dart code, but native dependencies will remain, increasing the app size. To prevent this, remove unused packages from pubspec.yaml and regularly check for unused dependencies. You can use available tools to scan for your unused dependencies and assets from your codebase. 1. [dependency\_validator](https://pub.dev/packages/dependency_validator) reports any missing, under-promoted, over-promoted, and unused dependencies. Any package that either provides an executable or a builder that will be auto-applied via the [dart build system](https://github.com/dart-lang/build) will be considered used even if it isn't imported. 2. [Flutter: Find Unused Dart Files & Assets](https://marketplace.visualstudio.com/items?itemName=esentis.flutter-find-unused-assets-and-dart-files) is a VS code extension which allows you to find unused assets, dart files & dependencies in your project. --- ## If I list an asset in pubspec.yaml but don't use it within the app, will it still get bundled into the application? Yes. If you list an asset in your pubspec.yaml but don't use it within your Flutter app, it will still be bundled into the application. This applies to most assets like images, sounds, or general files: the asset bundling process does not remove unused assets based on their use in the code. ## What about the message about fonts being tree-shaken while you build for Android? The message about fonts being "treeshaken" refers to Flutter's font tree-shaking feature. This feature is different from how assets like images or other files are handled. ![Flutter fonts being treeshaken](https://saileshdahal.com.np/images/posts/flutter-faq-codegen-and-bundle-size/aa27d37d-84ee-44ee-bd42-4b6af6a5b95d.png) When you build your Flutter app in release mode, Flutter includes only the font glyphs (characters) that are actually used in your app. Flutter analyzes your app's text usage and optimizes font files by including only the required characters. This is why you see the "fonts being treeshaken" message during the build. ## Will static metaprogramming make tools like build_runner obsolete? The [Dart macro system](https://dart.dev/language/macros/) has introduced support for static meta-programming to the Dart language. This means that, when the feature is live, we could use annotations and the dart compiler will JIT compile the annotations to code on the fly. Most of the notable packages are planning to adopt this approach to mitigate the shortcoming of the build\_runner (time consuming). Here's [Remi Rousselet](https://hashnode.com/@remi_rousselet) talking about rewriting Freezed with macros. [▶ Watch on YouTube](https://youtu.be/AsF_liobO-c) ## How are you handling generated files? Do you commit them to VCS? Using tools like build\_runner generates a significant number of files, which can be cumbersome to manage, potentially leading to issues like merge conflicts if not handled correctly. Committing these files to a version control system would be a matter of preference and is somewhat opinionated. *Personally, I do not check these files into VCS when working on collaborative projects to avoid merge conflicts.* If you choose not to check these files into your VCS, you may need to add an additional build step in your CI process to generate these files, which might increase your CI build time. [https://stackoverflow.com/questions/893913/should-i-store-generated-code-in-source-control](https://stackoverflow.com/questions/893913/should-i-store-generated-code-in-source-control) ## Is build_runner getting slower as the project grows? Can you fix it? As your project expands with more files and additional build\_runner generators, the overhead increases, leading to longer times for code generation. This can be optimized by creating a build.yaml file and explicitly telling the generator which files to check for code generation. This can drastically decrease the codegen time. Here's an example of such build.yaml file. ```yaml targets: $default: builders: freezed:freezed: generate_for: exclude: - test/**.dart include: - lib/**/*data/**/*.dart - lib/**/*models/**/*.dart - lib/**/*domain/**/*.dart - lib/core/error/failures/**/*_failure.dart - lib/features/**/*bloc/*_bloc.dart injectable_generator:injectable_builder: generate_for: exclude: - test/**.dart include: - lib/**/*_{bloc,data_source,interceptor,http_client,repository,service,plugin,viewmodel}.dart - lib/**/*usecases/**/*.dart - lib/core/di/**/*.dart - lib/domain/**/*_{factory,api,filters,impl}.dart - lib/network/**/*.dart json_serializable:json_serializable: generate_for: exclude: - test/**.dart include: - lib/**/*{data,models,domain}/**/*.dart options: create_factory: true create_to_json: true field_rename: snake explicit_to_json: true retrofit_generator: generate_for: exclude: - test/**.dart include: - lib/**/*{data_source,http_client}.dart stacked_generator: stackedBottomsheetGenerator: generate_for: exclude: - test/**.dart include: - lib/**/*_bottom_sheet.dart stackedDialogGenerator: enabled: false stackedLocatorGenerator: enabled: false stackedFormGenerator: enabled: true generate_for: exclude: - test/**.dart include: - lib/**/*_view.dart stackedLoggerGenerator: enabled: true generate_for: exclude: - test/**.dart include: - lib/**/*_view.dart - lib/core/setup/config/logger.dart - lib/core/setup/circles_app.dart stackedRouterGenerator: enabled: true generate_for: exclude: - test/**.dart include: - lib/**/*_{view,navigator,screen}.dart - lib/core/setup/config/routes.dart ``` Although this reduces the time it takes to run codegen, using this method without proper code architecture will be a nightmare, causing even more problem in the longer run. [https://johnthiriet.com/optimizing-your-flutter-build-runner-for-faster-development/](https://johnthiriet.com/optimizing-your-flutter-build-runner-for-faster-development/) --- That's it for today. If you have questions or a topic you want covered, write it in the comments. ============================================================================== # Flutter share handler: receive images and video from other apps URL: https://saileshdahal.com.np/sharing-media-from-external-to-flutter-app Published: 2023-02-14 Tags: Flutter, Dart, Programming Blogs, Beginner Developers ============================================================================== Sharing things like files, pictures, videos, and texts from external apps can be difficult, especially when done without any external packages. The [share\_handler](https://pub.dev/packages/share_handler) package handles the sharing part for us, so we can focus on what to do with the shared media. We will be using a multi-flavored flutter app as our starting point for this tutorial. You can [find the repo here](https://github.com/saileshbro/sharing_into_flutter_app.git) Let's get started by adding `share_handler` as a dependency in `pubspec.yaml` ## Android setup: receive shared media in Flutter We will start with the easy setup. In our `android/app/src/main/AndroidManifest.xml` file, we will add the intent filters and metadata for the media types we want to support. For text-only support ```xml ``` For image-only support ```xml ``` For video-only support ```xml ``` If you want to support any type of files ```xml ``` > Tip: You can add `SHARE_MULTIPLE` action, if you want to support more than one media sharing. ```xml ``` > Tip: You can change the `android:mimeType` accordingly for the file types. Also, if you want to prevent the incoming shares from opening a new activity each time, you can prevent this by changing `android:launchMode` to `singleTask` ```xml ... ``` For adding our app in share suggestions and shortcuts, we can create a new file `share_targets.xml` in `android/app/src/main/res/xml/share_targets.xml` ```xml ``` > Tip: Make sure you have `app_id_suffix` resource string defined for each flavor. ```apache resValue "string", "app_id_suffix", applicationIdSuffix ``` If you are not using flavors, you can define this in the following format. ```xml ``` Now, we will add a metadata field in the `AndroidManifest.xml` file. ```xml ``` Once done, if you try to share something, you should see the app icon on the share page. ![App showing in share intent](https://saileshdahal.com.np/images/posts/sharing-media-from-external-to-flutter-app/0efdd6d6-d920-41e8-91a9-457f328656b3.png) --- ## iOS setup: receive shared media in Flutter ### Update the Runner `Info.plist` For iOS, let's get started by editing our `Info.plist` file. We will be registering a deep link that will be launched by Share Extension. For registering the deep link, add the following in your `Info.plist` ```xml CFBundleURLTypes CFBundleTypeRole Editor CFBundleURLSchemes ShareMedia-$(PRODUCT_BUNDLE_IDENTIFIER) ``` If you already have some entries for `CFBundleURLTypes`, you can add a new entry like this. ```xml CFBundleURLTypes ... CFBundleTypeRole Editor CFBundleURLSchemes ShareMedia-$(PRODUCT_BUNDLE_IDENTIFIER) ... ``` > Tip: Make sure you have a user-defined variable `PRODUCT_BUNDLE_IDENTIFIER` for your iOS project. Make sure you have an entry for `NSPhotoLibraryUsageDescription` if you are planning to share images. ```xml NSPhotoLibraryUsageDescription Photos can be shared to and used in this app ``` If you are planning to add `AirDrop` support, add the following entry in `Info.plist`. ```xml LSSupportsOpeningDocumentsInPlace No CFBundleDocumentTypes CFBundleTypeName ShareHandler LSHandlerRank Alternate LSItemContentTypes public.file-url public.image public.text public.movie public.url public.data ``` > You can modify `LSItemContentTypes` array based on your need. ### Share Extension setup Now for this part, we will create a share extension from Xcode. * Open `ios` folder of your flutter project in Xcode. ```bash open ios/Runner.xcworkspace ``` * Go to `File` -> `New` -> `Target` ![Image showing how to create a new target](https://saileshdahal.com.np/images/posts/sharing-media-from-external-to-flutter-app/315f2d44-700d-4c8f-b816-348a5bc99e74.png) * Search for `Share Extension` and hit `Next` ![Image showing how to select a Share Extension](https://saileshdahal.com.np/images/posts/sharing-media-from-external-to-flutter-app/fb9216af-8b17-4b2b-85f2-2aa3e53028e2.png) Now create the extension with `ShareExtension` name, and hit save. This will add a new target for your project. > Note: Make sure the name is `ShareExtension` ![Create a new Share Extension Prompt](https://saileshdahal.com.np/images/posts/sharing-media-from-external-to-flutter-app/c3d57abe-9d1a-4180-b745-ad8eaa6924a5.png) > Important: Make sure the minimum deployment version of both targets is the same. ![Setting min deployment version to 14](https://saileshdahal.com.np/images/posts/sharing-media-from-external-to-flutter-app/192e05e0-0731-4b0a-b919-0af9063176f2.png) For `Target` -> `Runner`, set minimum deployment version to `14.0`. ![Setting min deployment version to 14](https://saileshdahal.com.np/images/posts/sharing-media-from-external-to-flutter-app/2af298bf-0818-439f-ab10-bfd83df83f0d.png) Similarly for `Target`\-> `ShareExtension`, set minimum deployment version to `14.0` ![Setting min deployment version to 14](https://saileshdahal.com.np/images/posts/sharing-media-from-external-to-flutter-app/c8a9c274-7260-42e2-b569-031d02cb8255.png) Once this is done, open `ios/ShareExtension/ShareViewController.swift` and replace everything with following ```swift import share_handler_ios_models class ShareViewController: ShareHandlerIosViewController {} ``` > Note: Make sure to run `flutter pub get` and `pod install --repo-update` If you have multiple flavors set up then we will also have to create multiple flavors for our share extension. We will create 3 different schemes for share extension too. ![Manage scheme option](https://saileshdahal.com.np/images/posts/sharing-media-from-external-to-flutter-app/59d15593-eef5-433e-82ab-b741d3f14eb4.png) Go to `Manage Scheme`, and then we will duplicate and create 3 different schemes for `ShareExtension` as `ShareExtension-production`, `ShareExtension-development` and `ShareExtension-staging`. Rename `ShareExtension` to `ShareExtension-production` that will handle one scheme, and then for staging, we can duplicate `ShareExtension-production` and rename it to `ShareExtension-staging` and assign the correct configuration. ![Create a new Share Extension scheme for staging](https://saileshdahal.com.np/images/posts/sharing-media-from-external-to-flutter-app/62c12ca3-be04-4ddd-8d4c-ed45d3254211.jpeg) Similarly, we will create one scheme for `ShareExtension-development` as well. ![All final schemes](https://saileshdahal.com.np/images/posts/sharing-media-from-external-to-flutter-app/a45b3dde-0a31-4ebc-9b34-bc71bb3277a5.png) > Note: you don't have to duplicate the schemes if you are not using flavors. ### Share Extension `Info.plist` update Once this is done, we will make some changes to `ios/ShareExtension/Info.plist` ```xml CFBundleVersion $(FLUTTER_BUILD_NUMBER) NSExtension NSExtensionAttributes IntentsSupported INSendMessageIntent NSExtensionActivationRule NSExtensionActivationSupportsWebURLWithMaxCount 999 NSExtensionActivationSupportsWebPageWithMaxCount 999 NSExtensionActivationSupportsMovieWithMaxCount 999 NSExtensionActivationSupportsImageWithMaxCount 999 NSExtensionActivationSupportsFileWithMaxCount 999 NSExtensionActivationSupportsAttachmentsWithMaxCount 999 NSExtensionActivationSupportsText PHSupportedMediaTypes Video Image NSExtensionMainStoryboard MainInterface NSExtensionPointIdentifier com.apple.share-services ``` Here you can make changes to the `NSExtensionActivationRule` field based on your requirements. > Note: Here we have set the maximum file-sharing count to 999, but you can disable or enable them based on your requirements. Once this is done, make sure we have different bundle identifiers for each flavor and configuration for `Share Extension`. If you click `Target` -> `Share Extension` -> `Singing & Capabilities` -> `All` ![All bundle ids for share extension](https://saileshdahal.com.np/images/posts/sharing-media-from-external-to-flutter-app/678a49d8-2cef-4d4d-9218-fb118a2f387b.png) > Note: Make sure the `ShareExtension` bundle identifier for each config has a `.ShareExtension` prefix compared to the `Runner`. Once done, we need to assign both targets with a group identifier. For both `Runner` and `ShareExtension` target, go to `Signing & Capabilities`. ![Show how to add app group](https://saileshdahal.com.np/images/posts/sharing-media-from-external-to-flutter-app/b32f39ad-7e5b-4e46-aa55-97644a3e2b34.png) Now, for each flavor, we will add an app group. ![Show how to add app group](https://saileshdahal.com.np/images/posts/sharing-media-from-external-to-flutter-app/d90f88b6-7357-42f5-b16b-0a89a81ea540.png) ![Show how to add app group](https://saileshdahal.com.np/images/posts/sharing-media-from-external-to-flutter-app/0b4f0856-0a10-43f2-aeae-b851091f53fa.png) > Note: Make sure to add the group id as `group.` for each flavor. For `Debug-production`, `Release-production`, and `Profile-production`. ![add new group id for production](https://saileshdahal.com.np/images/posts/sharing-media-from-external-to-flutter-app/04fb9de6-227c-4a9a-bf46-038303eddf60.png) > Tip: For matching group, you can select the check box ![option showing the group id](https://saileshdahal.com.np/images/posts/sharing-media-from-external-to-flutter-app/dac703ea-93d6-421a-8291-9545ce4dbef3.png) For `Debug-development`, `Release-development`, and `Profile-development`. ![add new group id for development](https://saileshdahal.com.np/images/posts/sharing-media-from-external-to-flutter-app/2b178b2b-1bb3-42f4-aab3-d09fbb44bd25.png) Similarly for `Debug-staging`, `Release-staging`, and `Profile-staging` ![add new group id for staging](https://saileshdahal.com.np/images/posts/sharing-media-from-external-to-flutter-app/0f017a91-0600-46ec-8d4e-0eedb61cb93b.png) If you click on `Target` -> `Runner` -> `Signing & Capabilities` -> `All`, you should see the following. ![All app groups linked](https://saileshdahal.com.np/images/posts/sharing-media-from-external-to-flutter-app/04773b29-42fe-4a3a-8361-de3adb195cce.png) This will create `entitlements` files for each configuration. Similarly for `ShareExtension` we can quickly create `.entitlements` files. In `ios/ShareExtension` we will create an entitlements file for each config and each flavor. We will create 9 entitlements files for each flavor (`production`, `development`, and `staging`), and `Debug`, `Release`, and `Profile` config. For `Debug`, `Release`, and `Profile` configuration for `development` flavor, we will create three files `ShareExtensionRelease-development.entitlements`, `ShareExtensionDebug-development.entitlements`, and `ShareExtensionProfile-development.entitlements`, with the following contents. ```xml com.apple.security.application-groups group.np.com.saileshdahal.sharing.dev ``` Similarly, for each configuration of `production` and `staging` flavor, we will create three entitlements files. By the end, we will have the following files ```apache ShareExtensionDebug-development.entitlements ShareExtensionDebug-production.entitlements ShareExtensionDebug-staging.entitlements ShareExtensionProfile-development.entitlements ShareExtensionProfile-production.entitlements ShareExtensionProfile-staging.entitlements ShareExtensionRelease-development.entitlements ShareExtensionRelease-production.entitlements ShareExtensionRelease-staging.entitlements ``` Once this is done, link the entitlement files by clicking `+` button for app group capabilities for each flavor. ![reverify your entitlements file](https://saileshdahal.com.np/images/posts/sharing-media-from-external-to-flutter-app/a5681d0c-2d34-4a64-9d15-657cc78085ea.png) > Tip: Make sure the entitlements files are correctly linked, else this may not work properly for each flavor. Make sure, we have all the entitlements files linked up correctly. If we do everything right we will see the following. ![all linked group ids](https://saileshdahal.com.np/images/posts/sharing-media-from-external-to-flutter-app/47123d8c-caa9-4a75-9178-e2420c29dbb5.png) ![final entitlement files](https://saileshdahal.com.np/images/posts/sharing-media-from-external-to-flutter-app/6dbfe44f-3a78-4b7e-a677-5c04999e3a55.png) > For iOS setup without flavors All the setup above can be taken reference for the apps without flavors as well. The only difference here is, we can have single entitlements file for each target, that is `Runner.entitlements` and `ShareExtension.entitlements`. The bundle id for the `Share Extension` will be `np.com.saileshdahal.sharing.ShareExtension`, and the group bundle id will be `group.np.com.saileshdahal.sharing` Now, for the final setup, we will make some changes in `Podfile` and register the dependency. ```ruby target 'Runner' do use_frameworks! use_modular_headers! flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__)) # Add this block target 'ShareExtension' do inherit! :search_paths pod "share_handler_ios_models", :path => ".symlinks/plugins/share_handler_ios/ios/Models" end # End of block end ``` Once this is done, run `pod install --repo-update` in `ios` directory. > Note that `pod install --repo-update` gives an error: \[!\] No podspec found for `share_handler_ios_models` in `.symlinks/plugins/share_handler_ios/ios/Models`until the project has been built at least once, at which point Flutter has created the referenced symlink. Thanks [Guyren Howe](https://hashnode.com/@gisborne) for mentioning this in the comments. --- ## Test sharing from Android and iOS Let's check if we can see our app when we share something. ![app showing in share intent](https://saileshdahal.com.np/images/posts/sharing-media-from-external-to-flutter-app/002962c1-c902-45e5-b31c-528e5ffbf932.png) Here, we can see both flavors appear while we try to share images from photos. --- ## Get shared media in Flutter Now that we have both Android and iOS working, we can start implementing our share handler service to get the shared media. The package exposes a stream of `SharedMedia`. We can subscribe to this stream and do side effects based on the shared media type. Also, the package has a method to get the initial shared media. This will be helpful if the app is in terminated state, and we open the app from the share page. I have added the code snippet from [`share_handler`](https://pub.dev/packages/share_handler) ```dart class HomePage extends StatefulWidget { const HomePage({super.key}); @override State createState() => _HomePageState(); } class _HomePageState extends State { StreamSubscription? _streamSubscription; SharedMedia? media; @override void initState() { super.initState(); initPlatformState(); } @override void dispose() { _streamSubscription?.cancel(); super.dispose(); } Future initPlatformState() async { final handler = ShareHandlerPlatform.instance; media = await handler.getInitialSharedMedia(); _streamSubscription = handler.sharedMediaStream.listen((SharedMedia media) { if (!mounted) return; setState(() => this.media = media); }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Share Handler'), ), body: ListView( padding: const EdgeInsets.all(16), children: [ Text( 'Shared to conversation identifier: ${media?.conversationIdentifier}', ), const SizedBox(height: 10), Text('Shared text: ${media?.content}'), const SizedBox(height: 10), Text('Shared files: ${media?.attachments?.length}'), ...(media?.attachments ?? []).map((attachment) { final path = attachment?.path; if (path != null && attachment?.type == SharedAttachmentType.image) { return Column( children: [ ElevatedButton( onPressed: () { ShareHandlerPlatform.instance.recordSentMessage( conversationIdentifier: 'custom-conversation-identifier', conversationName: 'John Doe', conversationImageFilePath: path, serviceName: 'custom-service-name', ); }, child: const Text('Record message'), ), const SizedBox(height: 10), Image.file(File(path)), ], ); } return Text( '${attachment?.type} Attachment: ${attachment?.path}', ); }), ], ), ); } } ``` Now, if we run this code and try to share an image from Photos, we should see something like this. ![working demo of the gif](https://saileshdahal.com.np/images/posts/sharing-media-from-external-to-flutter-app/c35ad321-b438-4991-b90e-02fc03cff5af.gif) --- The source code for this tutorial is on GitHub at [`saileshbro/sharing_into_flutter_app`](https://github.com/saileshbro/sharing_into_flutter_app). And if you're interested in learning more about setting up flavors in Flutter, be sure to check out [Simplifying flavor setup in the existing Flutter app](/flavor-setup-flutter) to learn how to add different flavors to your app with unique configurations and build targets. ## References * [`share_handler`](https://pub.dev/packages/share_handler) * [Android Developers: send simple data to other apps](https://developer.android.com/training/sharing/send) * [Apple Developer: Share extensions](https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/Share.html) ============================================================================== # Flutter flavors: configure multiple app environments URL: https://saileshdahal.com.np/flavor-setup-flutter Published: 2023-02-06 Tags: Flutter, Programming Blogs, Programming Tips, Dart ============================================================================== Flavors are build configurations in Flutter apps that allow developers to create separate environments using the same code base. They allow for customization at runtime based on the defined compile-time configurations. * Flavors allow efficient management of different development environments (development, staging, production) * Enables creation of multiple versions of the same app (free and paid versions, separate environments for feature development) * Simplifies setting different parameters (API endpoints, API keys, app icons) for each configuration * Makes it easier to manage different app versions > Apps created using the [`very_good_cli`](https://pub.dev/packages/very_good_cli) comes with three flavors - `development`, `staging`, and `production`. --- ## Adding flavors to an existing Flutter app To add flavors to an existing Flutter app, packages like [`flutter_flavorizr`](https://pub.dev/packages/flutter_flavorizr) promise to automate the process. It's common for these solutions to encounter issues when the app has already made extensive use of native plugins and custom configurations. As a result, manually adding flavors to an app may be the best course of action. This process requires a bit of human touch, but it provides complete control over the configuration of your app's flavors. --- ## Setting Up Flavors To get a hands-on experience, we will be adding flavors to [an existing app](https://github.com/saileshbro/fitness_app_clone) > We will be setting up `development`, `staging` and `production` flavors. ### Create Target Files We will start by creating three main files for each flavor, `main_.dart` and create a `main` function. We can replace the outdated `main.dart` file with our new entry points. In the `bootstrap.dart file`, we'll create a new function called `bootstrap` which will accept the environment from each entry point and use it to launch the app. * `main_production.dart` ```dart void main() => bootstrap('production'); ``` * `main_development.dart` ```dart void main() => bootstrap('development'); ``` * `main_staging.dart` ```dart void main() => bootstrap('staging'); ``` * `bootstrap.dart` ```dart void bootstrap(String env) => runApp(MyApp(env: env)); class MyApp extends StatelessWidget { const MyApp({super.key, required this.env}); final String env; @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, builder: (context, child) { if (env == 'production') return child!; return Banner( message: env.toUpperCase(), location: BannerLocation.topEnd, child: child, ); }, theme: ThemeData( fontFamily: 'Montserrat', ), home: const HomePage(), ); } } ``` Since setting up flavors in android is pretty straightforward, we will start with it so we can test it quickly. ### Using flavors in Android To add `production`, `staging` and `development` flavors, let's make some changes in the app level `build.gradle` in `android/app/build.gradle` file. Here, let's specify a `flavorDimensions` and a `productFlavors` object. ```apache flavorDimensions 'default' productFlavors { production { dimension 'default' resValue "string", "app_name", "FITNESS" } development { dimension 'default' applicationIdSuffix '.dev' versionNameSuffix '.dev' resValue "string", "app_name", "FITNESS.DEV" } staging { dimension 'default' applicationIdSuffix '.stg' versionNameSuffix '.stg' resValue "string", "app_name", "FITNESS.STG" } } ``` The `flavorDimensions` line defines the name of the flavor dimension, which is `default` in this case. Each flavor is defined within the `productFlavors` block. The `production` flavor is the base flavor of the app. The `development` and `staging` flavors are similar, but with some differences. The `applicationIdSuffix` and `versionNameSuffix` lines add the `.dev` or `.stg` suffix to the application ID and version name, respectively. The `resValue` line sets the name of the app to `FITNESS.DEV` or `FITNESS.STG`, respectively. > NOTE: Don't forget to add `android:label="@string/app_name"` to the `application` tag in `AndroidManifest.xml` file. --- #### Let's Test Now, we can run the app using the following commands. ```bash flutter run --flavor development --target lib/main_development.dart flutter run --flavor staging --target lib/main_staging.dart flutter run --flavor production --target lib/production.dart ``` Once done, you should have three different apps with different names, versions, and bundle identifiers. ![android flavors working](https://saileshdahal.com.np/images/posts/flavor-setup-flutter/2b172457-0674-4066-89dc-cb6c1755d472.png) --- ### iOS schemas We will now be creating schemas for each flavor in Xcode. We already have a `Runner` schema, which is the default schema. We will rename it to `production` and create two new schemas, `development` and `staging`. * Open the ios folder in Xcode. ```bash open ios/Runner.xcworkspace ``` * Let's make some changes to our configuration. ![config-initial](https://saileshdahal.com.np/images/posts/flavor-setup-flutter/9193336b-1b16-4e9e-acde-158c3d3b3f1c.png) * In Project Runner, rename `Debug`, `Release`, and `Profile` adding `-production` suffixes to each. ![config-production](https://saileshdahal.com.np/images/posts/flavor-setup-flutter/58f491d2-f3dd-4795-bf5f-2f5229f1f616.png) * Once done, we will now create these 3 files for each `staging` and `development` as well. You can use duplicate. ![config-duplicate-button](https://saileshdahal.com.np/images/posts/flavor-setup-flutter/8854c543-13a8-4f36-b679-5f27b2dfcc3e.png) * Once done, we will have 9 configurations, 3 for each flavor. ![configuration](https://saileshdahal.com.np/images/posts/flavor-setup-flutter/2c1b695b-5aa4-40e1-a916-3c585dbb1796.png) * Select the Runner scheme and click the Manage Schemes button. ![manage-scheme-button](https://saileshdahal.com.np/images/posts/flavor-setup-flutter/f2be4c69-88ec-4bfa-aedd-257b327bd970.png) * Once, there rename `Runner` scheme to `production`, and duplicate `production` to two new `development` and `staging`. ![duplicate-button](https://saileshdahal.com.np/images/posts/flavor-setup-flutter/131c1a60-3d98-411e-9b86-5db4fba1d6b9.png) * Make sure to add the correct configuration while duplicating. ![staging-scheme](https://saileshdahal.com.np/images/posts/flavor-setup-flutter/cf4fe682-11b5-458c-88aa-50cd3360cf29.jpeg) Similarly, create a `development` scheme with the correct configuration. ![development-scheme](https://saileshdahal.com.np/images/posts/flavor-setup-flutter/70050902-c61c-4920-b9bd-71bd11905135.png) Once you are done with schemes, you will have something like this. ![final-schemes](https://saileshdahal.com.np/images/posts/flavor-setup-flutter/81dc9e70-2888-47f3-b86a-2cb5c37d8b74.png) > Note: make sure all three schemes have correct configuration, don't forget to recheck `production` scheme configuration. * Now, let's add the bundle identifier for each configuration. In `Target`\-> `Runner`, click `Build Settings` and search for `Product Bundle Identifier`. ![bundle-identifier](https://saileshdahal.com.np/images/posts/flavor-setup-flutter/f8b3b271-b6f6-49ef-93c7-14977e69f8ef.jpeg) > Add the suffix as required. ![final-bundle-ids](https://saileshdahal.com.np/images/posts/flavor-setup-flutter/1a9e836a-13fb-408b-9504-5032e7bf78b7.png) Once you are done, you will have something like this. * Finally, we will create a new `User Defined Variable` called `APP_DISPLAY_NAME` which will have a different name for each configuration. In same `Target` -> `Runner` -> `Build Settings`, click on the `+` button, and create a new user-defined variable. ![user_defined_variable](https://saileshdahal.com.np/images/posts/flavor-setup-flutter/8d88f4d4-2ccb-4723-bd53-12c030d5b47f.png) ![APP_DISPLAY_NAME](https://saileshdahal.com.np/images/posts/flavor-setup-flutter/c0d571c2-5b53-49d0-86a3-a1bd0cdfd43f.png) Once, you are done with the user-defined variable, you need to change the `Info.plist` to use this variable. ```xml CFBundleName $(APP_DISPLAY_NAME) CFBundleDisplayName $(APP_DISPLAY_NAME) ``` --- #### Let's Test Similar to android, we can test iOS by running the following commands. ```bash flutter run --flavor development --target lib/main_development.dart flutter run --flavor staging --target lib/main_staging.dart flutter run --flavor production --target lib/production.dart ``` ![](https://saileshdahal.com.np/images/posts/flavor-setup-flutter/806598ad-33cc-4e3b-8c85-df0e578cf60b.png) --- ### App icons for Android and iOS > Let's make it look pretty! Now, let's have different icons based on the flavors. We will have 3 different icons for each flavor. ![launch-icons](https://saileshdahal.com.np/images/posts/flavor-setup-flutter/ae0ed32b-1b2e-43c9-8d23-32194e23ad45.png) We will use [`flutter_launcher_icons`](https://pub.dev/packages/flutter_launcher_icons) to generate the launch icons. Install it as a dev dependency. ```bash flutter pub add flutter_launcher_icons --dev ``` Once this is done, we will create `flutter_launcher_icons-.yaml` files for each flavor. * `flutter_launcher_icons-development.yaml` ```yaml flutter_icons: android: true ios: true remove_alpha_ios: true image_path: "assets/icons/dev-icon.png" ``` * `flutter_launcher_icons-staging.yaml` ```yaml flutter_icons: android: true ios: true remove_alpha_ios: true image_path: "assets/icons/stg-icon.png" ``` * `flutter_launcher_icons-production.yaml` ```yaml flutter_icons: android: true ios: true remove_alpha_ios: true image_path: "assets/icons/prod-icon.png" ``` Now to generate the launch icons we need to run ```bash flutter pub run flutter_launcher_icons:main -f flutter_launcher_icons-* ``` This will generate all the icons. Finally, we will set the icons to be dynamic in Xcode. * In `Runner` -> `Assets.xcassets` we can see that we have all the required icons. We can remove the `AppIcon` , which is unused. ![app-icons](https://saileshdahal.com.np/images/posts/flavor-setup-flutter/7b92cc30-14d2-46b1-8454-575049fa104d.png) * Now, in `Target`\-> `Runner` -> `Build Settings`, search for `primary app icon`. ![app-icon-change](https://saileshdahal.com.np/images/posts/flavor-setup-flutter/01802647-e968-4bdb-9cfa-0cec4f07e1fa.png) * Now, set the corresponding suffix to each one. ![final-app-icon](https://saileshdahal.com.np/images/posts/flavor-setup-flutter/77259f1f-7b3f-4bef-93c1-0b488881e650.png) --- ### Final result Now, if we run the app, for all the flavors, we will have different icons, names, versions, and bundle identifiers. > NOTE: Make sure to uninstall the previous builds before running the app. ![final-preview](https://saileshdahal.com.np/images/posts/flavor-setup-flutter/d5f7c534-ee4a-49dc-a990-bb1957ffbc8c.png) --- ## Bonus For easy usage, we can create some launch configs for `Visual Studio Code` and `Android Studio`. ### For Visual Studio Code Create a new file in `.vscode/launch.json` with the following content. ```json { "version": "0.2.0", "configurations": [ { "name": "DEV | DEBUG", "request": "launch", "type": "dart", "program": "lib/main_development.dart", "args": [ "--flavor", "development", "--target", "lib/main_development.dart" ], "flutterMode": "debug" }, { "name": "DEV | RELEASE", "request": "launch", "type": "dart", "program": "lib/main_development.dart", "args": [ "--flavor", "development", "--target", "lib/main_development.dart" ], "flutterMode": "release" }, { "name": "DEV | PROFILE", "request": "launch", "type": "dart", "program": "lib/main_development.dart", "args": [ "--flavor", "development", "--target", "lib/main_development.dart" ], "flutterMode": "profile" }, { "name": "STG | DEBUG", "request": "launch", "type": "dart", "program": "lib/main_staging.dart", "args": [ "--flavor", "staging", "--target", "lib/main_staging.dart" ], "flutterMode": "debug" }, { "name": "STG | RELEASE", "request": "launch", "type": "dart", "program": "lib/main_staging.dart", "args": [ "--flavor", "staging", "--target", "lib/main_staging.dart" ], "flutterMode": "release" }, { "name": "STG | PROFILE", "request": "launch", "type": "dart", "program": "lib/main_staging.dart", "args": [ "--flavor", "staging", "--target", "lib/main_staging.dart" ], "flutterMode": "profile" }, { "name": "PROD | DEBUG", "request": "launch", "type": "dart", "program": "lib/main_production.dart", "args": [ "--flavor", "production", "--target", "lib/main_production.dart" ], "flutterMode": "debug" }, { "name": "PROD | RELEASE", "request": "launch", "type": "dart", "program": "lib/main_production.dart", "args": [ "--flavor", "production", "--target", "lib/main_production.dart" ], "flutterMode": "release" }, { "name": "PROD | PROFILE", "request": "launch", "type": "dart", "program": "lib/main_production.dart", "args": [ "--flavor", "production", "--target", "lib/main_production.dart" ], "flutterMode": "profile" }, ] } ``` This will create the launch config with all the args. ![vs-code-launch-settings](https://saileshdahal.com.np/images/posts/flavor-setup-flutter/11fa8f57-66cb-4452-8ba2-d7f8dfa74b98.png) ### For Android Studio For Android Studio, you can get the [configuration files from here](https://github.com/saileshbro/fitness_app_clone/tree/master/.idea/runConfigurations), for all the configs. ![android-studio-launch-settings](https://saileshdahal.com.np/images/posts/flavor-setup-flutter/a3b68a58-d26d-4eb6-bd59-c0ce6d6a42a5.png) --- We added different flavors to the Flutter application, each with its own unique configurations and build targets. Flavors do more than pick an environment, though. For instance, you can inject dependencies based on the flavor you are using, which can entirely change the behavior of your app. The repository for this tutorial is at [https://github.com/saileshbro/fitness\_app\_clone](https://github.com/saileshbro/fitness_app_clone); if you get stuck, submit an issue or contribute a pull request. --- ### References * [Flutter Flavors, App Icons, and Firebase Tutorial - Marcus Ng](https://www.youtube.com/watch?v=Vhm1Cv2uPko) * [Creating flavors for Flutter](https://docs.flutter.dev/deployment/flavors) * [Android Developers - Build Variants: Flavor Dimensions](https://developer.android.com/studio/build/build-variants#flavor-dimensions) ============================================================================== # Dart Frog and Flutter JWT authentication tutorial (Part 6) URL: https://saileshdahal.com.np/building-a-fullstack-app-with-dartfrog-and-flutter-in-a-monorepo-part-6 Published: 2023-01-31 Tags: Flutter, Dart, Programming Blogs, full stack Series: Building a Fullstack App with dart_frog and Flutter in a Monorepo (part 6 of 6) ============================================================================== In earlier sections of this tutorial series, we covered the fundamental stages for creating a to-do application with Flutter and Dart. We've covered everything from bootstrapping an empty [Flutter](https://flutter.dev) project, importing necessary dependencies, setting up the folder structure, integrating the frontend with the backend, and implementing the Todo data sources, repositories, and view models. In this part, we'll show you how to use dart frog to establish user authentication with [JSON Web Tokens (JWT)](https://jwt.io). By the end of this article, we will be able to: * Implement user login and register. * Create `User` model. * Create and implement `UserRepository` and `UserDataSource`. * Handle `UnauthorizedException`. * Create a new authorization middleware. * Implement user authentication using [JWT](https://jwt.io). * Secure routes with `Authorization` headers. Don't forget to check out the [GitHub](https://github.com/saileshbro/full_stack_todo_dart) repo for this article if you need any assistance along the way. --- ## Overview When we're done, we should have an app that supports the following requests: 1. `/todos*` - Guard all to-do routes with `Authorization` header. ```bash curl -s -L -X POST 'http://localhost:8080/todos' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY5ZWFkZTg3LWRhNWUtNDRjZC1iNTRlLTQ3NGE4NWQ4ZGVlNCIsIm5hbWUiOiJTYWlsZXNoIERhaGFsIiwiZW1haWwiOiJzYWlsZXNoYnJvQGdtYWlsLmNvbSIsImNyZWF0ZWRfYXQiOiIyMDIzLTAxLTIzVDIyOjU0OjM4Ljg2NDkxMloiLCJwYXNzd29yZCI6IiQyYSQxMCR6VjBTZlI3cUpHOHlCelJWWThtNkRlMWo0UUtHL2VRdXl6NzduQ1lBL1luZENtb1ZSbzB0RyIsImlhdCI6MTY3NDQ5Mzc3OX0.W36mHXKFrxZDhz0Hrxt0Cmrz3WNVexiAZe2KAjrWMaE' -H 'Content-Type: application/json' --data-raw '{ "title":"this is a title asdf", "description":"Not so great description asdf" }' ``` 1. `/users/login` - Login user ```bash curl -s -L -X POST 'http://localhost:8080/users/login' -H 'Content-Type: application/json' --data-raw '{ "email":"saileshbro@gmail.com", "password":"6aMj@UBByu" }' ``` 1. `/users/signup` - Register new user ```bash curl -s -L -X POST 'http://localhost:8080/users/signup' -H 'Content-Type: application/json' --data-raw '{ "email":"saileshbro@gmail.com", "name":"Sailesh Dahal", "password":"6aMj@UBByu%7BzN^C9tMe#Te4b!4cJrXwwFi#HgKrQ&g&" }' ``` --- ## Updating Database To begin, we will create a new table called `users` and add a [`foreign key`](https://www.postgresqltutorial.com/postgresql-tutorial/postgresql-foreign-key/) to our current `todos` table. This ensures that each to-do item is associated with a single user, and that data is accessible only to that person. ### Create `users` table We will create a new `users` table with the following columns. * `id` : unique `uuid` * `name` : name of the user * `email` : email of the user * `password` : hashed password of the user * `created_at` : timestamp of when the user was created ```sql CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; CREATE TABLE users( id uuid DEFAULT uuid_generate_v4() PRIMARY KEY, name VARCHAR(255) NOT NULL, email TEXT NOT NULL, password TEXT NOT NULL, created_at timestamp default current_timestamp NOT NULL ); ``` > The [`uuid-ossp`](https://www.postgresql.org/docs/10/uuid-ossp.html) extension is used to generate unique IDs for each user. ### Updating `todos` table We will update the `todos` table to include a foreign key to the `users` table. ```sql TRUNCATE TABLE todos; ALTER TABLE todos ADD COLUMN user_id uuid NOT NULL, ADD CONSTRAINT constraint_fk FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE; ``` > The [`ON DELETE CASCADE`](https://www.commandprompt.com/education/postgresql-delete-cascade-with-examples/) clause ensures that when a user is deleted, all their to-do items will be deleted. Once we are done with the changes, we can proceed to create a user schema in our `models` package. > More about [`CONSTRAINTS`](https://www.postgresql.org/docs/current/ddl-constraints.html) here. --- ## Create `User` model > Defining our User Model We will start by creating a `UserId` in our `typedefs` package. ### Create `UserId` type In `typedefs` package, we will make some changes. We will rename the older `src/typedefs.dart` file to `src/todo_types.dart` and create a new `src/user_types.dart` file. ```dart typedef UserId = String; ``` > Make sure to update the exports. ```dart library typedefs; export 'src/todo_types.dart'; export 'src/user_types.dart'; ``` ### Update folder structure In `models` package, we will create a new `user.dart` file. Since we are creating models for a different feature, we will mode the todo specific models to a new `todo` folder. ```apache . ├── create_todo_dto │ └── create_todo_dto.dart ├── todo.dart └── update_todo_dto └── update_todo_dto.dart ``` Also, make sure to export `create_todo_dto` and `update_todo_dto` from `todo.dart` ```dart import 'package:freezed_annotation/freezed_annotation.dart'; import 'package:models/src/serializers/date_time_converter.dart'; import 'package:typedefs/typedefs.dart'; export './create_todo_dto/create_todo_dto.dart'; export './update_todo_dto/update_todo_dto.dart'; part 'todo.freezed.dart'; part 'todo.g.dart'; ``` ### Create `User` model Now, we will create a `User` model in `src/user/user.dart` file. ```dart import 'package:freezed_annotation/freezed_annotation.dart'; import 'package:models/src/serializers/date_time_converter.dart'; import 'package:typedefs/typedefs.dart'; part 'user.freezed.dart'; part 'user.g.dart'; @freezed class User with _$User { const factory User({ required UserId id, required String name, required String email, @DateTimeConverter() required DateTime createdAt, @Default('') @JsonKey(includeToJson: false) String password, }) = _User; factory User.fromJson(Map json) => _$UserFromJson(json); } ``` > We are ignoring the `password` field when serializing the model to JSON. This is because we don't want to send the password to the client. Similarly, we will create `CreateUserDto` and `LoginUserDto` for creating and logging in users. In `src/user/create_user_dto/create_user_dto.dart` file, ```dart import 'package:either_dart/either.dart'; import 'package:exceptions/exceptions.dart'; import 'package:failures/failures.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; part 'create_user_dto.freezed.dart'; part 'create_user_dto.g.dart'; @freezed class CreateUserDto with _$CreateUserDto { factory CreateUserDto({ required String name, required String email, required String password, }) = _CreateUserDto; factory CreateUserDto.fromJson(Map json) => _$CreateUserDtoFromJson(json); static Either validated( Map json, ) { try { final errors = >{}; final name = json['name'] as String? ?? ''; final email = json['email'] as String? ?? ''; final password = json['password'] as String? ?? ''; if (name.isEmpty) { errors['name'] = ['Name is required']; } if (email.isEmpty) { errors['email'] = ['Email is required']; } if (!email.contains('@')) { errors['email'] = ['Email is invalid']; } if (password.isEmpty) { errors['password'] = ['Password is required']; } if (password.length < 6) { errors['password'] = ['Password must be at least 6 characters']; } if (errors.isEmpty) return Right(CreateUserDto.fromJson(json)); throw BadRequestException( message: 'Validation failed', errors: errors, ); } on BadRequestException catch (e) { return Left( ValidationFailure( message: e.message, errors: e.errors, statusCode: e.statusCode, ), ); } } } ``` Similarly, we will create a new `login_user_dto`. ```dart import 'package:either_dart/either.dart'; import 'package:exceptions/exceptions.dart'; import 'package:failures/failures.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; part 'login_user_dto.freezed.dart'; part 'login_user_dto.g.dart'; @freezed class LoginUserDto with _$LoginUserDto { factory LoginUserDto({ required String email, required String password, }) = _LoginUserDto; factory LoginUserDto.fromJson(Map json) => _$LoginUserDtoFromJson(json); static Either validated( Map json, ) { try { final errors = >{}; final email = json['email'] as String? ?? ''; final password = json['password'] as String? ?? ''; if (email.isEmpty) { errors['email'] = ['Email is required']; } if (password.isEmpty) { errors['password'] = ['Password is required']; } if (errors.isEmpty) return Right(LoginUserDto.fromJson(json)); throw BadRequestException( message: 'Validation failed', errors: errors, ); } on BadRequestException catch (e) { return Left( ValidationFailure( message: e.message, errors: e.errors, statusCode: e.statusCode, ), ); } } } ``` These data transfer objects will be used to send, receive and validate data from the client in the backend. > Make sure to export `create_user_dto` and `login_user_dto` from `user.dart`, and `user.dart` from `models.dart` ```dart import 'package:freezed_annotation/freezed_annotation.dart'; import 'package:models/src/serializers/date_time_converter.dart'; import 'package:typedefs/typedefs.dart'; export './create_user_dto/create_user_dto.dart'; export './login_user_dto/login_user_dto.dart'; part 'user.freezed.dart'; part 'user.g.dart'; ``` ```dart library models; export 'src/todo/todo.dart'; export 'src/user/user.dart'; ``` > Make sure to run `flutter pub run build_runner build` to generate new files. --- ## Creating `UserDataSource` > Building the foundation for user management with `UserDataSource` In `data_source` package, we will create a new interface called `UserDataSource` in `data_source/lib/src/user_data_source.dart` ```dart import 'package:models/models.dart'; import 'package:typedefs/typedefs.dart'; abstract class UserDataSource { Future getUserById(UserId id); Future createUser(CreateUserDto user); Future getUserByEmail(String email); } ``` We will have methods to query and create users. We will do the implementation in our `backend`. --- ## Creating `UserRepository` > Designing our User Management System with UserRepository We will also create an interface called `UserRepository` which will return either a failure or valid data making use of `UserDataSource`. In `repository` package, we will create a new interface called `UserRepository` in `repository/lib/src/user_repository.dart` ```dart import 'package:either_dart/either.dart'; import 'package:failures/failures.dart'; import 'package:models/models.dart'; import 'package:typedefs/typedefs.dart'; abstract class UserRepository { Future> getUserById(UserId id); Future> createUser(CreateUserDto createUserDto); Future> loginUser(LoginUserDto loginUserDto); Future> getUserByEmail(String email); } ``` We will have all the methods we need to create, query and login users. We will do the implementation in our backend. > Make sure to export `UserRepository` from `repository.dart` ```dart library repository; export 'src/todo_repository.dart'; export 'src/user_repository.dart'; ``` --- ## Implementing `UserDataSource` > Bringing `UserDataSource` to life Now, we will implement `UserDataSource` in our backend. In `backend/lib/user/data_source/user_data_source_impl.dart`, we will create a new class called `UserDataSourceImpl` which will implement `UserDataSource`. This is how an empty implementation of `UserDataSourceImpl` will look like. ```dart import 'package:data_source/data_source.dart'; import 'package:models/models.dart'; import 'package:typedefs/typedefs.dart'; class UserDataSourceImpl implements UserDataSource { UserDataSourceImpl(this._databaseConnection); final DatabaseConnection _databaseConnection; Future createUser(CreateUserDto user) { throw UnimplementedError(); } @override Future getUserByEmail(String email) { throw UnimplementedError(); } @override Future getUserById(UserId id) { throw UnimplementedError(); } } ``` ### Implementing `createUser` method Now, we will implement `createUser` method. We will use `DatabaseConnection` to connect to our database and create a new user from the dto we received. ```dart @override Future createUser(CreateUserDto user) async { try { await _databaseConnection.connect(); final result = await _databaseConnection.db.query( ''' INSERT INTO users (name, email, password) VALUES (@name, @email, @password) RETURNING * ''', substitutionValues: user.toJson(), ); if (result.affectedRowCount == 0) { throw const ServerException('Failed to create todo'); } final userMap = result.first.toColumnMap(); return User.fromJson(userMap); } on PostgreSQLException catch (e) { throw ServerException(e.message ?? 'Unexpected error'); } finally { await _databaseConnection.close(); } } ``` We will validate the DTO in the repository before passing it to the data source. The database will then be queried to generate a new user. We will throw a `ServerException` if the user is not created, else, we will return the user that was created. ### Implementing `getUserByEmail` method ```dart @override Future getUserByEmail(String email) async { try { await _databaseConnection.connect(); final result = await _databaseConnection.db.query( ''' SELECT id, name, email, password, created_at FROM users WHERE email = @email ''', substitutionValues: {'email': email}, ); if (result.isEmpty) { throw const NotFoundException('User not found'); } return User.fromJson(result.first.toColumnMap()); } on PostgreSQLException catch (e) { throw ServerException(e.message ?? 'Unexpected error'); } finally { await _databaseConnection.close(); } } ``` Here, we will query the database for the user with the given email. If the user is not found, we will throw a `NotFoundException`. We will handle this exception in the `UserController` and return a `404` response. ### Implementing `getUserById` method ```dart @override Future getUserById(UserId id) async { try { await _databaseConnection.connect(); final result = await _databaseConnection.db.query( 'SELECT id, name, email, created_at FROM users WHERE id = @id', substitutionValues: {'id': id}, ); if (result.isEmpty) { throw const NotFoundException('User not found'); } return User.fromJson(result.first.toColumnMap()); } on PostgreSQLException catch (e) { throw ServerException(e.message ?? 'Unexpected error'); } finally { await _databaseConnection.close(); } } ``` We will query the database for the user with the given id. If the user is not found, we will throw a `NotFoundException`. We will handle this exception in the `UserController` and return a `404` response. --- ## Creating `PasswordHasherService` > Keeping user passwords safe with `PasswordHasherService` We will create a new service to hash and validate hashed passwords before going on to the `UserRepository` implementation. The `CreateUserDto` with the hashed password will be passed to the `UserRepository`. In `backend/lib/services/password_hasher_service.dart`, we will create a new class called `PasswordHasherService`. We will use [`bcrypt`](https://pub.dev/packages/bcrypt) package to hash and verify the password. ```bash flutter pub add bcrypt ``` ```dart import 'package:bcrypt/bcrypt.dart'; class PasswordHasherService { const PasswordHasherService(); String hashPassword(String password) { return BCrypt.hashpw(password, BCrypt.gensalt()); } bool checkPassword(String password, String hashedPassword) { return BCrypt.checkpw(password, hashedPassword); } } ``` Now, we can pass this service as a dependency to `UserRepository` when we implement our repository. --- ## Implementing `UserRepository` > Bringing `UserRepository` to life Now, we will implement `UserRepository` in our backend. In `backend/lib/user/repository/user_repository_impl.dart`, we will create a new class called `UserRepositoryImpl` which will implement `UserRepository`. ```dart import 'package:backend/services/password_hasher_service.dart'; import 'package:data_source/data_source.dart'; import 'package:either_dart/either.dart'; import 'package:failures/failures.dart'; import 'package:models/models.dart'; import 'package:repository/repository.dart'; import 'package:typedefs/typedefs.dart'; class UserRepositoryImpl implements UserRepository { UserRepositoryImpl(this.dataSource, this.passwordHasherService); final UserDataSource dataSource; final PasswordHasherService passwordHasherService; @override Future> createUser(CreateUserDto createUserDto) { throw UnimplementedError(); } @override Future> getUserByEmail(String email) { throw UnimplementedError(); } @override Future> getUserById(UserId id) { throw UnimplementedError(); } @override Future> loginUser(LoginUserDto loginUserDto) { throw UnimplementedError(); } } ``` ### Implementing `getUserByEmail` method We will start by implementing `getUserByEmail`. We will use the data source's `dataSource.getUserByEmail` method to see if we get a `User` object. If we get a `User` object, we shall wrap it in a `Right` object and return it. If an error occurs, we will return a `Left` object along with a `ServerFailure` object. ```dart @override Future> getUserByEmail(String email) async { try { final user = await dataSource.getUserByEmail(email); return Right(user); } catch (e) { log(e.toString()); return const Left( ServerFailure( message: 'User with this email does not exist', statusCode: HttpStatus.notFound, ), ); } } ``` ### Implementing `getUserById` method Similarly, we will call `dataSource.getUserById` method and see whether we get a `User` object. If we get a `User` object, we shall wrap it in a `Right` object and return it. If an error occurs, we will return a `Left` object along with a `ServerFailure` object. ```dart @override Future> getUserById(UserId id) async { try { final res = await dataSource.getUserById(id); return Right(res); } on NotFoundException catch (e) { log(e.message); return Left( ServerFailure( message: e.message, statusCode: e.statusCode, ), ); } on ServerException catch (e) { log(e.message); return Left( ServerFailure(message: e.message), ); } } ``` ### Implementing `loginUser` method Now, we will implement `loginUser`. We will check if the user with the given email exists. If the user exists, we will check the password with `PasswordHasherService`. If they do not match, we will return a failure, else we will return the logged in user. This method will be used by the `UserController` to create an access token for the user. ```dart @override Future> loginUser(LoginUserDto loginUserDto) async { try { final email = loginUserDto.email; final userExists = await getUserByEmail(email); if (userExists.isLeft) { throw const ServerException('Invalid email or password'); } final user = userExists.right; final password = loginUserDto.password; final isPasswordCorrect = passwordHasherService.checkPassword(password, user.password); if (!isPasswordCorrect) { throw const ServerException('Invalid email or password'); } return Right(user); } catch (e) { log(e.toString()); return const Left( ServerFailure( message: 'Invalid email or password', statusCode: HttpStatus.unauthorized, ), ); } } ``` ### Implementing `createUser` method Similarly, we will implement `createUser`. We will check if the user with the given email exists. If the user exists, we will return an failure, else we will create the user and return it. ```dart @override Future> createUser(CreateUserDto createUserDto) async { try { final userExists = await getUserByEmail(createUserDto.email); if (userExists.isRight) { throw const ServerException('Email already in use'); } // dto is already validated in the controller // we will hash the password here final hashedPassword = passwordHasherService.hashPassword( createUserDto.password, ); final user = await dataSource.createUser( createUserDto.copyWith( password: hashedPassword, ), ); return Right(user); } on ServerException catch (e) { log(e.message); return Left( ServerFailure(message: e.message), ); } } ``` --- ## Implementing `JwtService` > Securing User Login with JWT Now, once we have all the necessary implementation of the repository and the data source, we will implement `JwtService` before implementing our `UserController`. This service will be used to create a JWT token for the user when they log in or signup. In `backend/lib/user/service/jwt_service.dart`, we will create a new class called `JwtService`. We will use [`dart_jsonwebtoken`](https://pub.dev/packages/dart_jsonwebtoken) package to create and verify JWT tokens. ```bash flutter pub add dart_jsonwebtoken ``` Now, we will implement the `JwtService` class. ```dart import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart'; import 'package:dotenv/dotenv.dart'; class JWTService { const JWTService(this._env); final DotEnv _env; String sign(Map payload) { final secret = _env['JWT_SECRET']!; final jwt = JWT(payload); return jwt.sign(SecretKey(secret)); } Map verify(String token) { final secret = _env['JWT_SECRET']!; final jwt = JWT.verify(token, SecretKey(secret)); return jwt.payload as Map; } } ``` > NOTE: We are using `dotenv` package to get the `JWT_SECRET` from the `.env` file. Make sure you have added the `JWT_SECRET` to the `.env` file. ```apache ... JWT_SECRET=verycomplexsupersecret ``` Once this is done, we will implement the `UserController`. --- ## Tidy Up Time > Make Room for Improvement ### Remove duplicates We have multiple routes that are not implemented and are returning the same response. We will remove these routes and create a new `Handler` to handle these requests. We will create a new `Handler` called `notAllowedRequestHandler` in `backend/lib/request_handlers/not_allowed_request_handler.dart` to handle these requests. ```dart Future notAllowedRequestHandler(RequestContext context) async { return Response.json( body: {'error': '👀 Looks like you are lost 🔦'}, statusCode: HttpStatus.methodNotAllowed, ); } ``` Then, we will replace the routes with this handler. * `backend/routes/index.dart` ```dart import 'package:backend/request_handlers/not_allowed_request_handler.dart'; import 'package:dart_frog/dart_frog.dart'; Handler onRequest = notAllowedRequestHandler; ``` * `backend/routes/todos/index.dart` * `backend/routes/todos/[id].dart` ```diff + return notAllowedRequestHandler(context); - return Response.json( - body: {'error': '👀 Looks like you are lost 🔦'}, - statusCode: HttpStatus.methodNotAllowed, - ); ``` ### Refactor `HttpController` Right now, if we have to implement `HttpController`, we will have to override all the methods. We will refactor the `HttpController` to make it optional to override all the methods. we will create a new handler called `unimplementedHandler` in `backend/lib/request_handlers/unimplemented_handler.dart` to handle the unimplemented methods. ```dart import 'dart:io'; import 'package:dart_frog/dart_frog.dart'; Future unimplementedHandler([RequestContext? context]) async { return Response.json( body: {'error': '👀 Not implemented yet'}, statusCode: HttpStatus.notImplemented, ); } ``` Then, we will refactor the `HttpController` to use this handler. In `backend/lib/controller/http_controller.dart`, we will change the methods to call and return `unimplementedHandler`. ```dart abstract class HttpController { FutureOr index(Request request) => unimplementedHandler(); FutureOr store(Request request) => unimplementedHandler(); FutureOr show(Request request, String id) => unimplementedHandler(); FutureOr update(Request request, String id) => unimplementedHandler(); FutureOr destroy(Request request, String id) => unimplementedHandler(); } ``` --- ## Implementing `UserController` Now, we will implement `UserController` in `user/controller/user_controller.dart`. ```dart import 'dart:async'; import 'package:backend/controller/http_controller.dart'; import 'package:backend/services/jwt_service.dart'; import 'package:dart_frog/dart_frog.dart'; import 'package:repository/repository.dart'; class UserController extends HttpController { UserController(this._repo, this._jwtService); final UserRepository _repo; final JWTService _jwtService; @override FutureOr store(Request request) async { throw UnimplementedError(); } FutureOr login(Request request) async { throw UnimplementedError(); } } ``` We will override `store` method, which will responsible for creating and storing the user, and then another `login` method, to log in the user. Also, we will create a new private method called `_signAndSendToken` which will take the user, and then sign and send the user along with the JWT token. ### Implement `_signAndSendToken` method We will use this method once `login` and `store` method are successful and when we want to send a response to the user. ```dart Response _signAndSendToken(User user, [int? httpStatus]) { final token = _jwtService.sign(user.toJson()); return Response.json( body: { 'token': token, 'user': user.toJson()..remove('password'), }, statusCode: httpStatus ?? HttpStatus.ok, ); } ``` ### Implement `store` method We will parse the JSON body, and validate the body with `CreateUserDto`. Once validated, we will call `repository.createUser` which will hash the password and create a new user if not already there. Once this is done, we will call `_signAndSendToken` and return with `201` status code. ```dart @override FutureOr store(Request request) async { final parsedBody = await parseJson(request); if (parsedBody.isLeft) { return Response.json( body: {'message': parsedBody.left.message}, statusCode: parsedBody.left.statusCode, ); } final json = parsedBody.right; final createTodoDto = CreateUserDto.validated(json); if (createTodoDto.isLeft) { return Response.json( body: { 'message': createTodoDto.left.message, 'errors': createTodoDto.left.errors, }, statusCode: createTodoDto.left.statusCode, ); } final res = await _repo.createUser(createTodoDto.right); return res.fold( (left) => Response.json( body: {'message': left.message}, statusCode: left.statusCode, ), (right) => _signAndSendToken(right, HttpStatus.created), ); } ``` ### Implement `login` Similar to `store`, we will parse and verify `LoginUserDto`, and call `repository.loginUser` which will verify the password and return the user. ```dart FutureOr login(Request request) async { final parsedBody = await parseJson(request); if (parsedBody.isLeft) { return Response.json( body: {'message': parsedBody.left.message}, statusCode: parsedBody.left.statusCode, ); } final json = parsedBody.right; final loginUserDto = LoginUserDto.validated(json); if (loginUserDto.isLeft) { return Response.json( body: { 'message': loginUserDto.left.message, 'errors': loginUserDto.left.errors, }, statusCode: loginUserDto.left.statusCode, ); } final res = await _repo.loginUser(loginUserDto.right); return res.fold( (left) => Response.json( body: {'message': left.message}, statusCode: left.statusCode, ), _signAndSendToken, ); } ``` --- ## Adding Dependency Injections In our global middleware `backend/routes/_middleware.dart`, we will register the dependencies. ```dart import 'package:backend/db/database_connection.dart'; import 'package:backend/services/jwt_service.dart'; import 'package:backend/services/password_hasher_service.dart'; import 'package:backend/user/controller/user_controller.dart'; import 'package:backend/user/data_source/user_data_source_impl.dart'; import 'package:backend/user/repositories/user_repository_impl.dart'; import 'package:dart_frog/dart_frog.dart'; import 'package:data_source/data_source.dart'; import 'package:dotenv/dotenv.dart'; import 'package:repository/repository.dart'; final env = DotEnv()..load(); final _db = DatabaseConnection(env); final _userDs = UserDataSourceImpl(_db); const _passwordHasher = PasswordHasherService(); final _userRepo = UserRepositoryImpl(_userDs, _passwordHasher); final _jwtService = JWTService(env); final _userController = UserController(_userRepo, _jwtService); Handler middleware(Handler handler) { return handler .use(requestLogger()) .use(provider((_) => _db)) .use(provider((_) => _jwtService)) .use(provider((_) => _userDs)) .use(provider((_) => _userRepo)) .use(provider((_) => _userController)) .use(provider((_) => _passwordHasher)); } ``` --- ## Setting up routes > Connecting the dots - defining API endpoints We will now setup the routes for `UserController` in `backend/routes/user` we will create three new files `index.dart`, `login.dart`, and `signup.dart`. Doing this we will have three different routes: * `/user` - `index.dart` * `/user/login` - `login.dart` * `/user/signup` - `signup.dart` ### `index.dart` This route does nothing. We will just return `unimplementedHandler`. ```dart import 'package:backend/request_handlers/not_allowed_request_handler.dart'; import 'package:dart_frog/dart_frog.dart'; Handler onRequest = notAllowedRequestHandler; ``` ### `login.dart` We will get the `UserController` from the `RequestContext` and call `login` method for `POST` requests. ```dart import 'dart:async'; import 'package:backend/request_handlers/not_allowed_request_handler.dart'; import 'package:backend/user/controller/user_controller.dart'; import 'package:dart_frog/dart_frog.dart'; FutureOr onRequest(RequestContext context) { final userController = context.read(); if (context.request.method != HttpMethod.post) { return notAllowedRequestHandler(context); } return userController.login(context.request); } ``` ### `signup.dart` Similar to `login.dart`, we will get the `UserController` from the `RequestContext` and call `store` method for `POST` requests. ```dart import 'dart:async'; import 'package:backend/request_handlers/not_allowed_request_handler.dart'; import 'package:backend/user/controller/user_controller.dart'; import 'package:dart_frog/dart_frog.dart'; FutureOr onRequest(RequestContext context) { final userController = context.read(); if (context.request.method != HttpMethod.post) { return notAllowedRequestHandler(context); } return userController.store(context.request); } ``` > NOTE: Recheck your `.env` file before testing the routes. --- ## Testing routes > Testing Time - Verify Your Routes! Once we have set up the routes, we can test them using `curl` or `postman`. ### `/users/login` #### When the email and password match * `REQUEST` ```bash curl -s -L -X POST 'http://localhost:8080/users/login' -H 'Content-Type: application/json' --data-raw '{ "email":"saileshbro@gmail.com", "password":"6aMj@UBByu%7BzN^C9tMe#Te4b!4cJrXwwFi#HgKrQ&g&ddNN6eHQ94vd5SuJtEc%7^H6L^xews8soG@R7GnW*RvfJVMaKEuBXNtVtbP5!3^qs*n!Z%87q8eRJmKFUHg" }' ``` * `RESPONSE` ```json { "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjQ4ODY5Yjc4LWEwMjgtNGMwNS1hN2QzLTQ5OTQ2Mzk4NDI2NSIsIm5hbWUiOiJTYWlsZXNoIERhaGFsIiwiZW1haWwiOiJzYWlsZXNoQGdtYWlsLmNvbSIsImNyZWF0ZWRfYXQiOiIyMDIzLTAxLTMwVDA5OjU3OjQ5LjExODM2MVoiLCJwYXNzd29yZCI6IiQyYSQxMCRGSDVDc2N1Y0VuLlNwWlZmSWFtRGwuZzRMaDFhd2ltbVRMS05yU2NORW1qT25lSFZHOE4wLiIsImlhdCI6MTY3NTA2MDA0MH0.qvzLWgAEphlYqZztoBf7Bvag6hO1qkp44hUwl78CMVo", "user": { "id": "48869b78-a028-4c05-a7d3-499463984265", "name": "Sailesh Dahal", "email": "saileshbro@gmail.com", "created_at": "2023-01-30T09:57:49.118361Z" } } ``` #### When the email or password does not match. * `REQUEST` ```bash curl -s -L -X POST 'http://localhost:8080/users/login' -H 'Content-Type: application/json' --data-raw '{ "email":"sa@gmail.com", "name":"Sailesh Dahal", "password":"6aMj@UBByu%7BzN^C9tMe#Te4b!4cJrXwwFi#HgKrQ&g&ddNN6eHQ94vd5SuJtEc%7^H6L^xews8soG@R7GnW*RvfJVMaKEuBXNtVtbP5!3^qs*n!Z%87q8eRJmKFUHg" }' ``` * `RESPONSE` ```json { "message": "Invalid email or password" } ``` ### `/users/signup` #### When the data is not valid * `REQUEST` ```bash curl -s -L -X POST 'http://localhost:8080/users/signup' -H 'Content-Type: application/json' --data-raw '{ "email":"sailes@gmail.com", "password":"6aMj@UBByu" }' ``` * `RESPONSE` ```json { "message": "Validation failed", "errors": { "name": ["Name is required"] } } ``` #### When there is no user with the given email, it will create a new user and return the same response as `/users/login`. * `REQUEST` ```bash curl -s -L -X POST 'http://localhost:8080/users/signup' -H 'Content-Type: application/json' --data-raw '{ "email":"sailesh12@gmail.com", "name":"Sailesh Dahal", "password":"6aMj123" }' ``` * `RESPONSE` ```json { "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6ImM1ODE2YWQ0LTNkODctNDUzZC1hZmFkLTUwMzljY2YxZjdjNyIsIm5hbWUiOiJTYWlsZXNoIERhaGFsIiwiZW1haWwiOiJzYWlsZXNoMTJAZ21haWwuY29tIiwiY3JlYXRlZF9hdCI6IjIwMjMtMDEtMzBUMDY6Mjg6MzUuMzkyMjM4WiIsInBhc3N3b3JkIjoiJDJhJDEwJHRuTllGM3FHQmlPT1dKeHVacm1MaU91SHhBMU1HQlNMcmoxYS5ndHY2TTNCMHpmRW5Dc1dLIiwiaWF0IjoxNjc1MDYwMTE0fQ.aUpo9HXTFmdmkTsfGoTp0mcK0OJ_fFuwZArqyNFpKvQ", "user": { "id": "c5816ad4-3d87-453d-afad-5039ccf1f7c7", "name": "Sailesh Dahal", "email": "sailesh12@gmail.com", "created_at": "2023-01-30T06:28:35.392238Z" } } ``` #### When there is a user with the given email, it will return the following response. * `REQUEST` ```bash curl -s -L -X POST 'http://localhost:8080/users/signup' -H 'Content-Type: application/json' --data-raw '{ "email":"sailesh12@gmail.com", "name":"Sailesh Dahal", "password":"6aMj123" }' ``` * `RESPONSE` ```json { "message": "Email already in use" } ``` --- ## Protecting Routes > Locking Down: Secure Your Endpoints Now, we can protect our routes by adding middleware to the routes. We will check for the `Authorization` header and verify the token. We can intercept all the requests going to `/todos/*` and check for the token. We can do this by creating a new authorization middleware. We will create a new file `backend/lib/middlewares/authorization_middleware.dart`. Before that, let's create a new exception for unauthorized requests. ### Creating `UnauthorizedException` When the token is not valid, we will throw an `UnauthorizedException` which will be handled by the `ExceptionHandlerMiddleware`. In `exceptions` package, we will create a new exception called `UnauthorizedException` in `exceptions/lib/src/http_exception/unauthorized_exception.dart` ```dart import 'dart:io'; import 'package:exceptions/src/http_exception/http_exception.dart'; class UnauthorizedException extends HttpException { const UnauthorizedException({ String message = 'Unauthorized', this.errors = const {}, }) : super(message, HttpStatus.unauthorized); final Map> errors; } ``` > Make sure to export the exception in `http_exception.dart` file. ```dart export './bad_request_exception.dart'; export './not_found_exception.dart'; export './unauthorized_exception.dart'; ``` > Make sure to run `flutter pub run build_runner build` to generate the code. ### Creating `AuthorizationMiddleware` > Securing Endpoints with AuthorizationMiddleware Now, we can create the `AuthorizationMiddleware` in `backend/lib/middlewares/authorization_middleware.dart`. ```dart import 'dart:io'; import 'package:backend/db/database_connection.dart'; import 'package:backend/services/jwt_service.dart'; import 'package:backend/todo/controller/todo_controller.dart'; import 'package:backend/todo/data_source/todo_data_source_impl.dart'; import 'package:backend/todo/repositories/todo_repository_impl.dart'; import 'package:dart_frog/dart_frog.dart'; import 'package:data_source/data_source.dart'; import 'package:exceptions/exceptions.dart'; import 'package:models/models.dart'; import 'package:repository/repository.dart'; Handler authorizationMiddleware(Handler handler) { return (context) async { try { final request = context.request; final authHeader = request.headers[HttpHeaders.authorizationHeader] ?? ''; final token = authHeader.replaceFirst('Bearer ', ''); if (token.isEmpty) throw const UnauthorizedException(); final jwtService = context.read(); final decoded = jwtService.verify(token); final decodedUser = User.fromJson(decoded); final userRepo = context.read(); final user = await userRepo.getUserById(decodedUser.id); if (user.isLeft) throw const UnauthorizedException(); context = _handleAuthDependencies(context, user.right); return handler(context); } on UnauthorizedException catch (e) { return Response.json( body: {'message': e.message}, statusCode: e.statusCode, ); } }; } RequestContext _handleAuthDependencies( RequestContext context, User user, ) { late RequestContext updatedContext; updatedContext = context.provide(() => user); return updatedContext; } ``` Here, we are checking for the `Authorization` header and verifying the token. If the token is valid, we are adding the `User` object to the context, so that we can get the logged-in user later from the provider. > If the token is invalid, we will return a `401` response. ### Updating auth dependencies Now, since we will have to add a `user_id` whenever, we will create a todo, we will have to pass the logged-in user to the TodoDataSource. We will update the `TodoDataSourceImpl` to accept the `User` object from the constructor. ```dart class TodoDataSourceImpl implements TodoDataSource { /// {@macro todo_data_source_impl} const TodoDataSourceImpl(this._databaseConnection, this.user); final DatabaseConnection _databaseConnection; final User user; } ``` Also, we will update `createTodo` and `updateTodo` methods to add the `user_id`. #### Update `createTodo` method ```diff @override Future createTodo(CreateTodoDto todo) async { try { await _databaseConnection.connect(); final result = await _databaseConnection.db.query( ''' - INSERT INTO todos (title, description, completed) + INSERT INTO todos (title, description, completed, user_id) - VALUES (@title, @description, @completed) RETURNING * + VALUES (@title, @description, @completed, @user_id) RETURNING * ''', substitutionValues: { 'title': todo.title, 'description': todo.description, 'completed': false, + 'user_id': _user.id, }, ); if (result.affectedRowCount == 0) { throw const ServerException('Failed to create todo'); } final todoMap = result.first.toColumnMap(); return Todo( id: todoMap['id'] as int, + userId: todoMap['user_id'] as String, title: todoMap['title'] as String, description: todoMap['description'] as String, createdAt: todoMap['created_at'] as DateTime, ); } on PostgreSQLException catch (e) { throw ServerException(e.message ?? 'Unexpected error'); } finally { await _databaseConnection.close(); } } ``` #### Update `updateTodo` method ```diff final result = await _databaseConnection.db.query( ''' UPDATE todos SET title = COALESCE(@new_title, title), description = COALESCE(@new_description, description), completed = COALESCE(@new_completed, completed), updated_at = current_timestamp WHERE id = @id + AND user_id = @user_id RETURNING * ''', substitutionValues: { 'id': id, + 'user_id': _user.id, 'new_title': todo.title, 'new_description': todo.description, 'new_completed': todo.completed, }, ); ``` #### Update `Todo` model to add `userId` We will also add the missing `userId` from `Todo` model. ```diff factory Todo({ required TodoId id, + required UserId userId, required String title, @Default('') String description, @Default(false) bool completed, @DateTimeConverter() required DateTime createdAt, @DateTimeConverterNullable() DateTime? updatedAt, }) = _Todo; ``` Now, once this is done, we will update the `_handleAuthDependencies` method in `authorization_middleware.dart` as ```dart RequestContext _handleAuthDependencies( RequestContext context, User user, ) { final db = context.read(); final todoDs = TodoDataSourceImpl(db, user); final todoRepo = TodoRepositoryImpl(todoDs); final todoController = TodoController(todoRepo); late RequestContext updatedContext; updatedContext = context.provide(() => user); updatedContext = updatedContext.provide(() => todoController); updatedContext = updatedContext.provide(() => todoRepo); updatedContext = updatedContext.provide(() => todoDs); return updatedContext; } ``` ### Using `AuthorizationMiddleware` > Applying `AuthorizationMiddleware` to routes Let's create a new middleware in `backend/routes/todos/_middleware.dart` and add the following code. ```dart import 'package:backend/middlewares/authorization_middleware.dart'; import 'package:dart_frog/dart_frog.dart'; Handler middleware(Handler handler) => authorizationMiddleware(handler); ``` Since this middleware lies inside `routes/todos`, all the `/todos/*` routes will be intercepted by this middleware. This means that all the `/todos/*` routes will be protected and if the user tries to access these routes without a valid token, it will return a `401` response. --- ## Testing protected `/todos/*` routes > Putting updated routes to the Test If we try to access the `/todos/*` routes without a valid token, it will return a `401` response. We can test this by running the following command. ### GET /todos * `REQUEST` ```bash curl -s -L -X GET 'http://localhost:8080/todos' ``` * `RESPONSE` ```json { "message": "Unauthorized" } ``` * `REQUEST` ```bash curl -s -L -X GET 'http://localhost:8080/todos' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY5ZWFkZTg3LWRhNWUtNDRjZC1iNTRlLTQ3NGE4NWQ4ZGVlNCIsIm5hbWUiOiJTYWlsZXNoIERhaGFsIiwiZW1haWwiOiJzYWlsZXNoYnJvQGdtYWlsLmNvbSIsImNyZWF0ZWRfYXQiOiIyMDIzLTAxLTIzVDIyOjU0OjM4Ljg2NDkxMloiLCJwYXNzd29yZCI6IiQyYSQxMCR6VjBTZlI3cUpHOHlCelJWWThtNkRlMWo0UUtHL2VRdXl6NzduQ1lBL1luZENtb1ZSbzB0RyIsImlhdCI6MTY3NDQ5Mzc3OX0.W36mHXKFrxZDhz0Hrxt0Cmrz3WNVexiAZe2KAjrWMaE' ``` * `RESPONSE` ```json [ { "id": 76, "user_id": "69eade87-da5e-44cd-b54e-474a85d8dee4", "title": "this is a title asdf", "description": "Not so great description asdf", "completed": false, "created_at": "2023-01-23T22:56:31.686023Z", "updated_at": "2023-01-30T04:08:06.349549Z" } ] ``` ### POST /todos * `REQUEST` ```bash curl -s -L -X POST 'http://localhost:8080/todos' -H 'Content-Type: application/json' --data-raw '{ "title":"this is a title asdf", "description":"Not so great description asdf" }' ``` * `RESPONSE` ```json { "message": "Unauthorized" } ``` * `REQUEST` ```bash curl -s -L -X POST 'http://localhost:8080/todos' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY5ZWFkZTg3LWRhNWUtNDRjZC1iNTRlLTQ3NGE4NWQ4ZGVlNCIsIm5hbWUiOiJTYWlsZXNoIERhaGFsIiwiZW1haWwiOiJzYWlsZXNoYnJvQGdtYWlsLmNvbSIsImNyZWF0ZWRfYXQiOiIyMDIzLTAxLTIzVDIyOjU0OjM4Ljg2NDkxMloiLCJwYXNzd29yZCI6IiQyYSQxMCR6VjBTZlI3cUpHOHlCelJWWThtNkRlMWo0UUtHL2VRdXl6NzduQ1lBL1luZENtb1ZSbzB0RyIsImlhdCI6MTY3NDQ5Mzc3OX0.W36mHXKFrxZDhz0Hrxt0Cmrz3WNVexiAZe2KAjrWMaE' -H 'Content-Type: application/json' --data-raw '{ "title":"this is a title asdf", "description":"Not so great description asdf" }' ``` * `RESPONSE` ```json { "id": 79, "user_id": "69eade87-da5e-44cd-b54e-474a85d8dee4", "title": "this is a title asdf", "description": "Not so great description asdf", "completed": false, "created_at": "2023-01-30T07:10:10.102358Z", "updated_at": null } ``` Similarly, we can test the other routes as well. --- Part 6 added user authentication with JWT to our [dart\_frog](https://dartfrog.vgv.dev/) backend. Across the series, we built a complete full-stack to-do application with Dart and Flutter. Testing isn't covered yet; let us know in the comments if you want it. The repo lives at [https://github.com/saileshbro/full\_stack\_todo\_dart](https://github.com/saileshbro/full_stack_todo_dart); if you get stuck, submit an issue or contribute a pull request. ============================================================================== # Dart Frog and Flutter monorepo: connect the app (Part 5) URL: https://saileshdahal.com.np/building-a-fullstack-app-with-dartfrog-and-flutter-in-a-monorepo-part-5 Published: 2023-01-13 Tags: Programming Blogs, Flutter, full stack, Dart, backend Series: Building a Fullstack App with dart_frog and Flutter in a Monorepo (part 5 of 6) ============================================================================== In the previous part of this series, we set up the foundation for our full-stack to-do application by creating models, data sources, repositories, exceptions and handling failures. In this part, we will build on that by: * Scaffolding an empty [`flutter`](https://flutter.dev) project * Importing necessary dependencies * Creating a folder structure * Connecting our front end with the backend using `TodosHttpClient` * Setting up dependency injection using [`injectable`](https://pub.dev/packages/injectable) and [`get_it`](https://pub.dev/packages/get_it) * Implementing `TodosRemoteDataSource` and `TodoRepository` * Handling errors as `NetworkFailure` * Creating `NetworkException` and `DioNetworkException` * Creating `NetworkErrorInterceptor` * Creating `ShowTodosView` and `ShowTodosViewModel` to show the fetched todos * Creating `MaintainTodoView` and `MaintainTodoViewModel` to maintain the todos * Adding linting with [`very_good_analysis`](https://pub.dev/packages/very_good_analysis) to make sure our code is clean. We will go deeper into flutter, following the conventions we set up and using the packages the ecosystem provides. And by the end of this part, we will have a fully functional front end for our to-do application. [▶ Watch on YouTube](https://www.youtube.com/shorts/7XJHrpCpzUo?feature=share) Don't forget to check the GitHub repo for this tutorial at [GitHub](https://github.com/saileshbro/full_stack_todo_dart) if you need a little help along the way. Let's get started ## Scaffolding Frontend We will begin by scaffolding an empty Flutter project using the command: ```bash flutter create --project-name fullstack_todo --org np.com.saileshdahal frontend ``` This will create an empty flutter project in `frontend` directory. We will then do some initial setup, and add some dependencies. ### Importing necessary dependencies We will import the necessary dependencies in the `pubspec.yaml` file. The dependencies that we are using are as follows. * [`dio`](https://pub.dev/packages/dio) * [`either_dart`](https://pub.dev/packages/either_dart) * [`flutter_hooks`](https://pub.dev/packages/flutter_hooks) * [`get_it`](https://pub.dev/packages/get_it) * [`injectable`](https://pub.dev/packages/injectable) * [`stacked`](https://pub.dev/packages/stacked) * [`stacked_services`](https://pub.dev/packages/stacked_services) * [`build_runner`](https://pub.dev/packages/build_runner) * [`very_good_analysis`](https://pub.dev/packages/very_good_analysis) * [`retrofit`](https://pub.dev/packages/retrofit) These cover HTTP, state management, and dependency injection for the app. To install you can use the following command: ```bash flutter pub add dio either_dart flutter_hooks get_it injectable stacked stacked_services retrofit flutter pub add --dev build_runner very_good_analysis stacked_generator injectable_generator retrofit_generator ``` We will also include the packages that we have built, once done `pubspec.yaml` should look something like this. ```yaml dependencies: data_source: path: ../data_source dio: ^4.0.6 either_dart: ^0.3.0 exceptions: path: ../exceptions failures: path: ../failures flutter: sdk: flutter flutter_hooks: ^0.18.5+1 get_it: ^7.2.0 injectable: ^2.1.0 models: path: ../models repository: path: ../repository retrofit: ^3.3.1 stacked: ^3.0.1 stacked_services: ^0.9.9 typedefs: path: ../typedefs dev_dependencies: build_runner: ^2.3.3 flutter_test: sdk: flutter injectable_generator: ^2.1.3 integration_test: sdk: flutter mockito: ^5.3.2 retrofit_generator: ^4.2.0 stacked_generator: ^0.8.3 very_good_analysis: ^3.1.0 ``` ### Folder Structure This is how we will architect our folder structures. ```apache . ├── constants ├── core │ ├── app │ ├── di │ └── network │ ├── exceptions │ └── interceptors ├── data │ ├── data_source │ │ ├── todo_http_client │ │ └── todo_remote_data_source │ └── repositories ├── data_services └── presentation ``` * `constants`: This folder will include all the hard-coded constants in our project, such as API endpoints or keys. * `core`: This directory will include all the core functionalities of our app, which are common for all the features. This includes routing, dependency injection, and network interceptors. * `data`: This directory will include all the implementations related to data sources and repositories. * `data_services`: This will include the implementation of services that will be shared across different view models. * `presentation`: This will include the views and view models for creating and listing The `data_source` and `repositories` folders contain the implementation of the abstract contracts defined in the `data_sources` and `repository` packages, respectively. This allows for the separation of concerns and easier maintenance of the codebase. ## Connecting with the backend Let's start by connecting with the [dart\_frog](https://pub.dev/packages/dart_frog) backend using [dio](https://pub.dev/packages/dio). For this, we will create a new class `TodosHttpClient` ### Create `TodosHttpClient` `TodosHttpClient` will be responsible for calling our backend. We will be using [`retrofit`](https://pub.dev/packages/retrofit) to generate the necessary codes to call the backend. Retrofit allows us to define our API endpoints as interfaces with annotated methods, and it will automatically generate the necessary code to handle the network requests and convert the JSON responses to Dart objects. We will create a new file in `data/data_source/todo_http_client/todos_http_client.dart` with the following code. ```dart import 'package:dio/dio.dart'; import 'package:injectable/injectable.dart'; import 'package:models/models.dart'; import 'package:retrofit/retrofit.dart'; import 'package:typedefs/typedefs.dart'; part 'todos_http_client.g.dart'; @RestApi() @lazySingleton abstract class TodosHttpClient { @factoryMethod factory TodosHttpClient(Dio _dio) = _TodosHttpClient; @GET('/todos') Future> getAllTodo(); @GET('/todos/{id}') Future getTodoById(@Path('id') TodoId id); @POST('/todos') Future createTodo(@Body() CreateTodoDto todo); @PATCH('/todos/{id}') Future updateTodo(@Path('id') TodoId id, @Body() UpdateTodoDto todo); @DELETE('/todos/{id}') Future deleteTodoById(@Path('id') TodoId id); } ``` After completing the steps, we will execute [`build_runner`](https://pub.dev/packages/build_runner) to generate the necessary code. This will create an implementation of `TodosHttpClient` and we will link the generated `_TodosHttpClient` to the factory method of this abstract class. The Dio object will be passed as a dependency through the constructor. Since we are using [`get_it`](https://pub.dev/packages/get_it) and [`injectable`](https://pub.dev/packages/injectable), it will be necessary to manage the injection of this dependency. ### Setup `injectable` Injectable is a package for Dart that allows for dependency injection in our application. It provides a simple and easy-to-use API for managing dependencies and injecting them into our classes. By using Injectable, we can easily swap out dependencies for different environments, like testing, and make our code more modular and testable. > Read more about [`injectable`](https://pub.dev/packages/injectable#registering-third-party-types). To handle how we inject the `Dio` instance, we will create some files to inject third-party modules. In the `core` directory, we will create a new directory called `di`, short for Dependency Injection. Here, create a new file called `locator.dart`, and add the following code. ```dart import 'package:fullstack_todo/core/di/locator.config.dart'; import 'package:get_it/get_it.dart'; import 'package:injectable/injectable.dart'; final locator = GetIt.instance; @injectableInit void setupLocator() => locator.init(); ``` Now, we will create another file called `third_party_modules.dart`, this file will have a getter which will resolve the instance of `Dio`. ```dart import 'package:dio/dio.dart'; import 'package:injectable/injectable.dart'; @module abstract class ThirdPartyModules { @lazySingleton Dio get dio => Dio(BaseOptions(baseUrl: kBaseUrl)); } ``` Then, we will create the missing `kBaseUrl` constant in `constants/constants.dart` file. ```dart const String kBaseUrl = 'http://localhost:8080'; ``` > Note: The port for you may be different, please check what port your backend will run by running `dart_frog dev` in the `backend` folder. If you check the generated code in `locator.config.dart`, it creates the instance of `dio` and passes it to our `TodosHttpClient`. ### Implement `TodosRemoteDataSource` We will now use `TodosHttpClient` to implement our `TodosDataSource`. For this, we will create `data_source/todos_remote_data_source.dart` file, and we will implement `TodosDataSource` as follows. ```dart import 'package:data_source/data_source.dart'; import 'package:fullstack_todo/data/data_source/todos_http_client/todos_http_client.dart'; import 'package:injectable/injectable.dart'; import 'package:models/models.dart'; import 'package:typedefs/typedefs.dart'; @LazySingleton(as: TodoDataSource) class TodosRemoteDataSource implements TodoDataSource { const TodosRemoteDataSource(this.httpClient); final TodosHttpClient httpClient; @override Future createTodo(CreateTodoDto todo) => httpClient.createTodo(todo); @override Future deleteTodoById(TodoId id) => httpClient.deleteTodoById(id); @override Future> getAllTodo() => httpClient.getAllTodo(); @override Future getTodoById(TodoId id) => httpClient.getTodoById(id); @override Future updateTodo({required TodoId id, required UpdateTodoDto todo}) => httpClient.updateTodo(id, todo); } ``` Here, we are passing the `TodosHttpClient` as a dependency. This will help us to test the implementation by passing a mock implementation of `HttpClient` to the data source implementation. ### Handling errors as `NetworkFailure` We need to handle the errors that we send from the backend as failures, and other possible errors as well. One possible way to do this is, when we implement our `TodoRepository`, we will catch all the exceptions as `NetworkException`, and then resolve the exception to the `Failure` and handle the failures in the UI. The way we do this is can create an `interceptor`, that we can plug into our `dio` instance, and then handle the errors from there. By creating a new `NetworkException` class in our `exceptions` package, we will be able to handle network errors that may occur while communicating with the backend. However, when working with APIs, we will encounter errors that are returned in the form of `DioError`. To handle these errors, we will use an `ErrorInterceptorHandler` to continue the Dio request-response cycle, while also rejecting any errors that occur. To catch `NetworkException` in the repository, we will create a new class called `DioNetworkException` which extends `DioError` and implements `NetworkException`. This allows us to create a new instance of `DioNetworkException` and reject it from the handler. ### Create `NetworkException` In our `exceptions` package, we will create a new exception called `NetworkExeption`. We are doing this because we don't want any external dependencies on our packages, as much as we can. Let's create a new file `exceptions/lib/src/network_exception/network_exception.dart` in `exceptions` package with the following contents. ```dart class NetworkException implements Exception { NetworkException({ required this.message, required this.statusCode, this.errors = const {}, }); final String message; final int statusCode; final Map> errors; } ``` > Make sure to export this from `exceptions.dart` library. ```dart library exceptions; export 'src/http_exception/http_exception.dart'; export 'src/network_exception/network_exception.dart'; export 'src/server_exception/server_exception.dart'; ``` Once this is done, let's come back to `frontend` and create a new class called `DioNetworkException`. ### Create `DioNetworkException` Let's create a new file in `lib/core/network/exceptions/dio_network_exception.dart` in the `frontend` package. ```dart import 'package:dio/dio.dart'; import 'package:exceptions/exceptions.dart'; class DioNetworkException extends DioError implements NetworkException { DioNetworkException({ required this.message, required this.statusCode, required this.errors, required super.requestOptions, }); @override final int statusCode; @override final String message; @override final Map> errors; } ``` This exception is created to convert the `DioError` we get while implementing the error interceptor, to our `NetworkException` so that we only care about the `NetworkException` we implemented in our package, and catch it. ### Create `NetworkErrorInterceptor` We will now implement `NetworkErrorInterceptor` extending `Interceptor` from `dio`. Here we will have to override `onError` method, which will give us `DioError` object and a handler. > Read more [about interceptors](https://pub.dev/packages/dio#interceptors) from here. Create a new file `frontend/lib/core/network/interceptors/dio_error_interceptor.dart` ```dart import 'package:dio/dio.dart'; import 'package:failures/failures.dart'; import 'package:fullstack_todo/core/network/exceptions/dio_network_exception.dart'; class NetworkErrorInterceptor extends Interceptor { @override void onError(DioError err, ErrorInterceptorHandler handler) { const genericInternetIssue = 'Please check your internet connection and try again'; try { if (err.response == null) { throw DioNetworkException( message: genericInternetIssue, statusCode: 500, requestOptions: err.requestOptions, errors: {}, ); } final errorJson = err.response!.data as Map; final failureFromServer = NetworkFailure.fromJson( { ...errorJson, 'status_code': err.response!.statusCode, }, ); throw DioNetworkException( message: failureFromServer.message, statusCode: err.response!.statusCode ?? failureFromServer.statusCode, errors: failureFromServer.errors, requestOptions: err.requestOptions, ); } on DioNetworkException catch (e) { handler.reject(e); } catch (e) { handler.reject( DioNetworkException( message: genericInternetIssue, statusCode: 500, requestOptions: err.requestOptions, errors: {}, ), ); } } } ``` > Note: we may need to make a small change to our `NetworkFailure` class and run `build_runner` ```diff const factory NetworkFailure({ required String message, required int statusCode, - @Default([]) List errors, + @Default({}) Map> errors, }) = _NetworkFailure; ``` Now once, we have implemented the `NetworkErrorInterceptor`, we will attach this interceptor to our dio object. Open `third_party_modules.dart` and add the following line. ```diff abstract class ThirdPartyModules { @lazySingleton Dio get dio => Dio(BaseOptions(baseUrl: kBaseUrl)); + ..interceptors.add(NetworkErrorInterceptor()); } ``` ### Implement `TodoRepository` Now we will implement the todo repository at `lib/data/repositories/todo_repository_impl.dart` in the following way. ```dart import 'package:data_source/data_source.dart'; import 'package:either_dart/either.dart'; import 'package:exceptions/exceptions.dart'; import 'package:failures/failures.dart'; import 'package:injectable/injectable.dart'; import 'package:models/models.dart'; import 'package:repository/repository.dart'; import 'package:typedefs/typedefs.dart'; @LazySingleton(as: TodoRepository) class TodoRepositoryImpl implements TodoRepository { const TodoRepositoryImpl(this._todoDataSource); final TodoDataSource _todoDataSource; @override Future> createTodo(CreateTodoDto createTodoDto) => handleError(() => _todoDataSource.createTodo(createTodoDto)); @override Future> deleteTodo(TodoId id) => handleError(() => _todoDataSource.deleteTodoById(id)); @override Future> getTodoById(TodoId id) => handleError(() => _todoDataSource.getTodoById(id)); @override Future>> getTodos() => handleError(_todoDataSource.getAllTodo); @override Future> updateTodo({ required TodoId id, required UpdateTodoDto updateTodoDto, }) => handleError( () => _todoDataSource.updateTodo(id: id, todo: updateTodoDto), ); Future> handleError( Future Function() callback, ) async { try { final res = await callback(); return Right(res); } on NetworkException catch (e) { return Left( NetworkFailure( message: e.message, statusCode: e.statusCode, errors: e.errors, ), ); } } } ``` Here, we are passing the data source as a dependency and calling methods from the data source for each implementation. In addition to that, we have created a `handleError` generic method, which takes a caller method, and checks if there is a `NetworkException`. The `NetworkException` will be caught here which was thrown from the interceptor as `DioNetworkException`. Now, let's do some UI, and connect our view model to the repository. ## Creating `ShowTodosView` Next, we will move on to creating the user interface and retrieving data from our repositories. To do this, we will perform some initial setup. We will use [stacked](https://pub.dev/packages/stacked) to implement a Model-View-ViewModel (MVVM) architecture. The necessary dependencies have already been imported. Let's begin by creating the views and adding them to the routes. ### Create `ShowTodosView` and `ShowTodosViewModel` In `lib/presentation//show_todos` we will create two new files called `show_todos_view.dart` and `show_todos_viewmodel.dart`. We will begin by creating an empty implementation of the view and view model. In `show_todos_viewmodel.dart` ```dart import 'package:stacked/stacked.dart'; import 'package:injectable/injectable.dart'; @injectable class ShowTodosViewModel extends BaseViewModel {} ``` Similarly in `show_todos_view.dart`. ```dart import 'package:flutter/material.dart'; import 'package:fullstack_todo/core/di/locator.dart'; import 'package:fullstack_todo/presentation/show_todos/show_todos_viewmodel.dart'; import 'package:stacked/stacked.dart'; class ShowTodosView extends StatelessWidget { const ShowTodosView({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return ViewModelBuilder.nonReactive( viewModelBuilder: () => locator(), builder: ( BuildContext context, ShowTodosViewModel model, Widget? child, ) { return Scaffold( body: Center( child: Text( 'ShowTodosView', ), ), ); }, ); } } ``` We will also take advantage of the routing system provided by [`stacked`](https://pub.dev/packages/stacked). This system uses context-less routing, allowing us to easily navigate within our application from the view model. We will create a `NavigationService` that can be injected as a dependency, which will enable us to handle routing. > Read [more about `NavigationService`](https://pub.dev/packages/stacked_services#navigation-service) from here. Let's create a new `core/app/routes.dart` file with the following contents. ```dart import 'package:fullstack_todo/presentation/show_todos/show_todos_view.dart'; import 'package:stacked/stacked_annotations.dart'; @StackedApp( routes: routes, ) const List> routes = [ AdaptiveRoute(page: ShowTodosView, initial: true), ]; ``` Here, the `@StackedApp` decorator does the magic. Once this is done, we run `build_runner` to run code generation. We will get a new file `routes.router.dart` > Read more [about `@StackedApp`](https://pub.dev/packages/stacked#router) Now, in `main.dart` file, let's remove all the comments and make these changes. ```dart import 'package:flutter/material.dart'; import 'package:fullstack_todo/core/app/routes.router.dart'; import 'package:fullstack_todo/core/di/locator.dart'; import 'package:stacked_services/stacked_services.dart'; void main() { setupLocator(); runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( title: 'Fullstack Todo App', theme: ThemeData( primarySwatch: Colors.blue, ), navigatorKey: StackedService.navigatorKey, onGenerateRoute: StackedRouter().onGenerateRoute, ); } } ``` Before calling `runApp()`, we are invoking `setupLocator()` from `locator.dart`. This function will handle all the logic related to dependency injection. We are also providing `navigatorKey` and `onGenerateRoute` parameters, which come from `stacked` and `routes.router.dart`, respectively. We also need to take an extra step, which is to manage the injection of the `NavigationService`. For this, we need to add a getter to the `third_party_modules.dart` file. ```dart @module abstract class ThirdPartyModules { @lazySingleton Dio get dio => Dio(BaseOptions(baseUrl: kBaseUrl)) ..interceptors.add(NetworkErrorInterceptor()); @lazySingleton NavigationService get navigationService; } ``` Once this is done, if you build the app you should see the first visual output. ![](https://saileshdahal.com.np/images/posts/building-a-fullstack-app-with-dartfrog-and-flutter-in-a-monorepo-part-5/c86ca5ee-3d5c-4c22-881d-249da562025b.png) #### Implement `ShowTodosViewModel` This view model will use the `TodoRepository` to retrieve data from the backend. Similar to `initState` method used when working with `StatefulWidgets`, we can create a new method in our ViewModel called `init` to handle initialization logic. Within this method, we can call the repository to fetch all the todos. We will also create a `refresh` method to handle pull-to-refresh functionality. ```dart @injectable class ShowTodosViewModel extends BaseViewModel { final TodoRepository _todoRepository; ShowTodosViewModel(this._todoRepository); void handleFailure(Failure failure) { setError(failure.message); log(failure.message); } void init() => runBusyFuture(refresh()); Future refresh() async { final response = await _todoRepository.getTodos(); response.fold(handleFailure, print); } } ``` Here, we have three methods, `init` `refresh` and `handleFailure`. * `init` This method is ideally run on `initState` state. This will call the `refresh` method, but in addition to that, this will mark the ViewModel as `busy` before calling the method. This will be helpful when we want to show a loading indicator when we first fetch the todos. * `refresh` This method will call the `TodoRepository` and than handle re response accordingly. * `handleFailure` Ideally, this is the place where we would handle the failure and show the UI accordingly. The `setError` is storing the failure message in the view model, which will be accessible by `modelError` getter from `BaseViewModel`. Now we are getting the todos from the backend. We will also be marking todos as completed and deleting the todo from this same view model. > Where should we put the todos once we fetch them? We will also have another view called `MaintainTodoView` which will be responsible for creating and updating the todos. We will come across a problem, where we will have to update the state of the created and updated todo. We can do this by passing the todo down the routes once it is created or updated. Another way to do this is by creating a data service, which will be shared across different view models. This service will be reactive, meaning if the data changes, it will rebuild all the view models listening to this data. This will help us avoid passing down the problem of the route. #### Implement `TodosDataService` We will create a `TodosDataService` which will be shared across the view models. This will have some helper methods that will be used to maintain the state of all the todos. We will create a new file `lib/data_services/todos_data_service.dart` ```dart import 'package:flutter/material.dart'; import 'package:injectable/injectable.dart'; import 'package:models/models.dart'; import 'package:stacked/stacked.dart'; @lazySingleton class TodosDataService with ReactiveServiceMixin { TodosDataService() { listenToReactiveValues([_todos]); } late final ReactiveValue> _todos = ReactiveValue>([]); List get todos => _todos.value; void add(Todo todo) { final index = _todos.value.indexWhere((element) => element.id == todo.id); if (index == -1) { _todos.value.insert(0, todo); } else { _todos.value[index] = todo; } notifyListeners(); } void addAll(List todos) { _todos.value ..clear() ..insertAll(0, todos); notifyListeners(); } void remove(Todo todo) { _todos.value.removeWhere((element) => element.id == todo.id); notifyListeners(); } } ``` Here, we are using `ReactiveServiceMixin` and `ReactiveValue` objects. When we call `add` method, it checks if the todo is present already, if so, it will update the existing one, or else it will add it to the top, so that we can see the latest one at the top. Similarly, there is `addAll` which will clear all todos and then add them to the top. > Read more about [reactivity in stacked](https://pub.dev/packages/stacked#reactivity) from here. #### Updating `ShowTodosViewModel` Now we will inject `TodosDataService` from the constructor and make some changes to the view model to handle reactivity. ```dart @injectable class ShowTodosViewModel extends ReactiveViewModel { ShowTodosViewModel( this._todoRepository, this._todosDataService, ); final TodoRepository _todoRepository; final TodosDataService _todosDataService; List get todos => _todosDataService.todos; Future init() => runBusyFuture(refresh()); void handleFailure(Failure failure) { setError(failure.message); log(failure.message); } Future refresh() async { final response = await _todoRepository.getTodos(); response.fold(handleFailure, _todosDataService.addAll); } @override List get reactiveServices => [_todosDataService]; } ``` If you noticed, we are extending from [`ReactiveViewModel`](https://pub.dev/packages/stacked#reactiveviewmodel) and also overriding `reactiveServices`. We are listening to the `_todosDataService` and will be reflecting the UI whenever the data is changed in the reactive service. #### Update `ShowTodosView` to show fetched todos Now we can update the `show_todos_view.dart` to show the fetched todos. ```dart class ShowTodosView extends StatelessWidget { const ShowTodosView({super.key}); @override Widget build(BuildContext context) { return ViewModelBuilder.reactive( viewModelBuilder: locator, onModelReady: (model) => model.init(), builder: ( BuildContext context, ShowTodosViewModel model, Widget? child, ) { return Scaffold( appBar: AppBar( title: const Text('Todos'), ), floatingActionButton: FloatingActionButton( onPressed: () {}, child: const Icon(Icons.add), ), body: Builder( builder: (context) { if (model.isBusy) { return const Center(child: CircularProgressIndicator()); } if (model.hasError) { return Center(child: Text(model.modelError.toString())); } if (model.todos.isEmpty) { return Center( child: Text( 'No todos found!', style: Theme.of(context).textTheme.titleLarge, ), ); } return RefreshIndicator( onRefresh: model.refresh, child: ListView.builder( physics: const AlwaysScrollableScrollPhysics(), itemCount: model.todos.length, itemBuilder: (context, index) { final todo = model.todos[index]; return ListTile( title: Text(todo.title), subtitle: Text(todo.description), leading: Checkbox( value: todo.completed, onChanged: (val) {}, ), trailing: Row( mainAxisSize: MainAxisSize.min, children: [ IconButton( icon: const Icon( Icons.edit, color: Colors.blue, ), onPressed: () {}, ), IconButton( icon: const Icon( Icons.delete, color: Colors.red, ), onPressed: () {}, ), ], ), ); }, ), ); }, ), ); }, ); } } ``` This view will get all the lists from the view model with `init` method. > Notice the `onModelReady` in the `ViewModelBuilder` where we are calling `init` method from the view model. We are using the `Builder` to check the different states of the view model and show the UI accordingly. We show a `CircularProgressIndicator` when the view model is `isBusy`. This value is set by `runBusyFuture` method in the view model. Also if the model has an error, we can check `hasError` and show the error message accordingly. We will also show the todos with a `ListView.builder`, when there are todos, and an empty message as well. Now let's build the app, and check out the progress. > Tip: If you are using macOS, make sure to allow [network access in the entitlements file](https://stackoverflow.com/a/61201109/9611399). ![](https://saileshdahal.com.np/images/posts/building-a-fullstack-app-with-dartfrog-and-flutter-in-a-monorepo-part-5/53546965-2fb3-42cf-859c-718611e45739.png) And, if you create one using postman, you should see something like this. [▶ Watch on YouTube](https://youtube.com/shorts/hcz457pUcZ0?feature=share) #### Create `TodoListTile` Once this is done, let's do some refactoring. We can create a `TodoListTile` widget as a separate stateless widget in `show_todos/widgets/todo_list_tile.dart` ```dart class TodoListTile extends ViewModelWidget { const TodoListTile({ super.key, required this.todo, }); final Todo todo; @override Widget build(BuildContext context, ShowTodosViewModel viewModel) { return ListTile( title: Text(todo.title), subtitle: Text(todo.description), leading: Checkbox( value: todo.completed, onChanged: (val) => viewModel.markCompleted(todo), ), trailing: Row( mainAxisSize: MainAxisSize.min, children: [ IconButton( icon: const Icon( Icons.edit, color: Colors.blue, ), onPressed: () => viewModel.handleTodo(todo), ), IconButton( icon: const Icon( Icons.delete, color: Colors.red, ), onPressed: () => viewModel.deleteTodo(todo), ), ], ), ); } } ``` Here, we have missing view model methods, which we will implement later. Also, we are not using a `StatelessWidget` here, but it's a `ViewModelWidget`. This is a wrapper around `StatelessWidget` which will also provide us with the view model. This works with `Provider` under the hood. Let's use this `TodoListTile` widget in `ShowTodosView`. We can update the `ListView.builder` as follows. ```dart ListView.builder( physics: const AlwaysScrollableScrollPhysics(), itemCount: model.todos.length, itemBuilder: (context, index) => TodoListTile(todo: model.todos[index]), ), ``` #### Implement `delete` and `markCompleted` methods. We will now implement `deleteTodo` and `markCompleted` methods as follows. ```dart Future deleteTodo(Todo todo) async { final response = await _todoRepository.deleteTodo(todo.id); response.fold( handleFailure, (_) => _todosDataService.remove(todo), ); } ``` Here, we are calling the `deleteTodo` method from the repository, and if it's successful, we will remove the todo from the data service, that in turn will remove it from the UI. ```dart Future markCompleted(Todo todo) async { if (busy('updating')) return; final completed = !todo.completed; _todosDataService.add(todo.copyWith(completed: completed)); final updateDto = UpdateTodoDto( completed: !todo.completed, ); final update = await runBusyFuture( _todoRepository.updateTodo(id: todo.id, updateTodoDto: updateDto), busyObject: 'updating', ); update.fold( (failure) { _todosDataService.add(todo.copyWith(completed: !completed)); handleFailure(failure); }, (_) {}, ); } ``` Here, in `markCompleted` method, we are first negating the state, and then adding it to the data service, and then calling the API. This is to show the update in an instantaneous manner. If the update fails, we will revert the checkbox state. We will add an empty implementation for the `handleTodo` method as follows. ```dart Future? handleTodo([Todo? todo]) { return null; } ``` This method will take an optional Todo object, and we will pass it to `MaintainTodoView`, which will create or update the todo based on the value provided. Once these methods are completed, we will have these functionalities. [▶ Watch on YouTube](https://www.youtube.com/shorts/sY-5NN8GC8I?feature=share) ## Creating `MaintainTodoView` This view will be able to create and update the todos. We will create the view and the view model. Also, we will create a route for this, and implement `handleTodo` method in `ShowTodosViewModel`. We will create `maintain_todo_view.dart` and `maintain_todo_viewmomdel.dart` in `presentstion/main_todo` directory. We will create the view model as follows. ```dart @injectable class MaintainTodoViewModel extends ReactiveViewModel { MaintainTodoViewModel(this._todosDataService); final TodosDataService _todosDataService; Todo? _todo; void init(Todo? todo) { if (todo == null) return; _todo = todo; } @override List get reactiveServices => [_todosDataService]; } ``` Similarly, we will create an empty view, as follows ```dart class MaintainTodoView extends StatelessWidget { const MaintainTodoView(this.todo, {super.key}); final Todo? todo; @override Widget build(BuildContext context) { return ViewModelBuilder.nonReactive( onModelReady: (model) => model.init(todo), viewModelBuilder: () => locator(), builder: ( BuildContext context, MaintainTodoViewModel model, Widget? child, ) { return Scaffold( body: Center( child: Text( 'MaintainTodoView', ), ), ); }, ); } } ``` Now, we can add this view to the route, and handle routing from `ShowTodosViewModel`. In `lib/core/app/routes.dart` add the following route entry: ```dart const List> routes = [ AdaptiveRoute(page: ShowTodosView, initial: true), AdaptiveRoute(page: MaintainTodoView), ]; ``` Once you run the `build_runner`, open the `ShowTodosViewModel`, and make the following change. We will have to register `NavigationService` in our third-party modules as follows. ```dart @module abstract class ThirdPartyModules { @lazySingleton Dio get dio => Dio(BaseOptions(baseUrl: kBaseUrl)); @lazySingleton NavigationService get navigationService; } ``` Now, we will inject the `NavigationService` from the `ShowTodosViewModel` constructor and implement the `handleTodo` as follows. ```diff ShowTodosViewModel( this._todoRepository, this._todosDataService, + this._navigationService, ); final TodoRepository _todoRepository; final TodosDataService _todosDataService; + final NavigationService _navigationService; Future? handleTodo([Todo? todo]) { + return _navigationService.navigateTo( + Routes.maintainTodoView, + arguments: MaintainTodoViewArguments(todo: todo), + ); } ``` > Make sure to run `build_runner`. Now, you should be able to navigate to `MaintainTodoView`. [▶ Watch on YouTube](https://www.youtube.com/shorts/Ua27jp70j2s?feature=share) ### Implementing `MaintainTodoViewModel` We will create three state variables for handling `title`, `description`, and `completed` value. We will also expose the getters for these variables. Also, we will create `onChanged` methods for these variables. We will also create an `init` method, which will take an optional `Todo` object and populate these state variables. Here if we pass this optional todo, we will be updating that todo. ```dart @injectable class MaintainTodoViewModel extends BaseViewModel { MaintainTodoViewModel( this._todosDataService, this._repository, this._navigationService, ); final TodosDataService _todosDataService; final TodoRepository _repository; final NavigationService _navigationService; String _title = ''; String get title => _title; String _description = ''; String get description => _description; bool _completed = false; bool get completed => _completed; bool get isValidated { final empty = _title.isEmpty || _description.isEmpty; if (empty) return false; final errors = error('title') != null || error('description') != null; if (errors) return false; return true; } void onTitleChanged(String value) { _title = value; if (value.isEmpty) { setErrorForObject('title', 'Title is required'); } else { setErrorForObject('title', null); } notifyListeners(); } void onDescriptionChanged(String value) { _description = value; if (value.isEmpty) { setErrorForObject('description', 'Description is required'); } else { setErrorForObject('description', null); } notifyListeners(); } Todo? _todo; void init(Todo? todo) { if (todo == null) return; _title = todo.title; _description = todo.description; _todo = todo; _completed = todo.completed; } void onCompletedChanged({bool? value}) { _completed = value ?? false; notifyListeners(); } void handleTodo() {} } ``` In change handlers, we are also validating the values, and setting the errors for each state on change, and then we have a `isValidated` getter which would return if the values are validated or not. ### Implementing `MaintainTodoView` ```dart class MaintainTodoView extends HookWidget { const MaintainTodoView(this.todo, {super.key}); final Todo? todo; @override Widget build(BuildContext context) { final titleController = useTextEditingController(text: todo?.title); final descriptionController = useTextEditingController(text: todo?.description); final titleFocusNode = useFocusNode(); final descriptionFocusNode = useFocusNode(); final checkBoxFocusNode = useFocusNode(); final buttonFocusNode = useFocusNode(); return ViewModelBuilder.nonReactive( onModelReady: (model) => model.init(todo), viewModelBuilder: locator, builder: ( BuildContext context, MaintainTodoViewModel model, Widget? child, ) { return Scaffold( appBar: AppBar( title: Text('${todo == null ? 'Create' : 'Edit'} Todo'), ), body: Padding( padding: const EdgeInsets.all(16), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ TextFormField( controller: titleController, focusNode: titleFocusNode, decoration: const InputDecoration( hintText: 'Title', border: OutlineInputBorder(), ), onChanged: model.onTitleChanged, onEditingComplete: descriptionFocusNode.requestFocus, ), const SizedBox(height: 14), TextFormField( controller: descriptionController, focusNode: descriptionFocusNode, decoration: const InputDecoration( hintText: 'Description', border: OutlineInputBorder(), ), onChanged: model.onDescriptionChanged, onEditingComplete: checkBoxFocusNode.requestFocus, ), if (todo != null) const SizedBox(height: 14), if (todo != null) SelectorViewModelBuilder( selector: (model) => model.completed, builder: (context, val, _) { return CheckboxListTile( focusNode: checkBoxFocusNode, contentPadding: EdgeInsets.zero, title: const Text('Mark Completed'), value: val, onChanged: (v) => model.onCompletedChanged(value: v), ); }, ), const Spacer(), SelectorViewModelBuilder( selector: (model) => model.isValidated, builder: (context, validated, child) { if (validated) return child!; return const SizedBox.shrink(); }, child: SizedBox( height: 50, child: ElevatedButton( focusNode: buttonFocusNode, onPressed: model.handleTodo, child: SelectorViewModelBuilder( selector: (model) => model.isBusy, builder: (context, isBusy, child) { if (isBusy) return child!; return Row( mainAxisSize: MainAxisSize.min, children: const [ Icon( Icons.save, size: 30, ), SizedBox(width: 10), Text( 'Save', style: TextStyle( fontSize: 20, ), ), ], ); }, child: const CircularProgressIndicator( valueColor: AlwaysStoppedAnimation(Colors.white), ), ), ), ), ) ], ), ), ); }, ); } } ``` This form includes text fields for the `title` and `description` of the to-do task. The optional check box is displayed only when an existing to-do task is provided for updating. If a to-do task is provided, the form will be populated with the previous values and the check box will be visible for updating. To handle the form's input, we use the `flutter_hooks` library for managing `TextEditingControllers` and `FocusNodes`. The `ElevatedButton` takes care of the create/update action. We are also using `SelectorViewModelBuilder` with a `selector` to rebuild only when the value returned from the selector is changed, resulting in partial rebuilds. For example, we are only showing the `ElevatedButton` when the form is validated. > Read [more about `SelectorViewModelBuilder`](https://pub.dev/packages/stacked#selectorviewmodelbuilder) With this, we have a functional form UI something like this. [▶ Watch on YouTube](https://www.youtube.com/shorts/SOUH1t2RdbY?feature=share) In the final part, let's implement `handleTodo` method in `MaintainTodoViewmodel` like this. ```dart Future handleTodo() { if (!isValidated) return Future.value(); if (_todo == null) { return _createTodo(); } return _updateTodo(); } Future _createTodo() async { final dto = CreateTodoDto(title: title, description: description); final response = await runBusyFuture(_repository.createTodo(dto)); return response.fold( (failure) { setError(failure.message); }, (todo) { _todosDataService.add(todo); _navigationService.back(); }, ); } Future _updateTodo() async { if (_todo == null) return; final dto = UpdateTodoDto( title: title, description: description, completed: completed, ); final response = await runBusyFuture( _repository.updateTodo(id: _todo!.id, updateTodoDto: dto), ); return response.fold( (failure) { setError(failure.message); }, (todo) { _todosDataService.add(todo); _navigationService.back(); }, ); } ``` The `handleTodo` method is responsible for creating or updating a to-do task based on the optional `Todo` provided in the `init` method. It calls the private methods `_createTodo` and `_updateTodo` internally. `_createTodo` creates a data transfer object, passes it to the repository, and navigates back upon successful creation. Similarly, `_updateTodo` updates the provided to-do task and navigates back upon successful update. Finally, we have a fully functional app. [▶ Watch on YouTube](https://www.youtube.com/shorts/oy4_PN1cbyI?feature=share) --- ## Bonus To keep our code consistent across the project, let's add some linting with [very\_good\_analysis](https://pub.dev/packages/very_good_analysis) in `analysis_options.yaml`. The package is stricter than the default lint set, so expect a wall of lints on the first run. Fix them once, and every diff after that stays consistent. ```yaml include: package:very_good_analysis/analysis_options.yaml analyzer: exclude: - '**/*.router.dart' - '**/*.locator.dart' - '**/*.config.dart' - '**/*.g.dart' linter: rules: public_member_api_docs: false ``` --- We've built the front end of our full-stack to-do application using Flutter: dependencies, connecting with the backend, creating and updating todos, error handling, and a reactive data service. Testing isn't covered yet in this series. If you want to see it across the modules we've built, let us know in the comments. The repo lives at [https://github.com/saileshbro/full\_stack\_todo\_dart](https://github.com/saileshbro/full_stack_todo_dart); if you get stuck, open an issue or send a pull request. ============================================================================== # Dart Frog and Flutter with PostgreSQL: build CRUD routes (Part 4) URL: https://saileshdahal.com.np/building-a-fullstack-app-with-dartfrog-and-flutter-in-a-monorepo-part-4 Published: 2023-01-02 Tags: Flutter, Dart, Programming Blogs, General Programming Series: Building a Fullstack App with dart_frog and Flutter in a Monorepo (part 4 of 6) ============================================================================== In the previous part, we set up models, data sources, repositories, exceptions and failures for the full-stack to-do application. We also made some changes to our packages. In this part, we will: * Connect to a [Postgres](https://www.postgresql.org/) database * Complete the backend routes * Add a new controller to handle HTTP requests * Add necessary failures and exceptions * Fully implement CRUD operations for the to-do application ## Implementing the Backend ### Importing necessary dependencies We will import all the necessary dependencies in the `pubspec.yaml` file. ```yaml dependencies: dart_frog: ^0.3.0 data_source: path: ../data_source dotenv: ^4.0.1 either_dart: ^0.3.0 exceptions: path: ../exceptions failures: path: ../failures http: ^0.13.5 models: path: ../models postgres: ^2.5.2 repository: path: ../repository typedefs: path: ../typedefs ``` ### Setting up the Postgres database We will be using the [PostgreSQL](https://www.postgresql.org/) database for this tutorial. To use the [PostgreSQL](https://www.postgresql.org/) database for this tutorial, we can set up a test database on [elephantsql.com](http://elephantsql.com). Simply sign up on the website and click on the option to create a new instance. You should see something similar to this. ![Elephant SQL New DB](https://saileshdahal.com.np/images/posts/building-a-fullstack-app-with-dartfrog-and-flutter-in-a-monorepo-part-4/aed8bde3-4e4f-4be1-8870-249f1ccacf4f.png) Once you add a name, you will be prompted to choose a name for your instance and a region that is nearest to you. I have selected the `AP-East-1` region, but you can choose any region that you prefer. ![Elephant SQL Select Region](https://saileshdahal.com.np/images/posts/building-a-fullstack-app-with-dartfrog-and-flutter-in-a-monorepo-part-4/4a5d9897-4fad-44a7-8aba-97aac2734568.png) Once you have chosen a name and selected a region, click the `Create Instance` button. This will redirect you to a dashboard where you can click on the instance name to access the credentials for the database you have just created. The credentials should look similar to this. ![Elephant SQL Creds](https://saileshdahal.com.np/images/posts/building-a-fullstack-app-with-dartfrog-and-flutter-in-a-monorepo-part-4/aba373d5-7eb6-417b-872e-70e3c7a53b70.png) ### Connecting to the Database #### Setting up environment Now that we have created a database, we can connect to it from our application. To do this, we will create a new file `.env` at the root of the `backend` directory. This file will contain the credentials for the database that we have just created. The `.env` file should look similar to this. Once this is done, we will use the [dotenv](https://pub.dev/packages/dotenv) package to load the environment variables from the `.env` file. We will also use the [postgres](https://pub.dev/packages/postgres) package to connect to the database. You can run the following command in the backend directory to add the necessary dependencies. ```bash dart pub add dotenv postgres ``` This is how `.env` file should look. Make sure to use your database credentials ```apache DB_HOST=tiny.db.elephantsql.com DB_PORT=5432 DB_DATABASE=asztgqfq DB_USERNAME=asztgqfq DB_PASSWORD=PcIXbvXQLwpEON61GVPzqs0zHyzHyHZc ``` #### Creating database connection Now, create a `backend/lib/db/database_connection.dart` file and add the following code. ```dart import 'dart:developer'; import 'package:dotenv/dotenv.dart'; import 'package:postgres/postgres.dart'; class DatabaseConnection { DatabaseConnection(this._dotEnv) { _host = _dotEnv['DB_HOST'] ?? 'localhost'; _port = int.tryParse(_dotEnv['DB_PORT'] ?? '') ?? 5432; _database = _dotEnv['DB_DATABASE'] ?? 'test'; _username = _dotEnv['DB_USERNAME'] ?? 'test'; _password = _dotEnv['DB_PASSWORD'] ?? 'test'; } final DotEnv _dotEnv; late final String _host; late final int _port; late final String _database; late final String _username; late final String _password; PostgreSQLConnection? _connection; PostgreSQLConnection get db => _connection ??= throw Exception('Database connection not initialized'); Future connect() async { try { _connection = PostgreSQLConnection( _host, _port, _database, username: _username, password: _password, ); log('Database connection successful'); return _connection!.open(); } catch (e) { log('Database connection failed: $e'); } } Future close() => _connection!.close(); } ``` Whenever we want to query, we will open a connection to the database and close it once we are done. This will ensure that we are not keeping the connection open for too long. #### Injecting `DatabaseConnection` through `provider` Create a new file `routes/_middleware.dart` and add the following code. ```dart import 'package:backend/db/database_connection.dart'; import 'package:dart_frog/dart_frog.dart'; import 'package:dotenv/dotenv.dart'; final env = DotEnv()..load(); final _db = DatabaseConnection(env); Handler middleware(Handler handler) { return handler.use(provider((_) => _db)); } ``` This middleware is used to provide the `DatabaseConnection` instance to other parts of the application through a `provider`. #### Fetching `DatabaseConnection` from `provider` Now in `routes/index.dart` you can get this `DatabaseConnection` from `RequestContext` and use it to query the database. ```dart import 'package:backend/db/database_connection.dart'; import 'package:dart_frog/dart_frog.dart'; Future onRequest(RequestContext context) async { final connection = context.read(); await connection.connect(); final response = await connection.db.query('select * from information_schema.tables'); await connection.close(); return Response.json(body: response.map((e) => e.toColumnMap()).toList()); } ``` If you run `dart_frog dev`, then you should be able to open `http://localhost:8080` and see the following output. ![Database Connection Successful](https://saileshdahal.com.np/images/posts/building-a-fullstack-app-with-dartfrog-and-flutter-in-a-monorepo-part-4/52a4d34e-c9b0-4237-bf5c-c399941321f9.png) We have successfully connected to the database. #### Create Database Table Before implementing the `TodoDataSource`, we will need to create the table in the database. To do this, open the [elephantsql.com](elephantsql.com) dashboard and click on the `BROWSER` tab. ![Elephant SQL Query Execution](https://saileshdahal.com.np/images/posts/building-a-fullstack-app-with-dartfrog-and-flutter-in-a-monorepo-part-4/334dd3bf-3d31-49fd-abc2-d2235d7865b4.png) Then, execute the following query: ```sql CREATE TABLE todos( id SERIAL PRIMARY KEY NOT NULL, title VARCHAR(255) NOT NULL, description TEXT NOT NULL, completed BOOL DEFAULT FALSE, created_at timestamp default current_timestamp NOT NULL, updated_at timestamp null ); ``` This [PostgreSQL query](https://www.tutorialspoint.com/postgresql/postgresql_create_table.htm) creates a new table called todos with the following columns: * `id`: an integer column that is the table's primary key and is generated automatically by the database (using the [`SERIAL`](https://www.educba.com/postgresql-serial/) type). The [`NOT NULL`](https://www.tutorialspoint.com/postgresql/postgresql_constraints.htm) constraint ensures that this column cannot contain a `NULL` value. * `title`: a string column with a maximum length of 255 characters. The [`NOT NULL`](https://www.tutorialspoint.com/postgresql/postgresql_constraints.htm) constraint ensures that this column cannot contain a `NULL` value. * `description`: a text column. The `NOT NULL` constraint ensures that this column cannot contain a `NULL` value. * `completed`: a boolean column with a default value of `FALSE`. * `created_at`: a [`timestamp`](https://www.educba.com/postgresql-timestamp/) column with a default value of the current timestamp. The `NOT NULL` constraint ensures that this column cannot contain a `NULL` value. * `updated_at`: a [`timestamp`](https://www.educba.com/postgresql-timestamp/) column that can contain a `NULL` value. The `todos` table will be used to store the to-do items in our application. Each row in the table represents a single to-do item, and the table's columns store the data for that to-do item. Once you run the query, you should see a toast message at the top. ![Elephant SQL Query Success](https://saileshdahal.com.np/images/posts/building-a-fullstack-app-with-dartfrog-and-flutter-in-a-monorepo-part-4/08dd1b1d-1c5b-4f4d-bd8b-7562dc7ffc3f.png) ### Implementing `TodoDataSource` We will implement the todo data source in `backend/lib/todo/data_source/todo_data_source_impl.dart`. This file will contain the implementation of the `TodoDataSource` interface. We will pass a `DatabaseConnection` as a dependency to this class. Create `TodoDataSourceImpl` and implement `TodoDataSource` interface, and override necessary methods. The empty implementation should look like this. #### Empty `TodoDataSource` implementation ```dart import 'package:backend/db/database_connection.dart'; import 'package:data_source/data_source.dart'; import 'package:models/models.dart'; import 'package:typedefs/typedefs.dart'; class TodoDataSourceImpl implements TodoDataSource { const TodoDataSourceImpl(this._databaseConnection); final DatabaseConnection _databaseConnection; @override Future createTodo(CreateTodoDto todo) { throw UnimplementedError(); } @override Future deleteTodoById(TodoId id) { throw UnimplementedError(); } @override Future> getAllTodo() { throw UnimplementedError(); } @override Future getTodoById(TodoId id) { throw UnimplementedError(); } @override Future updateTodo({required TodoId id, required UpdateTodoDto todo}) { throw UnimplementedError(); } } ``` #### Injecting `TodoDataSource` through `provider` > Let's add this to our global middleware in `routes/_middleware.dart` file ```dart import 'package:backend/db/database_connection.dart'; import 'package:backend/todo/data_source/todo_data_source_impl.dart'; import 'package:dart_frog/dart_frog.dart'; import 'package:dotenv/dotenv.dart'; final env = DotEnv()..load(); final _db = DatabaseConnection(env); final _ds = TodoDataSourceImpl(_db); Handler middleware(Handler handler) { return handler .use(requestLogger()) .use(provider((_) => _db)) .use(provider((_) => _ds)); } ``` #### `createTodo` implementation Now we will implement the `createTodo` method. ```dart @override Future createTodo(CreateTodoDto todo) async { try { await _databaseConnection.connect(); final result = await _databaseConnection.db.query( ''' INSERT INTO todos (title, description, completed, created_at) VALUES (@title, @description, @completed, @created_at) RETURNING * ''', substitutionValues: { 'title': todo.title, 'description': todo.description, 'completed': false, 'created_at': DateTime.now(), }, ); if (result.affectedRowCount == 0) { throw const ServerException('Failed to create todo'); } final todoMap = result.first.toColumnMap(); return Todo( id: todoMap['id'] as int, title: todoMap['title'] as String, description: todoMap['description'] as String, createdAt: todoMap['created_at'] as DateTime, ); } on PostgreSQLException catch (e) { throw ServerException(e.message ?? 'Unexpected error'); } finally { await _databaseConnection.close(); } } ``` First, the method establishes a connection to the database using the `_databaseConnection` object. Then, it uses the query method on the `db` object to execute an `INSERT` statement The `substitutionValues` parameter is used to bind the values from the `CreateTodoDto` . If the `INSERT` statement is successful, the method retrieves the inserted row from the database using the `RETURNING *` clause and converts it to a map using the `toColumnMap` method. The method then uses this map to create and return a new `Todo` object. #### `getAllTodo` implementation Now we will implement the `getAllTodo` method. ```dart @override Future> getAllTodo() async { try { await _databaseConnection.connect(); final result = await _databaseConnection.db.query( 'SELECT * FROM todos', ); final data = result.map((e) => e.toColumnMap()).map(Todo.fromJson).toList(); return data; } on PostgreSQLException catch (e) { throw ServerException(e.message ?? 'Unexpected error'); } finally { await _databaseConnection.close(); } } ``` This `getAllTodo` method is used to retrieve a list of all the to-do items stored in the database. This executes a `SELECT` query to retrieve all rows from the `todos` table, maps each row to a `Todo` object using the `Todo.fromJson` function, and returns the list of `Todo` objects. #### `getTodoById` implementation Now we will implement the `getTodoById` method. ```dart @override Future getTodoById(TodoId id) async { try { await _databaseConnection.connect(); final result = await _databaseConnection.db.query( 'SELECT * FROM todos WHERE id = @id', substitutionValues: {'id': id}, ); if (result.isEmpty) { throw const NotFoundException('Todo not found'); } return Todo.fromJson(result.first.toColumnMap()); } on PostgreSQLException catch (e) { throw ServerException(e.message ?? 'Unexpected error'); } finally { await _databaseConnection.close(); } } ``` We execute a `SELECT` query that selects from `todos` table where the id equals the provided id. If the query returns an empty result set, we throw a `NotFoundException`, indicating that the requested to-do item could not be found, else it returns the mapped todo object. #### `updateTodo` implementation Here is the implementation of the `updateTodo` method. ```dart @override Future updateTodo({ required TodoId id, required UpdateTodoDto todo, }) async { try { await _databaseConnection.connect(); final result = await _databaseConnection.db.query( ''' UPDATE todos SET title = COALESCE(@new_title, title), description = COALESCE(@new_description, description), completed = COALESCE(@new_completed, completed), updated_at = current_timestamp WHERE id = @id RETURNING * ''', substitutionValues: { 'id': id, 'new_title': todo.title, 'new_description': todo.description, 'new_completed': todo.completed, }, ); if (result.isEmpty) { throw const NotFoundException('Todo not found'); } return Todo.fromJson(result.first.toColumnMap()); } on PostgreSQLException catch (e) { throw ServerException(e.message ?? 'Unexpected error'); } finally { await _databaseConnection.close(); } } ``` It executes an `UPDATE` query on the todos table. If no value is provided for a column, then the `COALESCE` function is used to keep the existing value in the database unchanged. The `updated_at` column is set to the `current_timestamp`. If the result set is empty, it means that no row with the given id was found, so a `NotFoundException` is thrown. #### `deleteTodoById` implementation Now, we will implement the `deleteTodoById` method. ```dart @override Future deleteTodoById(TodoId id) async { try { await _databaseConnection.connect(); await _databaseConnection.db.query( ''' DELETE FROM todos WHERE id = @id ''', substitutionValues: {'id': id}, ); } on PostgreSQLException catch (e) { throw ServerException(e.message ?? 'Unexpected error'); } finally { await _databaseConnection.close(); } } ``` If the delete statement is successful, the method does not return anything. #### `PostgresSQLException` handling In all of the methods above, if there is an exception while querying, like `PostgresSQLException`, it is caught and a `ServerException` is thrown with a more general error message. Finally, the database connection is closed before the method finishes executing. ### Implementing `TodoRepository` We will implement the todo repository in `backend/lib/todo/repositories/todo_repository_impl.dart`. This file will contain the implementation of the `TodoRepository` interface. We will pass a `TodoDataSource` as a dependency to this class. #### Empty `TodoRepository` implementation Create `TodoRepositoryImpl` and implement `TodoRepository` interface, and override necessary methods. The empty implementation should look like this. ```dart import 'package:data_source/data_source.dart'; import 'package:either_dart/either.dart'; import 'package:failures/failures.dart'; import 'package:models/models.dart'; import 'package:repository/repository.dart'; import 'package:typedefs/src/typedefs.dart'; class TodoRepositoryImpl implements TodoRepository { TodoRepositoryImpl(this.dataSource); final TodoDataSource dataSource; @override Future> createTodo(CreateTodoDto createTodoDto) { throw UnimplementedError(); } @override Future> deleteTodo(TodoId id) { throw UnimplementedError(); } @override Future> getTodoById(TodoId id) { throw UnimplementedError(); } @override Future>> getTodos() { throw UnimplementedError(); } @override Future> updateTodo({ required TodoId id, required UpdateTodoDto updateTodoDto, }) { throw UnimplementedError(); } } ``` #### Injecting `TodoRepository` through `provider` Before implementing the methods of `TodoRepository`, we will add it to our global middleware so that we can access it from our routes. ```dart import 'package:backend/db/database_connection.dart'; import 'package:backend/todo/data_source/todo_data_source_impl.dart'; import 'package:backend/todo/repositories/todo_repository_impl.dart'; import 'package:dart_frog/dart_frog.dart'; import 'package:data_source/data_source.dart'; import 'package:dotenv/dotenv.dart'; import 'package:repository/repository.dart'; final env = DotEnv()..load(); final _db = DatabaseConnection(env); final _ds = TodoDataSourceImpl(_db); final _repo = TodoRepositoryImpl(_ds); Handler middleware(Handler handler) { return handler .use(requestLogger()) .use(provider((_) => _db)) .use(provider((_) => _ds)) .use(provider((_) => _repo)); } ``` #### `createTodo` implementation Now we will implement the `createTodo` method. ```dart @override Future> createTodo(CreateTodoDto createTodoDto) async { try { final todo = await dataSource.createTodo(createTodoDto); return Right(todo); } on ServerException catch (e) { log(e.message); return Left( ServerFailure(message: e.message), ); } } ``` In this code, we are implementing the `createTodo` method which is part of the `TodoRepository` interface using `dataSource.createTodo` method. The `dataSource.createTodo` method is responsible for inserting the todo into the database. If the insertion is successful, it returns the todo object. #### `getTodoById` implementation Now we will implement the `getTodoById` method. ```dart @override Future> getTodoById(TodoId id) async { try { final res = await dataSource.getTodoById(id); return Right(res); } on NotFoundException catch (e) { log(e.message); return Left( ServerFailure( message: e.message, statusCode: e.statusCode, ), ); } on ServerException catch (e) { log(e.message); return Left( ServerFailure(message: e.message), ); } } ``` The method calls the `dataSource.getTodoById` method, which is responsible for querying the database and returning the todo object. If the todo is found, it returns the value. A `NotFoundException` is thrown when the todo with the given id is not found in the database. #### `getTodos` implementation Here, we will implement the `getTodos` method. ```dart @override Future>> getTodos() async { try { return Right(await dataSource.getAllTodo()); } on ServerException catch (e) { log(e.message); return Left( ServerFailure(message: e.message), ); } } ``` We call the `dataSource.getAllTodo` method. If the method execution is successful, we return the list of todo items. #### `updateTodo` implementation Now we will implement the `updateTodo` method. ```dart @override Future> updateTodo({ required TodoId id, required UpdateTodoDto updateTodoDto, }) async { try { return Right( await dataSource.updateTodo( id: id, todo: updateTodoDto, ), ); } on NotFoundException catch (e) { log(e.message); return Left( ServerFailure( message: e.message, statusCode: e.statusCode, ), ); } on ServerException catch (e) { log(e.message); return Left( ServerFailure(message: e.message), ); } } ``` The method updates the todo using the `dataSource.updateTodo` method. If the update is successful, it returns the updated todo. If the update fails and a `NotFoundException` is thrown, it logs the error message and returns a `ServerFailure` object with the appropriate status code. #### `deleteTodo` implementation Finally, we will implement the `deleteTodo` method. ```dart @override Future> deleteTodo(TodoId id) async { try { final exists = await getTodoById(id); if (exists.isLeft) return exists; final todo = await dataSource.deleteTodoById(id); return Right(todo); } on ServerException catch (e) { log(e.message); return Left( ServerFailure(message: e.message), ); } } ``` We check if a todo exists by calling the `this.getTodoById` method. If it does not exist, we return a `Failure`. If the todo does exist, we delete it by calling the `dataSource.deleteTodoById`. #### Handling `Exception` The `dataSource` methods throw exceptions like `ServerException` and `NotFoundException`. We catch these exceptions and log the error message. We then return a `Left` object containing a `ServerFailure` object. The `ServerFailure` object is a custom failure type that we can use to indicate that a server error occurred. ### Building the `TodoController` The controller will be responsible for handling HTTP requests and sending back the appropriate response. We will implement five methods in the controller: * **index** `GET /resource` * **show** `GET /resource/{id}` * **store** `POST /resource` * **update** `PUT/PATCH /resource/{id}` * **delete** `DELETE /resource/{id}` These methods will correspond to the standard HTTP methods for retrieving, creating, updating, and deleting data. By using these methods, we can keep our code clean and organized. This approach is inspired by the [Laravel framework's API controller](https://laravel.com/docs/9.x/controllers) methods. #### Abstract `HttpController` We will create a new abstract class in the `backend/lib/controller/http_controller.dart` called `HttpController` and which will have five methods. ```dart import 'dart:async'; import 'package:dart_frog/dart_frog.dart'; abstract class HttpController { FutureOr index(Request request); FutureOr store(Request request); FutureOr show(Request request, String id); FutureOr update(Request request, String id); FutureOr destroy(Request request, String id); } ``` #### Empty `TodoController` implementation Now for the implementation of this contract, we will create a new class `TodoController` in the `backend/lib/controller/todo_controller.dart` file. We will implement each method in the `TodoController` class. ```dart import 'dart:async'; import 'dart:convert'; import 'dart:io'; import 'package:backend/controller/http_controller.dart'; import 'package:dart_frog/dart_frog.dart'; import 'package:either_dart/either.dart'; import 'package:exceptions/exceptions.dart'; import 'package:failures/failures.dart'; import 'package:models/models.dart'; import 'package:repository/repository.dart'; import 'package:typedefs/typedefs.dart'; class TodoController extends HttpController { TodoController(this._repo); final TodoRepository _repo; @override FutureOr index(Request request) async { throw UnimplementedError(); } @override FutureOr show(Request request, String id) async { throw UnimplementedError(); } @override FutureOr destroy(Request request, String id) async { throw UnimplementedError(); } @override FutureOr store(Request request) async { throw UnimplementedError(); } @override FutureOr update(Request request, String id) async { throw UnimplementedError(); } Future>> parseJson( Request request, ) async { throw UnimplementedError(); } } ``` #### Parse request body Before we implement the methods, we will create a new helper method in `HttpController` which will be responsible to parse the request body. If the request body is not a valid JSON, it will return a `Left` object containing a `BadRequestFailure` object. If the request body is a valid JSON, it will return a `Right` object containing the parsed JSON. Add the following method to the `HttpController` class. ```dart Future>> parseJson( Request request, ) async { try { final body = await request.body(); if (body.isEmpty) { throw const BadRequestException(message: 'Invalid body'); } late final Map json; try { json = jsonDecode(body) as Map; return Right(json); } catch (e) { throw const BadRequestException(message: 'Invalid body'); } } on BadRequestException catch (e) { return Left( ValidationFailure( message: e.message, errors: {}, ), ); } } ``` #### Injecting `TodoController` through `provider` > Let's add this to our global middleware `routes/_middleware.dart` file. ```dart import 'package:backend/db/database_connection.dart'; import 'package:backend/todo/controller/todo_controller.dart'; import 'package:backend/todo/data_source/todo_data_source_impl.dart'; import 'package:backend/todo/repositories/todo_repository_impl.dart'; import 'package:dart_frog/dart_frog.dart'; import 'package:data_source/data_source.dart'; import 'package:dotenv/dotenv.dart'; import 'package:repository/repository.dart'; final env = DotEnv()..load(); final _db = DatabaseConnection(env); final _ds = TodoDataSourceImpl(_db); final _repo = TodoRepositoryImpl(_ds); final _todoController = TodoController(_repo); Handler middleware(Handler handler) { return handler .use(requestLogger()) .use(provider((_) => _db)) .use(provider((_) => _ds)) .use(provider((_) => _repo)) .use(provider((_) => _todoController)); } ``` > Note: We are using `requestLogger` middleware to log the request and response. ### Implementing `TodoController` We will implement the `TodoController` methods as follows: #### `index` implementation The `index` method will be responsible for retrieving all todo items from the database. We will implement it as follows: ```dart @override FutureOr index(Request request) async { final res = await _repo.getTodos(); return res.fold( (left) => Response.json( body: {'message': left.message}, statusCode: left.statusCode, ), (right) => Response.json( body: right.map((e) => e.toJson()).toList(), ), ); } ``` We will call the `getTodos` method and map the response. If the failure case is returned, we will return a response with an error status code and the error message. Else, we will return a `200` status code and the list of todos. #### `show` implementation `show` method will be responsible for retrieving a single to-do item from the database. We will implement it as follows: ```dart @override FutureOr show(Request request, String id) async { final todoId = mapTodoId(id); if (todoId.isLeft) { return Response.json( body: {'message': todoId.left.message}, statusCode: todoId.left.statusCode, ); } final res = await _repo.getTodoById(todoId.right); return res.fold( (left) => Response.json( body: {'message': left.message}, statusCode: left.statusCode, ), (right) => Response.json( body: right.toJson(), ), ); } ``` We will first call `mapTodoId` method to validate the `id` parameter. If it returns a failure, we will return a failure response with the status code. Then we will get the todo item from the repository and return the todo item if it is found. Else, we will return a failure response with the status code. #### `store` implementation `store` method is as follows ```dart @override FutureOr store(Request request) async { final parsedBody = await parseJson(request); if (parsedBody.isLeft) { return Response.json( body: {'message': parsedBody.left.message}, statusCode: parsedBody.left.statusCode, ); } final json = parsedBody.right; final createTodoDto = CreateTodoDto.validated(json); if (createTodoDto.isLeft) { return Response.json( body: { 'message': createTodoDto.left.message, 'errors': createTodoDto.left.errors, }, statusCode: createTodoDto.left.statusCode, ); } final res = await _repo.createTodo(createTodoDto.right); return res.fold( (left) => Response.json( body: {'message': left.message}, statusCode: left.statusCode, ), (right) => Response.json( body: right.toJson(), statusCode: HttpStatus.created, ), ); } ``` If `parseJson` resolves to a failure, we will return a response with an error status code and message. We will pass the JSON object to the `CreateTodoDto.validated` method. If this returns a failure, we will return a response with an error status code and message, here it will be `ValidationFailure`. We will pass the DTO to `createTodo` method of the `TodoRepository`. If creating fails we will return a response with an error status code and message. If everything goes right, we will return a response with a `201` status code and the to-do item. #### `destroy` implementation `destroy` method is as follows ```dart @override FutureOr destroy(Request request, String id) async { final todoId = mapTodoId(id); if (todoId.isLeft) { return Response.json( body: {'message': todoId.left.message}, statusCode: todoId.left.statusCode, ); } final res = await _repo.deleteTodo(todoId.right); return res.fold( (left) => Response.json( body: {'message': left.message}, statusCode: left.statusCode, ), (right) => Response.json(body: {'message': 'OK'}), ); } ``` We will first call `mapTodoId` method to validate the `id` parameter. If it returns a failure, we will return a failure response with the status code. Then we will get the delete todo item from the repository and return OK with 200 status code if. If there is a failure, we will return a failure response with the status code. #### `update` implementation `update` method will be responsible for updating a single to-do item from the database. We will implement it as follows: ```dart @override FutureOr update(Request request, String id) async { final parsedBody = await parseJson(request); final todoId = mapTodoId(id); if (todoId.isLeft) { return Response.json( body: {'message': todoId.left.message}, statusCode: todoId.left.statusCode, ); } if (parsedBody.isLeft) { return Response.json( body: {'message': parsedBody.left.message}, statusCode: parsedBody.left.statusCode, ); } final json = parsedBody.right; final updateTodoDto = UpdateTodoDto.validated(json); if (updateTodoDto.isLeft) { return Response.json( body: { 'message': updateTodoDto.left.message, 'errors': updateTodoDto.left.errors, }, statusCode: updateTodoDto.left.statusCode, ); } final res = await _repo.updateTodo( id: todoId.right, updateTodoDto: updateTodoDto.right, ); return res.fold( (left) => Response.json( body: {'message': left.message}, statusCode: left.statusCode, ), (right) => Response.json( body: right.toJson(), ), ); } ``` This method is similar to the `store` method. We will first validate the `id` parameter and then the JSON body. If both are valid, we will call the `updateTodo` method of the `TodoRepository` and then resolve the failure or the updated value. ### Implementing Routes We will now implement the routes. These are the routes that we will have * **GET** `/todos` - Get all todos * **GET** `/todos/:id` - Get a single todo * **POST** `/todos` - Create a todo * **PUT** `/todos/:id` - Update a todo * **PATCH** `/todos/:id` - Update a todo * **DELETE** `/todos/:id` - Delete a todo `dart_frog` has a file system routing. For example, let's say we need to create `todos/1` route. We will create a file `routes/todos/[id].dart` and it will be mapped to `todos/1` route. #### Implementing `todos/` route To implement all the HTTP methods related to `todos/` route, we will create a new file `routes/todos/index.dart`. We will create a file `routes/todos/index.dart` and implement the routes as follows: ```dart import 'dart:io'; import 'package:backend/todo/controller/todo_controller.dart'; import 'package:dart_frog/dart_frog.dart'; Future onRequest(RequestContext context) async { final controller = context.read(); switch (context.request.method) { case HttpMethod.get: return controller.index(context.request); case HttpMethod.post: return controller.store(context.request); case HttpMethod.put: case HttpMethod.patch: case HttpMethod.delete: case HttpMethod.head: case HttpMethod.options: return Response.json( body: {'error': '👀 Looks like you are lost 🔦'}, statusCode: HttpStatus.methodNotAllowed, ); } } ``` Here, we are getting the `TodoController` from the `context` and mapping the respective HTTP method to the controller method. We are also handling cases when the HTTP method is not allowed. #### Implementing `todos/:id` route Similarly, we will create a file `routes/todos/[id].dart` and implement the routes as follows: ```dart import 'dart:io'; import 'package:backend/todo/controller/todo_controller.dart'; import 'package:dart_frog/dart_frog.dart'; Future onRequest(RequestContext context, String id) async { final todoController = context.read(); switch (context.request.method) { case HttpMethod.get: return todoController.show(context.request, id); case HttpMethod.put: case HttpMethod.patch: return todoController.update(context.request, id); case HttpMethod.delete: return todoController.destroy(context.request, id); case HttpMethod.head: case HttpMethod.options: case HttpMethod.post: return Response.json( body: {'error': '👀 Looks like you are lost 🔦'}, statusCode: HttpStatus.methodNotAllowed, ); } } ``` Here, we will get the `id` parameter from the route as a second parameter in `onRequest` method as a string. This can be anything. This explains the use of `mapTodoId` function in `typedefs` package. We will pass the `id` parameter to the controller methods. We will also handle the case when the HTTP method is not allowed. #### Implementing `/` route Finally, we will update `routes/index.dart` file to return `methodNotAllowed` response. This is our `/` route, which will be handled as follows: ```dart import 'dart:io'; import 'package:dart_frog/dart_frog.dart'; Future onRequest(RequestContext context) async { return Response.json( body: {'error': '👀 Looks like you are lost 🔦'}, statusCode: HttpStatus.methodNotAllowed, ); } ``` ## Testing backend We will run some e2e tests, to verify the backend works fine. create a file `backend/e2e/routes_test.dart` and implement the tests as follows: ```dart import 'dart:convert'; import 'dart:io'; import 'package:http/http.dart' as http; import 'package:models/models.dart'; import 'package:test/test.dart'; void main() { late Todo createdTodo; tearDownAll(() async { final response = await http.get(Uri.parse('http://localhost:8080/todos')); final todos = (jsonDecode(response.body) as List) .map((e) => Todo.fromJson(e as Map)) .toList(); for (final todo in todos) { await http.delete(Uri.parse('http://localhost:8080/todos/${todo.id}')); } }); group('E2E -', () { test('GET /todos returns empty list of todos', () async { final response = await http.get(Uri.parse('http://localhost:8080/todos')); expect(response.statusCode, HttpStatus.ok); expect(response.body, equals('[]')); }); test('POST /todos to create a new todo', () async { final response = await http.post( Uri.parse('http://localhost:8080/todos'), headers: { 'Content-Type': 'application/json', }, body: jsonEncode(_createTodoDto.toJson()), ); expect(response.statusCode, HttpStatus.created); createdTodo = Todo.fromJson(jsonDecode(response.body) as Map); expect(createdTodo.title, equals(_createTodoDto.title)); expect(createdTodo.description, equals(_createTodoDto.description)); }); test('GET /todos returns list of todos with one todo', () async { final response = await http.get(Uri.parse('http://localhost:8080/todos')); expect(response.statusCode, HttpStatus.ok); final todos = (jsonDecode(response.body) as List) .map((e) => Todo.fromJson(e as Map)) .toList(); expect(todos.length, equals(1)); expect(todos.first, equals(createdTodo)); }); test('GET /todos/:id returns the created todo', () async { final response = await http.get( Uri.parse('http://localhost:8080/todos/${createdTodo.id}'), ); expect(response.statusCode, HttpStatus.ok); final todo = Todo.fromJson(jsonDecode(response.body) as Map); expect(todo, equals(createdTodo)); }); test('PUT /todos/:id to update the created todo', () async { final updateTodoDto = UpdateTodoDto( title: 'updated title', description: 'updated description', ); final response = await http.put( Uri.parse('http://localhost:8080/todos/${createdTodo.id}'), headers: { 'Content-Type': 'application/json', }, body: jsonEncode(updateTodoDto.toJson()), ); expect(response.statusCode, HttpStatus.ok); final todo = Todo.fromJson(jsonDecode(response.body) as Map); expect(todo.title, equals(updateTodoDto.title)); expect(todo.description, equals(updateTodoDto.description)); }); test('PATCH /todos/:id to update the created todo', () async { final updateTodoDto = UpdateTodoDto( title: 'UPDATED TITLE', description: 'UPDATED DESCRIPTION', ); final response = await http.patch( Uri.parse('http://localhost:8080/todos/${createdTodo.id}'), headers: { 'Content-Type': 'application/json', }, body: jsonEncode(updateTodoDto.toJson()), ); expect(response.statusCode, HttpStatus.ok); final todo = Todo.fromJson(jsonDecode(response.body) as Map); expect(todo.title, equals(updateTodoDto.title)); expect(todo.description, equals(updateTodoDto.description)); }); test('DELETE /todos/:id to delete the created todo', () async { final response = await http.delete( Uri.parse('http://localhost:8080/todos/${createdTodo.id}'), ); expect(response.statusCode, HttpStatus.ok); expect(response.body, jsonEncode({'message': 'OK'})); }); test('GET /todos returns empty list of todos', () async { final response = await http.get(Uri.parse('http://localhost:8080/todos')); expect(response.statusCode, HttpStatus.ok); expect(response.body, equals('[]')); }); }); } final _createTodoDto = CreateTodoDto( title: 'title', description: 'description', ); ``` To run the tests, first, start the backend server by running the following command: ```bash dart_frog dev ``` And then on a new terminal run the tests: ```bash dart test e2e/routes_test.dart ``` ![Tests Passing fine](https://saileshdahal.com.np/images/posts/building-a-fullstack-app-with-dartfrog-and-flutter-in-a-monorepo-part-4/49437eba-3aac-4eda-af60-fe4bd0850487.png) --- We've completed the backend of our full-stack to-do application: Postgres connection, all backend routes, full CRUD operations, and passing tests. In the final part, we build the front end of our to-do app using Flutter. You can always refer back to the GitHub repo at [https://github.com/saileshbro/full\_stack\_todo\_dart](https://github.com/saileshbro/full_stack_todo_dart) if you need help along the way. ============================================================================== # Dart Frog CRUD API with Flutter: models and repositories (Part 3) URL: https://saileshdahal.com.np/building-a-fullstack-app-with-dartfrog-and-flutter-in-a-monorepo-part-3 Published: 2023-01-01 Tags: Flutter, Dart, dart_frog, full stack, Programming Blogs Series: Building a Fullstack App with dart_frog and Flutter in a Monorepo (part 3 of 6) ============================================================================== In the previous part, we set up models, data sources, repositories, and failures for the full-stack to-do application. In this part, we will: * Make changes to the failure classes * Create new failures * [`RequestFailure`](https://github.com/saileshbro/full_stack_todo_dart/blob/780b4662d4a03c17aaef281b100c8a686d8317bb/failures/lib/src/request_failure/request_failure.dart) * [`ValidationFailure`](https://github.com/saileshbro/full_stack_todo_dart/blob/780b4662d4a03c17aaef281b100c8a686d8317bb/failures/lib/src/validation_failure/validation_failure.dart) * [`ServerFailure`](https://github.com/saileshbro/full_stack_todo_dart/blob/780b4662d4a03c17aaef281b100c8a686d8317bb/failures/lib/src/server_failure/server_failure.dart) * Create new Exceptions * [`ServerException`](https://github.com/saileshbro/full_stack_todo_dart/blob/780b4662d4a03c17aaef281b100c8a686d8317bb/exceptions/lib/src/server_exception/server_exception.dart) * [`HttpException`](https://github.com/saileshbro/full_stack_todo_dart/blob/780b4662d4a03c17aaef281b100c8a686d8317bb/exceptions/lib/src/http_exception/http_exception.dart) * [`NotFoundException`](https://github.com/saileshbro/full_stack_todo_dart/blob/780b4662d4a03c17aaef281b100c8a686d8317bb/exceptions/lib/src/http_exception/not_found_exception.dart) * [`BadRequestException`](https://github.com/saileshbro/full_stack_todo_dart/blob/780b4662d4a03c17aaef281b100c8a686d8317bb/exceptions/lib/src/http_exception/bad_request_exception.dart) * Add validation to DTOs * [Add JSON converters](https://github.com/saileshbro/full_stack_todo_dart/blob/780b4662d4a03c17aaef281b100c8a686d8317bb/models/lib/src/serializers/date_time_converter.dart) ## Creating and updating packages In this section, we will create new packages using [very\_good\_cli](https://pub.dev/packages/very_good_cli) and update existing ones to efficiently manage the packages in our full-stack to-do application. ### Working with Failure classes ### Create `build.yaml` We will now create a new file called [`build.yaml`](https://github.com/saileshbro/full_stack_todo_dart/blob/780b4662d4a03c17aaef281b100c8a686d8317bb/failures/build.yaml) in the `failures` directory and add the following code. This will change the behaviour of the [`json_serializable`](https://pub.dev/packages/json_serializable) so that it generates JSON keys in `snake_case`. ```yaml targets: $default: builders: json_serializable: options: any_map: false checked: false create_factory: true disallow_unrecognized_keys: false explicit_to_json: true field_rename: snake generic_argument_factories: false ignore_unannotated: false include_if_null: true ``` #### Update `Failure` Let's add a new getter to the `Failure` abstract class to get the status code of the failure. This will allow us to map the failure to the appropriate [HTTP status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status) in the controller. ```diff abstract class Failure { String get message; + int get statusCode; } ``` #### Update `NetworkFailure` We will update our failure package and add more failures to it. To do this, we will begin by renaming the `code` field in our `NetworkFailure` class to `statusCode`. This will make the field more meaningful and easier for our readers to understand. ```diff class NetworkFailure extends Failure with _$NetworkFailure { /// {@macro network_failure} const factory NetworkFailure({ required String message, - required int code, + required int statusCode, @Default([]) List errors, }) = _NetworkFailure; ``` ### Create new failures We'll be creating [`RequestFailure`](https://github.com/saileshbro/full_stack_todo_dart/blob/780b4662d4a03c17aaef281b100c8a686d8317bb/failures/lib/src/request_failure/request_failure.dart), [`ServerFailure`](https://github.com/saileshbro/full_stack_todo_dart/blob/780b4662d4a03c17aaef281b100c8a686d8317bb/failures/lib/src/server_failure/server_failure.dart), and [`ValidationFailure`](https://github.com/saileshbro/full_stack_todo_dart/blob/780b4662d4a03c17aaef281b100c8a686d8317bb/failures/lib/src/validation_failure/validation_failure.dart) to help us map out any potential errors that may occur. #### `RequestFailure` To create a `RequestFailure` class, we will create a new file in the `src` directory called `request_failure/request_failure.dart`. ```dart import 'dart:io'; import 'package:failures/failures.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; part 'request_failure.freezed.dart'; @freezed class RequestFailure extends Failure with _$RequestFailure { const factory RequestFailure({ required String message, @Default(HttpStatus.badRequest) int statusCode, }) = _RequestFailure; } ``` We will create two new classes in the `src` directory, `ServerFailure` and `ValidationFailure`. The `ServerFailure` class will be used to represent errors that occur on the server side of our application, and the `ValidationFailure` class will be used to represent validation errors in our application. #### `ServerFailure` ```dart import 'dart:io'; import 'package:failures/failures.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; part 'server_failure.freezed.dart'; @freezed class ServerFailure extends Failure with _$ServerFailure { const factory ServerFailure({ required String message, @Default(HttpStatus.internalServerError) int statusCode, }) = _ServerFailure; } ``` #### `ValidationFailure` ```dart import 'dart:io'; import 'package:failures/failures.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; part 'validation_failure.freezed.dart'; @freezed class ValidationFailure extends Failure with _$ValidationFailure { const factory ValidationFailure({ required String message, @Default(HttpStatus.badRequest) int statusCode, @Default({}) Map> errors, }) = _ValidationFailure; } ``` > Don't forget to export and run [`build_runner`](https://pub.dev/packages/build_runner) ```dart library failures; export 'src/failure.dart'; export 'src/network_failure/network_failure.dart'; export 'src/request_failure/request_failure.dart'; export 'src/server_failure/server_failure.dart'; export 'src/validation_failure/validation_failure.dart'; ``` > Note: You can run the `build_runner` command by running `flutter pub run build_runner build` in the terminal. ### Working with packages #### Update `typedefs` package We will be creating a function called `mapTodoId` in the `typedefs` package. This function will be responsible for converting a string id to a `TodoId` object. This is necessary because we need a way to convert user input into a format our application can understand and use. If the id is not valid, the `mapTodoId` function will return a `RequestFailure` object so we can handle bad input gracefully. ```dart Either mapTodoId(String id) { try { final todoId = int.tryParse(id); if (todoId == null) throw const BadRequestException(message: 'Invalid id'); return Right(todoId); } on BadRequestException catch (e) { return Left( RequestFailure( message: e.message, statusCode: e.statusCode, ), ); } } ``` The `mapTodoId` method will return either a `TodoId` object or a `RequestFailure` object. Make sure to add the dependencies [`either_dart`](https://pub.dev/packages/either_dart) and `failures` to the `pubspec.yaml` file of the `typedefs` package. ```yaml dependencies: either_dart: ^0.3.0 exceptions: path: ../exceptions failures: path: ../failures ``` #### Create a new `exceptions` package To handle internal exceptions and map them to the appropriate failures, we will create a new package called `exceptions` and throw our custom exceptions. For example, if we encounter a [`PostgreSQLException`](https://pub.dev/documentation/postgres/latest/postgres/PostgreSQLException-class.html) while inserting a new to-do, we will throw a `ServerException` and map it to the `ServerFailure` class. To create the `exceptions` package, run the following command in the terminal: ```bash very_good create -t dart_pkg exceptions ``` This package will include different types of exceptions such as `ServerException`, `HttpException`, and `NotFoundException`. The `ServerException` will be thrown in the case of an internal server error, while the `HttpException` is an abstract class that will be extended by other exceptions like `NotFoundException` and `BadRequestException`. We can use these custom exceptions to handle internal errors and map them to the appropriate failure classes, such as `ServerFailure` or `RequestFailure`. We will start by creating a new file inside `src/server_exception/server_exception.dart` and add the following code: ##### `ServerException` To create `ServerException` we will create a new file `src/server_exception/server_exception.dart` and add the following code: ```dart class ServerException implements Exception { const ServerException(this.message); final String message; @override String toString() => 'ServerException: $message'; } ``` ##### `HttpExpection` To create `HttpException` we will create a new file `src/http_exception/http_exception.dart` and add the following code: ```dart abstract class HttpException implements Exception { const HttpException(this.message, this.statusCode); final String message; final int statusCode; @override String toString() => '$runtimeType: $message'; } ``` ##### `NotFoundException` Next, we will create `NotFoundException`, which will be thrown when a resource is not found. To do this, create a new file called `src/http_exception/not_found_exception.dart` and add the following code: ```dart class NotFoundException extends HttpException { const NotFoundException(String message) : super(message, HttpStatus.notFound); } ``` ##### `BadRequestException` Similarly, we will create a new file inside `src/http_exception/bad_request_exception.dart` and add the following code: ```dart class BadRequestException extends HttpException { const BadRequestException({ required String message, this.errors = const {}, }) : super(message, HttpStatus.badRequest); final Map> errors; } ``` Make sure to import the `HttpException`. Once you are done with `HttpException`, add an export statement in `http_exception.dart` file. ```dart export './bad_request_exception.dart'; export './not_found_exception.dart'; ``` And finally, export the `HttpException` from `exceptions/lib/exceptions.dart` file. ```dart library exceptions; export 'src/http_exception/http_exception.dart'; export 'src/server_exception/server_exception.dart'; ``` #### Update `models` package ##### Let's handle the validation To add validation to our `CreateTodoDto` class, we will create a new static method called validated in `models/lib/src/create_todo_dto/create_todo_dto.dart`. This method will return either a `ValidationFailure` object or a `CreateTodoDto` object. We will use this method to ensure that our to-do creation requests contain all necessary information before they are processed. The validation rules are: * the `title` should not be empty * the `description` should not be empty Before we can add the validated method to the `CreateTodoDto` class, we need to make sure that the necessary packages are added to the `pubspec.yaml` file. ```yaml dependencies: either_dart: ^0.3.0 exceptions: path: ../exceptions failures: path: ../failures freezed_annotation: ^2.2.0 json_annotation: ^4.7.0 typedefs: path: ../typedefs ``` Now we will create a `validated` method inside `CreateTodoDto` ```dart static Either validated( Map json, ) { try { final errors = >{}; if (json['title'] == null) { errors['title'] = ['Title is required']; } if (json['description'] == null) { errors['description'] = ['Description is required']; } if (errors.isEmpty) return Right(CreateTodoDto.fromJson(json)); throw BadRequestException( message: 'Validation failed', errors: errors, ); } on BadRequestException catch (e) { return Left( ValidationFailure( message: e.message, errors: e.errors, statusCode: e.statusCode, ), ); } } ``` Similarly, we will create a new static method called `validated` to validate the `UpdateTodoDto`. We will ensure that at least one field is present. ```dart static Either validated( Map json, ) { try { final errors = >{}; if (json['title'] == null || json['title'] == '') { errors['title'] = ['At least one field must be provided']; } if (json['description'] == null || json['description'] == '') { errors['description'] = ['At least one field must be provided']; } if (json['completed'] == null) { errors['completed'] = ['At least one field must be provided']; } if (errors.length < 3) return Right(UpdateTodoDto.fromJson(json)); throw BadRequestException( message: 'Validation failed', errors: errors, ); } on BadRequestException catch (e) { return Left( ValidationFailure( message: e.message, statusCode: e.statusCode, errors: e.errors, ), ); } } ``` ##### Custom JSON converters To serialize and deserialize our `DateTime` objects, we will create a new file called `models/lib/src/serializers/date_time_serializer.dart`. In this file, we will add the necessary code to handle the serialization and deserialization of `DateTime` objects. These classes will implement the [`JsonConverter`](https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonConverter-class.html) interface and provide the necessary logic to convert `DateTime` objects to and from JSON. The `DateTimeConverterNullable` class will handle cases where the `DateTime` object may be `null`, while the `DateTimeConverter` class will handle non-null `DateTime` objects. With these classes in place, we will be able to correctly handle the formatting of `DateTime` objects when retrieving data from the database. ```dart class DateTimeConverterNullable extends JsonConverter { const DateTimeConverterNullable(); @override DateTime? fromJson(dynamic json) { if (json == null) return null; return const DateTimeConverter().fromJson(json); } @override String? toJson(DateTime? object) { if (object == null) return null; return const DateTimeConverter().toJson(object); } } class DateTimeConverter extends JsonConverter { const DateTimeConverter(); @override DateTime fromJson(dynamic json) { if (json is DateTime) return json; return DateTime.parse(json as String); } @override String toJson(DateTime object) { return object.toIso8601String(); } } ``` Now we can use this converter in our `Todo` model. ```diff factory Todo({ required TodoId id, required String title, @Default('') String description, @Default(false) bool? completed, - required DateTime createdAt, + @DateTimeConverter() required DateTime createdAt, - DateTime? updatedAt, + @DateTimeConverterNullable() DateTime? updatedAt, }) = _Todo; ``` Once you have finished updating the `Todo` model to use the converters. > Don't forget to run `build_runner` In this part, we updated the failure classes, added new exceptions, and made sure our DTOs were properly validated. In part 4, we connect to a Postgres database and complete all our backend routes. If you ever need a reference, head on over to the GitHub repo for this tutorial at [https://github.com/saileshbro/full\_stack\_todo\_dart](https://github.com/saileshbro/full_stack_todo_dart). ============================================================================== # Dart Frog and Flutter monorepo: shared Dart packages (Part 2) URL: https://saileshdahal.com.np/building-a-fullstack-app-with-dartfrog-and-flutter-in-a-monorepo-part-2 Published: 2022-12-22 Tags: dart_frog, very_good_cli, Dart, Flutter, Programming Blogs Series: Building a Fullstack App with dart_frog and Flutter in a Monorepo (part 2 of 6) ============================================================================== In the first part of this article, we set up [melos](https://docs.page/invertase/melos) as a tool for managing monorepo projects and installed [dart\_frog](https://pub.dev/packages/dart_frog) as a web framework for building server-side apps with Dart. We also created a new Dart Frog project, which included adding the `dart_frog` dependency on the project's `pubspec.yaml` file and creating the necessary files and directories. We will create models, data sources, repositories, and failure classes as a separate Dart package in part 2. To do this, we will be using [very\_good\_cli](https://cli.vgv.dev/). ## Setting up models, repositories and data services ### Installing [very\_good\_cli](https://cli.vgv.dev/) > `very_good_cli`, a command-line interface that provides helpful commands and templates for generating various types of projects, including Flutter apps, Flame games, Flutter packages, Dart packages, federated plugins, and Dart CLI projects. `very_good_cli` makes it easy to create and set up these types of projects with a single command, so let's get started! To install it, open a terminal and enter the following command: ```bash dart pub global activate very_good_cli ``` To confirm that Very Good CLI has been installed correctly, you can enter the following command in a terminal `very_good --version` ### Creating `typedefs` We will have our typedefs, which will be shared between the backend and frontend of our project. To create a new Dart package for these typedefs, we can use the following command: ```bash very_good create -t dart_pkg typedefs ``` This will generate a new dart package `typedefs`. Here `-t dart_pkg` means we are creating a new dart package. Once the `typedefs` package has been created and the necessary files and directories have been generated, you can navigate to `typedefs/lib/src/typedefs.dart` to create a new typedef. In this file, you can create a new typedef by adding the following code: ```dart /// Primary key type for a Todo. typedef TodoId = int; ``` This will create a new typedef called `TodoId`, an alias for the type `int`. This typedef can then be used throughout the backend and frontend of your project to represent an identifier for a to-do item. Once you are done, make sure to export the file from `lib/typedefs.dart` ```dart library typedefs; export 'src/typedefs.dart'; ``` We will add additional shared typedefs to this file as needed. [GitHub link](https://github.com/saileshbro/full_stack_todo_dart/tree/main/typedefs) ### Creating `models` Similarly, we will have a separate package to house shared data models. 1. `CreateTodoDto` This will be used to send and receive payload for creating a new todo. 2. `UpdateTodoDto` This will be used to send and receive payload for updating an existing todo. 3. `Todo` This is the representation of the `Todo` entity. To create a separate package for models you can use the `very_good` CLI: ```bash very_good create -t dart_pkg models ``` Once the package has been created, we will install [freezed](https://pub.dev/packages/freezed) for JSON serialization and value equality, as this library provides helpful tools for these tasks. We will use [json\_serializable](https://pub.dev/packages/json_serializable) for JSON serialization. To install [freezed](https://pub.dev/packages/freezed) and [freezed\_annotation](https://pub.dev/packages/freezed_annotation), open your terminal inside the `models` package and use the command: ```bash dart pub add freezed json_serializable build_runner -d dart pub add freezed_annotation json_annotation ``` Inside `models/lib` we will create files in the given order. We have created a parent folder for each model to house the generated codes so that it will look cleaner. ```bash . ├── models.dart └── src ├── create_todo_dto │ └── create_todo_dto.dart ├── todo │ └── todo.dart └── update_todo_dto └── update_todo_dto.dart ``` Now, in `todo.dart` we will use [freezed](https://pub.dev/packages/freezed) package to create a data class. > [freezed](https://pub.dev/packages/freezed) package is a code generation tool that allows you to create immutable classes with concise, boilerplate-free syntax. It uses the `@freezed` annotation to generate a number of useful methods and operators for your class, such as `==`, `hashCode`, and `copyWith`. ```dart import 'package:freezed_annotation/freezed_annotation.dart'; import 'package:typedefs/typedefs.dart'; part 'todo.freezed.dart'; part 'todo.g.dart'; @freezed class Todo with _$Todo { factory Todo({ required TodoId id, required String title, @Default('') String description, @Default(false) bool? completed, required DateTime createdAt, DateTime? updatedAt, }) = _Todo; factory Todo.fromJson(Map json) => _$TodoFromJson(json); } ``` Since `TodoId` is in a separate package, we will have to add it in the `models/pubspec.yaml` file as a dependency. After adding the `pubspec.yaml` should look like this. ```yaml name: models description: A Very Good Project created by Very Good CLI. version: 0.1.0+1 publish_to: none environment: sdk: ">=2.18.0 <3.0.0" dependencies: freezed_annotation: ^2.2.0 json_annotation: ^4.7.0 typedefs: path: ../typedefs dev_dependencies: build_runner: ^2.3.3 freezed: ^2.3.2 json_serializable: ^6.5.4 lints: ^2.0.0 mocktail: ^0.3.0 test: ^1.19.2 very_good_analysis: ^3.1.0 ``` Similarly, we will create `create_todo_dto.dart` for `CreateDotoDto` class. ```dart import 'package:freezed_annotation/freezed_annotation.dart'; part 'create_todo_dto.freezed.dart'; part 'create_todo_dto.g.dart'; @freezed class CreateTodoDto with _$CreateTodoDto { factory CreateTodoDto({ required String title, required String description, }) = _CreateTodoDto; factory CreateTodoDto.fromJson(Map json) => _$CreateTodoDtoFromJson(json); } ``` Similarly, we will create `update_todo_dto.dart` for `UpdateTodoDto` class. ```dart import 'package:freezed_annotation/freezed_annotation.dart'; part 'update_todo_dto.freezed.dart'; part 'update_todo_dto.g.dart'; @freezed class UpdateTodoDto with _$UpdateTodoDto { factory UpdateTodoDto({ String? title, String? description, bool? completed, }) = _UpdateTodoDto; factory UpdateTodoDto.fromJson(Map json) => _$UpdateTodoDtoFromJson(json); } ``` Once this is done, we run [build\_runner](https://pub.dev/packages/build_runner) and generate the necessary code. To generate the missing code, you can run the following command: ```bash dart pub run build_runner build --delete-conflicting-outputs ``` This will generate `*.g.dart` and `*.freezed.dart` files. Once this is done, you will have files inside `models/lib` in the given order. ```plaintext . ├── models.dart └── src ├── create_todo_dto │ ├── create_todo_dto.dart │ ├── create_todo_dto.freezed.dart │ └── create_todo_dto.g.dart ├── todo │ ├── todo.dart │ ├── todo.freezed.dart │ └── todo.g.dart └── update_todo_dto ├── update_todo_dto.dart ├── update_todo_dto.freezed.dart └── update_todo_dto.g.dart ``` Once you are done creating the models, be sure to export them from `lib/models.dart` ```dart library models; export 'src/create_todo_dto/create_todo_dto.dart'; export 'src/todo/todo.dart'; export 'src/update_todo_dto/update_todo_dto.dart'; ``` You can find the [GitHub link for models here](https://github.com/saileshbro/full_stack_todo_dart/tree/main/models). ### Creating `failures` In the next step, we will create a separate package to handle failures in our application. This package will contain all the failures that may occur, such as `NetworkFailure` or `ServerFailure`. Whenever we need to handle an error, we will encapsulate the value or error in a union type and return it from a repository. For example, when we make an API call to retrieve data, we may receive either the requested data or an error. By encapsulating this in an `Either` type, we can handle failures in our app. > This is inspired by [ReSo Coder's Clean Architecture](https://resocoder.com/flutter-clean-architecture-tdd/) tutorial. To create the failures package: ```bash very_good create -t dart_pkg failures ``` This will create a new package in `failures` directory. We will use `freezed` and `json_serializable` libraries for data serialization here as well. ```bash dart pub add freezed json_serializable build_runner -d dart pub add freezed_annotation json_annotation ``` To begin, we will create a `Failure` class inside the `lib/src/failure.dart` directory. This class will be the base for all failure types and will handle and manage our app's failures. ```dart abstract class Failure { String get message; } ``` We can then create specific failure classes, such as `NetworkFailure`, to return to the caller whenever there is a network exception. For instance, if we have a `TodosRemoteDataSource` that makes a call to a backend and encounters a HTTP exception, the data source can throw a `NetworkException` which will be caught by a repository `TodoRepository`. The repository will then encapsulate the error in a union type and return either the requested data or the failure to the caller. To create a `NetworkFailure` class, we will create a new file in the `src` directory called `network_failure/network_failure.dart`. ```dart import 'package:failures/failures.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; part 'network_failure.freezed.dart'; part 'network_failure.g.dart'; @freezed class NetworkFailure extends Failure with _$NetworkFailure { const factory NetworkFailure({ required String message, required int code, @Default([]) List errors, }) = _NetworkFailure; factory NetworkFailure.fromJson(Map json) => _$NetworkFailureFromJson(json); } ``` Once you have finished creating the `NetworkFailure` class, you can run the `build_runner` command to generate the necessary files for your project. Make sure to export the failure class from `failures/lib/failures.dart` ```dart library failures; export 'src/failure.dart'; export 'src/network_failure/network_failure.dart'; ``` You can view [the code from here.](https://github.com/saileshbro/full_stack_todo_dart/tree/main/failures) ### Creating `data_source` We will create a new package with `very_good_cli` for our data source. ```bash very_good create -t dart_pkg data_source ``` Once the package is created, we can create an abstract data source contract. ```dart import 'package:models/models.dart'; import 'package:typedefs/typedefs.dart'; abstract class TodoDataSource { Future> getAllTodo(); Future getTodoById(TodoId id); Future createTodo(CreateTodoDto todo); Future updateTodo({ required TodoId id, required UpdateTodoDto todo, }); Future deleteTodoById(TodoId id); } ``` You can import the missing packages in `pubspec.yaml`. After importing, it should look like this. ```yaml name: data_source description: A Very Good Project created by Very Good CLI. version: 0.1.0+1 publish_to: none environment: sdk: ">=2.18.0 <3.0.0" dependencies: models: path: ../models typedefs: path: ../typedefs dev_dependencies: mocktail: ^0.3.0 test: ^1.19.2 very_good_analysis: ^3.1.0 ``` The `TodoDataSource` class will be used by both the frontend and backend of our app to access and manage data related to to-dos. This class will throw known exceptions that can be handled in the `TodoRepository`. To manage these exceptions, we will create a separate `exceptions` package where we can register all the exceptions used in our app. The `TodoRepository` will then be responsible for handling these exceptions. Once you are done adding the data source, make sure to export it in `data_sources/data_sources.dart` ```dart library data_source; export 'src/todo_data_source.dart'; ``` You can view the code [for the data\_source from GitHub.](https://github.com/saileshbro/full_stack_todo_dart/tree/main/data_source) ### Creating `repository` We will make use of the `TodoDataSource` class to access and manage data related to to-dos. We will also catch any exceptions thrown by the `TodoDataSource` and serialize them as failures. `TodoRepository` will then return the failure or the requested data to the caller. To create the `TodoRepository` class, we will first navigate to the root directory of our project and create a new package using the `very_good` CLI: ```bash very_good create -t dart_pkg repository ``` Once we are done, we will add all the dependencies in `pubspec.yaml` and run `dart pub get` to get all the packages. ```yaml dependencies: either_dart: ^0.3.0 models: path: ../models failures: path: ../failures typedefs: path: ../typedefs data_source: path: ../data_source ``` To create an abstract `TodoRepository` class, we will navigate to our project's `lib/src` directory and create a new file called `todo_repository.dart`. Inside this file, we will make the `TodoRepository` class and add the necessary abstract methods that will be used to manage and access data related to to-dos. ```dart import 'package:either_dart/either.dart'; import 'package:failures/failures.dart'; import 'package:models/models.dart'; import 'package:typedefs/typedefs.dart'; abstract class TodoRepository { Future>> getTodos(); Future> getTodoById(TodoId id); Future> createTodo(CreateTodoDto createTodoDto); Future> updateTodo({ required TodoId id, required UpdateTodoDto updateTodoDto, }); Future> deleteTodo(TodoId id); } ``` We are using [either\_dart](https://pub.dev/packages/either_dart) to encapsulate the failure and data in the same place. > [either\_dart](https://pub.dev/packages/either_dart) is the error handling and railway-oriented programming library is a Dart library that supports async "map" and async "then" functions for working with asynchronous computations and handling errors with `Future` values. After you are done, make sure to export it in `src/repository.dart` ```dart library repository; export 'src/todo_repository.dart'; ``` You can find the [code for this on GitHub](https://github.com/saileshbro/full_stack_todo_dart/tree/main/repository). That was a lot of code. In Part 3, we start implementing the repository layer: setting up the backend and establishing database connections. ============================================================================== # Dart Frog and Flutter monorepo tutorial: build a full-stack app (Part 1) URL: https://saileshdahal.com.np/building-a-fullstack-app-with-dartfrog-and-flutter-in-a-monorepo-part-1 Published: 2022-12-19 Tags: dart-backend, Dart, Flutter, full stack, Programming Blogs Series: Building a Fullstack App with dart_frog and Flutter in a Monorepo (part 1 of 6) ============================================================================== ## Introduction This tutorial will create a full-stack to-do application using Dart and Flutter in a monorepo setup. > A monorepo is a version control repository containing multiple projects with common code. This allows for easier management and collaboration on projects within a single repository. We will share a common code between the frontend and backend of our to-do application, like interfaces, data models e.t.c. The application's backend will be built using [dart\_frog](https://pub.dev/packages/dart_frog), while the front end will be developed using Flutter. This tutorial teaches you how to: * Set up a monorepo * Create a full-stack Dart Flutter application * Manage and share common code between front-end and back end * Build a to-do application with CRUD functionality * Complete the tutorial with a functional to-do application ## Let's Go! Before we can build our full-stack to-do application, we must set up the necessary tools and dependencies. First, we will install [melos](https://pub.dev/packages/melos). ### Setup [melos](https://docs.page/invertase/melos) > [Melos](https://docs.page/invertase/melos) is a library that will be used to set up and manage monorepo projects. To install [melos](https://docs.page/invertase/melos), open a terminal and run the following command: ```bash dart pub global activate melos ``` This command will install Melos globally, allowing us to use it in any project. Create a `melos.yaml` file and add the below content to set up our full-stack to-do application. ```yaml name: full_stack_todo_dart packages: - /**/pubspec.yaml ``` The `packages` field defines the packages that are part of the monorepo. In this case, we are using the `/**/pubspec.yaml` pattern, which tells Melos to search for all `pubspec.yaml` files in the project and consider them as packages. ### Hopping into backend with [dart\_frog](https://pub.dev/packages/dart_frog) To handle the server-side logic of our full-stack to-do application, we will use a library called [dart\_frog](https://pub.dev/packages/dart_frog). > [dart\_frog](https://pub.dev/packages/dart_frog) is a lightweight web framework for Dart that makes it easy to build server-side applications. To install [dart\_frog](https://pub.dev/packages/dart_frog), open a terminal and run the following command: ```bash dart pub global activate dart_frog_cli ``` To create a new dart\_frog project, open a terminal and navigate to the directory where you want to create the project. Then, run the following command: ```bash dart_frog create backend ``` The command "dart\_frog" creates a new project with necessary files and directories and adds the dependency "dart\_frog" to the project's "pubspec.yaml" file. Once you have created a new dart\_frog project and set up the backend of your full-stack to-do application, you can add a Melos script to run the server. To do this, open the `melos.yaml` file in the root directory of your project and add the following content: ```yaml scripts: backend:dev: run: melos exec -c 1 --fail-fast -- "dart_frog dev" description: Starts the dev server for the backend select-package: flutter: false ``` Once these steps have been completed, you can run the `backend:dev` script by opening a terminal and navigating to the root directory of your project. Then, run the following command `melos run backend:dev` This will start the dev server for the backend of your to-do application. You can then start building the server-side logic of your application using dart\_frog. ```plaintext ✓ Running on http://localhost:8080 (0.1s) ``` If you see an output similar to the one above after running the `melos run backend:dev` command means that the dev server for the backend of your full-stack to-do application has been successfully started. If you open `http://localhost:8080`, you should be greeted with `Welcome to Dart Frog!` > Stay tuned for part-2 ...