developer who has been in Montréal for almost 2 years, and I’m passionate about Flutter and development in general. 💻 I have 4 years of experience as a developer, and I primarily work with Flutter in my free time 😎 Théo AUGUST Taugust8
is the language used for Flutter. (OOP) • It allows developers to build natively compiled applications for mobile, web, and desktop from a single codebase. • Active and growing community. • Regular updates and support from Google. • Last stable version : 3.16.4
Holds frequently accessed data. • Goal: ◦ Speeds up access to common information. ◦ Lowers data retrieval time. • Functionality: ◦ Quick access for apps. ◦ Reduces need for external server requests. What’s the cache ?
access. ◦ Reduced loading times for frequently used data. • Bandwidth Savings: ◦ Fewer network requests due to local data availability. ◦ Lower bandwidth costs. • User Experience Enhancement: ◦ Immediate response to user interactions. ◦ Shorter wait times for a smoother experience. • Offline Capability: ◦ Access data without an internet connection. Benefits of data caching Optimizing performance and user experience
• Ease of use: User-friendly, supports custom types. • Performance: Enhances app efficiency with quick data access. • Security concern: Provides an encryption feature to securely store sensitive data What is Hive ? https://pub.dev/packages/hive
Similar to tables in SQL databases. ◦ However, they don't have a predefined structure. ◦ Capable of containing a variety of data types. • Applicability: ◦ Ideal for both small and large-scale applications. ◦ For simple apps, a single box may suffice. ◦ For complex applications, multiple boxes can be used for better data organization. • Process: ◦ 'Open Box' process: Boxes must be opened before use. ◦ This action loads the data from local storage to memory. ◦ Ensures quick and efficient data access.
objects and the database storage. They define how your custom Dart objects are stored and retrieved from the Hive database. • Use serialization process. • Provide a generator adapter based on annotations. Hive Adapters
is on par with Hive when it comes to read performance. SQLite performs much worse. Hive greatly outperforms SQLite and SharedPreferences when it comes to writing or deleting. Hive is definitely one of the best packages to date for local data storage in Flutter. Hive > SharedPreferences > SQLite
• We take the API url as key • We take the response as the value • We check if the value already exist in the cache • If it not exist we made the call How to cache data from an API ?
case of a weather app we want to have fresh data, so we need to have a refresh mechanism. • Implement an expiration mechanism using a timestamp or duration to track the freshness of cached data. • When fetching data, check if the cache has expired based on the defined expiration criteria. • If the data is expired, make a new API call to obtain fresh data. • Update the cache with the newly fetched and fresh data. Because we need to care about fresh data… It's not so simple…
• Define complex Dart classes to represent the structure of the API data you want to store. These classes can have relationships between them, lists, nested objects, etc. • Utilize relationships in Hive to manage associations between different data classes. For instance, if you have a User class and a Post class, you can use relationships to link posts to a specific user. • Create separate adapters for each complex data class. Each adapter is responsible for the serialization and deserialization of data for its respective class. If it’s more complex And chances are high...
not limited to JSON data fetched from APIs. • Images Matter Too ◦ Images often constitute a significant portion of app content. • Frequent Repetition ◦ Many images are reused across different screens or scenarios.