Flutter Local Storage: SharedPreferences vs Hive Quiz Quiz

Challenge your understanding of Flutter local storage solutions, focusing on the usage, features, and differences between SharedPreferences and Hive. This quiz is ideal for Flutter app developers aiming to strengthen their knowledge of key-value and NoSQL storage methods in mobile apps.

  1. Basic Usage of SharedPreferences

    Which method should you use to store a simple integer value persistently using SharedPreferences?

    1. saveInteger
    2. putValue
    3. setInt
    4. addNumber

    Explanation: The correct method for saving an integer value in SharedPreferences is 'setInt', which specifically stores integer values. The other options, like 'addNumber', 'putValue', and 'saveInteger', are not part of the SharedPreferences API and would result in errors if used. Using the precise method name is essential for correct functionality.

  2. Choosing the Right Storage Solution

    If your Flutter app needs to store simple user settings like theme color or login status, which storage approach is most appropriate?

    1. Hive
    2. File System
    3. SharedPreferences
    4. SQLite

    Explanation: SharedPreferences is designed to store simple key-value pairs such as user settings efficiently, making it the best fit for this scenario. Hive is suitable for more complex structured data, while File System and SQLite are typically reserved for larger or relational datasets, not just basic app preferences.

  3. Data Structure Support in Hive

    When using Hive as a local storage database, which type of data structure does it allow you to save by default?

    1. Lists and Maps
    2. Files directly
    3. Only strings
    4. Only integers

    Explanation: Hive supports storing collections such as Lists and Maps by default, allowing flexible and complex data storage. The options 'Only strings' and 'Only integers' are too limited as Hive is more versatile. Hive does not directly store files, but rather the data representations.

  4. Initialization Requirement

    What must you do before using Hive in your Flutter app for the first time during app startup?

    1. Delete existing key-value pairs
    2. Initialize Hive with a directory path
    3. Import Dart math library
    4. Call setState in the main method

    Explanation: Hive requires initialization with a directory path to function correctly and determine where data should be stored. Importing Dart math library and calling setState in the main method are unrelated to local storage initialization. Deleting key-value pairs is not a requirement before using Hive.

  5. Type Limitation of SharedPreferences

    Which of the following data types can SharedPreferences NOT directly store?

    1. Boolean
    2. String
    3. Double
    4. Custom objects

    Explanation: SharedPreferences cannot natively store custom objects; it handles primitive types like String, Boolean, and Double. Attempting to save complex data structures, such as custom objects, requires additional serialization. The other options are all supported natively by SharedPreferences.

  6. Retrieving a Non-existent Key with SharedPreferences

    What will SharedPreferences return if you try to read a value for a key that does not exist?

    1. An exception
    2. Empty string
    3. Zero
    4. Null

    Explanation: If a key does not exist, SharedPreferences returns null by default, allowing you to check for its presence easily. It does not return zero or an empty string unless explicitly set, nor does it throw an exception in this scenario. Handling null values is important when working with missing keys.

  7. Encryption Support

    Which storage solution provides built-in support for local encryption of data out of the box?

    1. File System
    2. Hive
    3. SharedPreferences
    4. JSON encode

    Explanation: Hive supports encryption by providing a built-in mechanism for encrypting its data. SharedPreferences and File System do not offer native encryption features and would require additional layers. 'JSON encode' is not a storage solution, but merely a data serialization method.

  8. Clearing All Data in SharedPreferences

    If you want to remove all stored data from SharedPreferences at once, which method should you use?

    1. reset
    2. clear
    3. removeAll
    4. deleteDatabase

    Explanation: The 'clear' method is provided to remove all key-value pairs from SharedPreferences at once. 'removeAll' and 'reset' are not valid method names in this context. 'deleteDatabase' is unrelated and would apply to database storage, not key-value storage like SharedPreferences.

  9. Hive vs SharedPreferences Performance

    For which type of application would Hive generally provide better performance compared to SharedPreferences?

    1. Storing an integer counter
    2. Storing large lists of structured data
    3. Keeping only a user's login status
    4. Saving a single setting value

    Explanation: Hive is optimized for handling large amounts of structured or complex data efficiently, making it suitable for scenarios requiring lists or maps. For single values like a setting, login status, or an integer counter, SharedPreferences is more lightweight and appropriate.

  10. Synchronous Access in SharedPreferences

    When reading data from SharedPreferences in a Flutter app, how is the method typically designed?

    1. Needs a callback function
    2. Synchronous and returns instantly
    3. Requires a stream subscription
    4. Asynchronous and returns a Future

    Explanation: Most methods for reading SharedPreferences data in Flutter are asynchronous and return a Future because reading from disk may take some time. Access is not synchronous, does not require streams, and does not mandate a callback function for basic get operations. Using async methods ensures non-blocking behavior.