Skip to content

Flutter example showing how to use Floor streams with Riverpod's StreamProvider and AsyncNotifierProvider, comparing inheritance vs non-inheritance models.

Notifications You must be signed in to change notification settings

SofiaGracia/Streams_in_floor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

66 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Floor Stream Demo (Flutter + Riverpod)

This project is a practical experiment to better understand how Streams work with Floor (a SQLite persistence library for Flutter) and how to use both StreamProvider and AsyncNotifierProvider with Riverpod.

🎯 Goals

  • Observe how Floor emits changes reactively using DAOs that return Stream<List<T>>.
  • Compare StreamProvider and AsyncNotifierProvider for managing data in Flutter.
  • Explore how inheritance affects data modeling and UI logic.
  • Analyze how to structure reactive screens based on different data sources.

πŸ“¦ Project Structure

This app defines two main entities:

  • Teacher: a simple class with no inheritance.
  • TeacherExtends: a class that extends a base User class.

For each entity, the app includes 3 screens:

1. Search Screen

  • Contains a search bar to filter teachers by name.
  • Uses StreamProvider to reactively display results as Floor emits data changes.

2. List with Stream Screen

  • Shows a list of teachers that updates in real time.
  • Includes a FloatingActionButton to add new teachers.
  • Powered by a StreamProvider that listens to the Floor DAO.

3. List with AsyncNotifier Screen

  • Shows a list of teachers using an AsyncNotifier.
  • This list does not update automatically when new teachers are added.
  • The notifier loads data only once via a Future when initialized.
  • Designed to contrast with the real-time behavior of the StreamProvider screen.

πŸ§ͺ This setup allows you to clearly see how StreamProvider reacts to database changes while AsyncNotifier requires explicit state updates.

🧩 Architecture and UI

The app uses a side drawer menu that lets you switch between:

  • Screens for Teacher
  • Screens for TeacherExtends

Although much of the code could be reused (e.g. through inheritance or extensions), the UI was deliberately split to highlight differences between a simple class and an inherited one.

πŸš€ Technologies & Packages

  • Flutter
  • Floor – SQLite persistence
  • Riverpod – state management

πŸ“ˆ Key Learnings

This project acts as a sandbox to understand:

  • When to use StreamProvider for live updates
  • When AsyncNotifier is more appropriate
  • How Floor manages data reactivity through DAO streams
  • How inheritance impacts model structure and UI implementation

🧠 Purpose

This project is intended as a personal learning tool and technical demo. It’s not a production-ready app, but a minimal and focused experiment on reactive data handling in Flutter using Floor and Riverpod.

πŸ“‚ Folder Structure

ℹ️ This project includes generated files like database.g.dart for ease of use and to avoid running code generation manually.

lib
β”œβ”€β”€ application
β”œβ”€β”€ data
β”‚Β Β  β”œβ”€β”€ datasources
β”‚Β Β  β”‚Β Β  └── db
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ dao
β”‚Β Β  β”‚Β Β      β”‚Β Β  β”œβ”€β”€ teacher_dao.dart
β”‚Β Β  β”‚Β Β      β”‚Β Β  └── teacherextends_dao.dart
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ database.dart
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ database.g.dart
β”‚Β Β  β”‚Β Β      └── database_service.dart
β”‚Β Β  └── repository
β”‚Β Β      β”œβ”€β”€ teacher_db.dart
β”‚Β Β      └── teacherextends_db.dart
β”œβ”€β”€ domain
β”‚Β Β  β”œβ”€β”€ entities
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ teacher.dart
β”‚Β Β  β”‚Β Β  └── teacher_extends.dart
β”‚Β Β  └── models
β”‚Β Β      └── user.dart
β”œβ”€β”€ main.dart
β”œβ”€β”€ presentation
β”‚Β Β  β”œβ”€β”€ providers
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ database.dart
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ teacher
β”‚Β Β  β”‚Β Β  β”‚Β Β  β”œβ”€β”€ teacher_async_notifier.dart
β”‚Β Β  β”‚Β Β  β”‚Β Β  β”œβ”€β”€ teacher_repository.dart
β”‚Β Β  β”‚Β Β  β”‚Β Β  └── teacher_stream.dart
β”‚Β Β  β”‚Β Β  └── teacherextends
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ teacherext_async_notifier.dart
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ teacherext_repository.dart
β”‚Β Β  β”‚Β Β      └── teacherext_stream.dart
β”‚Β Β  β”œβ”€β”€ screens
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ form.dart
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ home_drawer.dart
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ teacher
β”‚Β Β  β”‚Β Β  β”‚Β Β  β”œβ”€β”€ teacher_async.dart
β”‚Β Β  β”‚Β Β  β”‚Β Β  β”œβ”€β”€ teacher_list.dart
β”‚Β Β  β”‚Β Β  β”‚Β Β  β”œβ”€β”€ teacher_search.dart
β”‚Β Β  β”‚Β Β  β”‚Β Β  └── teacher_stream.dart
β”‚Β Β  β”‚Β Β  └── teacherextends
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ teacherext_async.dart
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ teacherext_list.dart
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ teacherext_search.dart
β”‚Β Β  β”‚Β Β      └── teacherext_stream.dart
β”‚Β Β  └── widgets
β”‚Β Β      β”œβ”€β”€ navigation_bar.dart
β”‚Β Β      β”œβ”€β”€ save_form_button.dart
β”‚Β Β      β”œβ”€β”€ search_bar.dart
β”‚Β Β      └── teacher_widget.dart
└── shared
    └── utils
        └── constants.dart

✨ Want to contribute?

Pull requests, ideas, or feedback are welcome!

Floor Stream Demo (Flutter + Riverpod)

Aquest projecte és una prova per entendre millor com funcionen els Streams amb Floor i les diferències entre StreamProvider i AsyncNotifierProvider amb Riverpod.

🎯 Objectius

  • Comprovar com Floor emet canvis reactius usant DAOs que retornen Stream<List<T>>.
  • Comparar el comportament entre StreamProvider i AsyncNotifierProvider.
  • Explorar la diferΓ¨ncia entre models amb i sense herΓ¨ncia a nivell de dades i interfΓ­cie.
  • Analitzar com es poden estructurar pantalles que reaccionen a dades en temps real.

πŸ“¦ Estructura del projecte

El projecte contΓ© dues entitats principals:

  • Teacher: una classe simple sense herΓ¨ncia.
  • TeacherExtends: una classe que hereta de User.

Per a cada entitat hi ha 3 pantalles:

1. Pantalla de Search

  • Inclou una barra de cerca per filtrar professors pel nom.
  • Utilitza StreamProvider per mostrar resultats de forma reactiva a mesura que Floor emet canvis.

2. Pantalla de Llista amb Stream

  • Mostra una llista de professors actualitzada en temps real.
  • TΓ© un FloatingActionButton per afegir nous professors.
  • Es basa en un StreamProvider connectat al DAO de Floor.

3. Pantalla de Llista amb AsyncNotifier

  • Mostra una llista de professors, perΓ² no s’actualitza automΓ ticament.
  • El AsyncNotifier nomΓ©s carrega dades en la inicialitzaciΓ³ via Future.
  • Permet comparar amb la pantalla basada en StreamProvider.

πŸ§ͺ AixΓ² permet veure clarament com StreamProvider reacciona als canvis a la base de dades, mentre que AsyncNotifier necessita una actualitzaciΓ³ manual.

🧩 Arquitectura i UI

El drawer de l'aplicaciΓ³ permet escollir entre:

  • UI basada en Teacher
  • UI basada en TeacherExtends

Tot i que es podria haver fet reutilització de codi, s'ha optat per mantenir les pantalles separades per fer evident la diferència entre una classe simple i una amb herència.

πŸš€ Tecnologies i paquets

  • Flutter
  • Floor – per a persistΓ¨ncia amb SQLite
  • Riverpod – per a gestiΓ³ d'estat

πŸ“ Conclusions

Aquest projecte serveix com a sandbox per entendre:

  • Quan Γ©s millor fer servir StreamProvider
  • Quan tΓ© sentit controlar l’estat amb AsyncNotifier
  • Com Floor gestiona reactivitat amb DAOs
  • Com estructurar models amb i sense herΓ¨ncia

πŸ“‚ Estructura de carpetes

ℹ️ Este projecte inclou arxius generats com database.g.dart per facilitat i per a evitar executar generaciΓ³ de codi manualment.

lib
β”œβ”€β”€ application
β”œβ”€β”€ data
β”‚Β Β  β”œβ”€β”€ datasources
β”‚Β Β  β”‚Β Β  └── db
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ dao
β”‚Β Β  β”‚Β Β      β”‚Β Β  β”œβ”€β”€ teacher_dao.dart
β”‚Β Β  β”‚Β Β      β”‚Β Β  └── teacherextends_dao.dart
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ database.dart
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ database.g.dart
β”‚Β Β  β”‚Β Β      └── database_service.dart
β”‚Β Β  └── repository
β”‚Β Β      β”œβ”€β”€ teacher_db.dart
β”‚Β Β      └── teacherextends_db.dart
β”œβ”€β”€ domain
β”‚Β Β  β”œβ”€β”€ entities
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ teacher.dart
β”‚Β Β  β”‚Β Β  └── teacher_extends.dart
β”‚Β Β  └── models
β”‚Β Β      └── user.dart
β”œβ”€β”€ main.dart
β”œβ”€β”€ presentation
β”‚Β Β  β”œβ”€β”€ providers
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ database.dart
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ teacher
β”‚Β Β  β”‚Β Β  β”‚Β Β  β”œβ”€β”€ teacher_async_notifier.dart
β”‚Β Β  β”‚Β Β  β”‚Β Β  β”œβ”€β”€ teacher_repository.dart
β”‚Β Β  β”‚Β Β  β”‚Β Β  └── teacher_stream.dart
β”‚Β Β  β”‚Β Β  └── teacherextends
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ teacherext_async_notifier.dart
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ teacherext_repository.dart
β”‚Β Β  β”‚Β Β      └── teacherext_stream.dart
β”‚Β Β  β”œβ”€β”€ screens
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ form.dart
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ home_drawer.dart
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ teacher
β”‚Β Β  β”‚Β Β  β”‚Β Β  β”œβ”€β”€ teacher_async.dart
β”‚Β Β  β”‚Β Β  β”‚Β Β  β”œβ”€β”€ teacher_list.dart
β”‚Β Β  β”‚Β Β  β”‚Β Β  β”œβ”€β”€ teacher_search.dart
β”‚Β Β  β”‚Β Β  β”‚Β Β  └── teacher_stream.dart
β”‚Β Β  β”‚Β Β  └── teacherextends
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ teacherext_async.dart
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ teacherext_list.dart
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ teacherext_search.dart
β”‚Β Β  β”‚Β Β      └── teacherext_stream.dart
β”‚Β Β  └── widgets
β”‚Β Β      β”œβ”€β”€ navigation_bar.dart
β”‚Β Β      β”œβ”€β”€ save_form_button.dart
β”‚Β Β      β”œβ”€β”€ search_bar.dart
β”‚Β Β      └── teacher_widget.dart
└── shared
    └── utils
        └── constants.dart

🧠 Inspiració

El projecte ha estat dissenyat com a eina d’aprenentatge i exploraciΓ³ personal. No pretΓ©n ser una aplicaciΓ³ completa, sinΓ³ una base per entendre millor les eines i patrons.


✨ Vols col·laborar?

Les millores, idees o pull requests sΓ³n benvingudes!

About

Flutter example showing how to use Floor streams with Riverpod's StreamProvider and AsyncNotifierProvider, comparing inheritance vs non-inheritance models.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages