Browse Interactive Flutter

Retrieving Data from Shared Preferences in Flutter

Learn how to efficiently retrieve data stored in shared preferences in Flutter, handle null values, and optimize app performance.

11.2.3 Retrieving Data from Shared Preferences in Flutter

In mobile app development, persisting user data is crucial for creating a seamless user experience. Flutter provides a convenient way to store simple data using the shared_preferences package. This section focuses on retrieving data from shared preferences, ensuring that your app can access and utilize stored information effectively.

Accessing Stored Data

The shared_preferences package allows you to store and retrieve simple data types such as strings, integers, booleans, doubles, and string lists. To retrieve data, you use methods like getString, getInt, getBool, etc. These methods are straightforward and return the stored value associated with a given key.

Example Code for Retrieving Data

Let’s explore how to retrieve different types of data from shared preferences:

import 'package:shared_preferences/shared_preferences.dart';

// Function to retrieve a stored username
Future<String?> getUserName() async {
  final prefs = await SharedPreferences.getInstance();
  return prefs.getString('username');
}

// Function to retrieve a stored user age
Future<int?> getUserAge() async {
  final prefs = await SharedPreferences.getInstance();
  return prefs.getInt('user_age');
}

// Function to check if the user is logged in
Future<bool> isUserLoggedIn() async {
  final prefs = await SharedPreferences.getInstance();
  return prefs.getBool('is_logged_in') ?? false; // Default to false if not set
}

In the above code:

  • getUserName retrieves a string value associated with the key 'username'.
  • getUserAge retrieves an integer value associated with the key 'user_age'.
  • isUserLoggedIn retrieves a boolean value associated with the key 'is_logged_in', defaulting to false if the key does not exist.

Handling Null Values

When retrieving data, it’s essential to handle cases where the data might not exist. The shared_preferences methods return null if the key does not exist. To ensure your app behaves correctly, you can provide default values.

Providing Default Values

Using the null-aware operator (??), you can specify a default value to use when the retrieved data is null. This practice prevents your app from encountering null-related errors and ensures a smooth user experience.

Future<String> getUserNameWithDefault() async {
  final prefs = await SharedPreferences.getInstance();
  return prefs.getString('username') ?? 'Guest'; // Default to 'Guest'
}

Future<int> getUserAgeWithDefault() async {
  final prefs = await SharedPreferences.getInstance();
  return prefs.getInt('user_age') ?? 18; // Default to 18
}

Using Retrieved Data

Once you have retrieved data from shared preferences, you can use it to customize your app’s UI or behavior. For example, you might display a personalized greeting using the user’s name or adjust app settings based on user preferences.

Example: Customizing UI Based on Retrieved Data

void displayGreeting() async {
  String userName = await getUserNameWithDefault();
  print('Welcome, $userName!');
}

In this example, the displayGreeting function retrieves the user’s name and prints a welcome message. If the username is not set, it defaults to “Guest.”

Performance Considerations

While retrieving data from shared preferences is relatively fast, repeated access can still impact performance, especially if done frequently. To optimize performance, consider caching retrieved data in memory when appropriate.

Caching Data

By storing retrieved data in a variable, you can minimize repeated calls to shared_preferences, enhancing your app’s efficiency.

String? cachedUserName;

Future<String> getCachedUserName() async {
  if (cachedUserName != null) {
    return cachedUserName!;
  }
  final prefs = await SharedPreferences.getInstance();
  cachedUserName = prefs.getString('username') ?? 'Guest';
  return cachedUserName!;
}

Visualizing the Data Retrieval Process

To better understand the data retrieval workflow, consider the following Mermaid.js diagram:

    graph LR
	    A[App] --> B[Need to Access Data]
	    B --> C[Call Get Function]
	    C --> D[shared_preferences.getX]
	    D --> E[Retrieve Data]
	    E --> F[Use Data in App]

This diagram illustrates the process of accessing data stored in shared preferences, from initiating a data retrieval request to using the retrieved data within the app.

Best Practices and Common Pitfalls

  • Use Default Values: Always provide default values when retrieving data to handle cases where the key does not exist.
  • Cache Data: Cache frequently accessed data to improve performance and reduce unnecessary calls to shared preferences.
  • Avoid Storing Sensitive Data: Shared preferences are not secure. Avoid storing sensitive information like passwords or personal data.
  • Keep Keys Consistent: Use consistent and descriptive keys to avoid conflicts and ensure data integrity.

Conclusion

Retrieving data from shared preferences is a fundamental aspect of Flutter app development, enabling you to maintain user preferences and settings across sessions. By understanding how to access and handle stored data, you can enhance your app’s functionality and user experience.

For further exploration, consider reading the official shared_preferences documentation and experimenting with different data types and use cases in your projects.

Quiz Time!

### What method would you use to retrieve a string value from shared preferences? - [x] getString - [ ] getInt - [ ] getBool - [ ] getDouble > **Explanation:** The `getString` method is used to retrieve a string value associated with a specific key from shared preferences. ### How can you handle a null value when retrieving data from shared preferences? - [x] Use the null-aware operator (??) to provide a default value - [ ] Ignore the null value - [ ] Use a try-catch block - [ ] Use a switch statement > **Explanation:** The null-aware operator (`??`) allows you to specify a default value to use when the retrieved data is `null`. ### What is a potential performance consideration when retrieving data from shared preferences? - [x] Repeated retrievals can impact performance - [ ] Data retrieval is always fast and efficient - [ ] Shared preferences automatically cache data - [ ] Data retrieval requires internet access > **Explanation:** Repeated retrievals from shared preferences can impact performance, so caching data when necessary is recommended. ### Which of the following is a best practice when using shared preferences? - [x] Provide default values for retrieved data - [ ] Store sensitive data like passwords - [ ] Use random keys for storing data - [ ] Retrieve data without checking for null > **Explanation:** Providing default values ensures that your app can handle cases where the data might not exist, avoiding null-related errors. ### What type of data can you store and retrieve using shared preferences? - [x] Strings, integers, booleans, doubles, and string lists - [ ] Complex objects and arrays - [ ] Images and files - [ ] Only strings > **Explanation:** Shared preferences support storing and retrieving simple data types such as strings, integers, booleans, doubles, and string lists. ### Why should you avoid storing sensitive data in shared preferences? - [x] Shared preferences are not secure - [ ] Shared preferences are too slow - [ ] Shared preferences do not support encryption - [ ] Shared preferences are difficult to use > **Explanation:** Shared preferences are not secure, so it's best to avoid storing sensitive information like passwords or personal data. ### How can you improve the performance of data retrieval from shared preferences? - [x] Cache frequently accessed data - [ ] Use a separate thread for each retrieval - [ ] Increase the app's memory usage - [ ] Use a different package for data storage > **Explanation:** Caching frequently accessed data in memory can improve performance by reducing the number of calls to shared preferences. ### What is the purpose of using descriptive keys in shared preferences? - [x] To avoid conflicts and ensure data integrity - [ ] To make the code more complex - [ ] To increase the app's performance - [ ] To store more data > **Explanation:** Using consistent and descriptive keys helps avoid conflicts and ensures data integrity when storing and retrieving data. ### What happens if you try to retrieve a non-existent key from shared preferences without providing a default value? - [x] The method returns null - [ ] The app crashes - [ ] A default value is automatically used - [ ] An exception is thrown > **Explanation:** If a key does not exist, the method returns `null` unless a default value is provided using the null-aware operator. ### True or False: Shared preferences require internet access to retrieve data. - [ ] True - [x] False > **Explanation:** Shared preferences store data locally on the device and do not require internet access to retrieve data.