Skip to content
tezvyn:

Drift: Type-Safe SQL in Dart & Flutter

Source: drift.simonbinder.euHardHow cards are made

Drift: Type-Safe SQL in Dart & Flutter

Drift gives your raw SQL queries compile-time safety. Instead of parsing maps, its build-time analyzer validates your SQL and generates typed Dart objects for results. Use it to write complex queries without risking runtime errors from typos or schema changes.

Why it exists

Standard Dart database libraries often return a List<Map<String, dynamic>>, a structure that offers no type safety. A typo in a column name or a schema mismatch is only discovered at runtime, leading to crashes. Drift was created to bring compile-time guarantees to database persistence in Dart, bridging the gap between raw SQL's power and Dart's type system.

The mental model

Think of Drift's SQL integration as a pre-flight check for your database queries. Before your app is even compiled, Drift's build runner acts like a database expert. It reads your SQL, checks it against your table definitions, and then generates the exact Dart classes needed to hold the results. It turns potential runtime crashes into compile-time errors you can fix immediately.

How it works

You define your tables using Dart classes. Then, you write SQL queries, typically in dedicated .drift files. During the standard Dart build process, a code generator parses this SQL. It understands the syntax, infers the columns and types of the result set, and generates corresponding type-safe Dart classes and methods. To execute the query, you call the generated Dart method and receive a list of strongly-typed objects, not a generic map.

When to use it

Use Drift's type-safe SQL when you need to write complex queries that are difficult or verbose to express with a fluent API. This is perfect for intricate joins, aggregations, or using database-specific functions not covered by the fluent interface. It allows developers comfortable with SQL to leverage their skills without sacrificing the safety of the Dart type system.

When not to use it

For simple CRUD (Create, Read, Update, Delete) operations, Drift's fluent Dart API is often cleaner and requires no context switching. For example, select(users).watch() is more direct than writing the equivalent SQL. Overusing raw SQL for simple tasks adds unnecessary boilerplate and can make the code harder to refactor if the schema changes.

One canonical example

Imagine you have users and purchases tables. To get each user and their total spending, you could write a query in a .drift file: totalSpentByUser: SELECT u.*, SUM(p.price) AS total FROM users u JOIN purchases p ON p.user_id = u.id GROUP BY u.id;. At compile time, Drift generates a TotalSpentByUserResult class with properties for all columns from the users table plus a double total field. Your Dart code then calls a generated method and gets a List<TotalSpentByUserResult>, fully typed and safe.

Interview question

What is the primary problem Drift aims to solve in Dart database interactions?

  • a.To provide a fully object-relational mapping (ORM) solution that abstracts all SQL.
  • b.To catch SQL-related errors like typos or schema inconsistencies at compile-time.Correct
  • c.To offer a more concise and fluent API for all types of database queries.
  • d.To ensure all database operations are executed on a separate isolate for performance.
Why?

Drift's core purpose is to bring compile-time type safety to SQL queries, preventing runtime crashes that would otherwise occur due to typos in column names or mismatches with the database schema. While it offers a fluent API, its unique value proposition is compile-time validation of raw SQL, not abstracting all SQL or solely focusing on a concise API for all queries.

Just read this? Test yourself on what you have been reading.

Read the original → drift.simonbinder.eu

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on flutter — each one lists the topics its interview covers.

See open roles