Article

A Guide for Flutter Mobile Application Developers, Part VI: Testing and Security

Slide 16_9 – 7

In the final installment of this series for Flutter mobile application developers, we’ll dive into Testing and Security. 

While aiming for complete code coverage is optimal, it’s not always feasible. Security is paramount, particularly in handling user information.

Throughout the first five parts of this series, we’ve covered:

Understanding these topics isn’t just valuable—it’s essential for developers. Concerning the subject matter of this post, we must ensure the robustness of our code, facilitating optimization and easy recovery to a functional state when implementing new changes. 

Prioritizing security fosters trust among users, making them feel more secure while engaging with your application.

Flutter Testing 

Automated testing is crucial in Flutter development due to its multi-platform targeting, saving time and effort compared to manual testing.

There are three types of Automated testing:

  1. Unit Test – test a single function, method, or class
  2. Widget Test – (in other UI frameworks referred to as component test) test a single widget
  3. Integration Test – test a complete app or a large part of an application

Generally, a well-tested app has many unit and widget tests, tracked by code coverage, plus enough integration tests to cover all the important use cases. This is based on the fact that there are trade-offs between different kinds of testing, as seen below.

It’s essential to have tests for critical app functionality. Integration testing ensures seamless component interaction and testing on physical devices or emulators is vital for uncovering issues not evident in simulation environments.

Some general commands that help with testing and get cleaner code are:

  •  flutter analyze  – Analyzes the project’s Dart source code.
  •  flutter test –coverage  – Runs test and generates a coverage file
  •  dart run dlcov -c 90 –exclude-suffix .pb.dart,.pbenum.dart  – (This should be run after generating the coverage file) Checks if the coverage is 90 or higher and excludes the files with the extension “.pb.dart,.pbenum.dart” since these are generated files that we don’t want to include in the coverage. (you can change the coverage percentage and the excluded files/extensions, if any, for the given project)

These commands are commonly employed in scripts to verify whether newly implemented changes are suitable for merging into the existing working project post-approval from other developers. Utilizing these commands is integral to maintaining high standards within your codebase.

Flutter security

Security is crucial for mobile apps, especially considering the permissions and sensitive data they handle. Developers bear the responsibility of safeguarding the app’s security. Flutter, fortunately, provides robust security features, and here are some essential practices to uphold Flutter security standards.

Code Obfuscation

Code obfuscation is a technique that obscures your app’s binary code, making it challenging for humans to understand. It safeguards against reverse engineering, protecting sensitive information like API keys and function/class names. Obfuscation is a security measure to deter reverse engineering attacks.

This example builds an apk android release version with the –obfuscate flag along with the —split-debug-info flag.

Prevent Background Snapshots

When your app runs in the background, it typically displays its last state in the task switcher, which can be useful for multitasking. However, there are situations where you want to protect sensitive information from being visible in the task switcher, like your bank account details. The secure_application Flutter package helps secure your app by preventing sensitive content from being displayed in the background when running in the task switcher.

Other Flutter security considerations

Authentication and Authorization

  • Use secure authentication methods and libraries to ensure that only authorized users can access your app’s features and data
  • Implement role-based access control (RBAC) to limit users’ permissions based on their roles.

Data Encryption

  • Encrypt sensitive data, such as user credentials and personal information, both in transit and at rest.
  • Utilize secure storage libraries to store sensitive data securely on the device.


Secure Network Communications

  • Use HTTPS to encrypt data transmitted between your app and servers.
  • Implement secure network protocols and avoid using plaintext communication.


Input Validation


Avoid Hardcoding Secrets

  • Store API keys, tokens, and sensitive information in environment variables or a secure configuration management system. Avoid hardcoding secrets in your code.

Security Updates

  • Keep dependencies, including Flutter and third-party packages, up to date to patch known security vulnerabilities.
  • Regularly review and update your app’s security libraries and practices.


Secure Authentication Tokens

  • Ensure that authentication tokens are adequately secured, stored, and validated.
  • Use standards like OAuth 2.0 for secure token-based authentication.


Secure File Handling

  • Be cautious when working with files, and use secure methods to store and access files to prevent unauthorized access or exposure of sensitive data.


Session Management

  • Implement secure session management to prevent unauthorized access to user data.
  • Use JSON Web Tokens (JWT) to manage session data securely.


Error Handling

  • Provide generic error messages to users and log detailed error information separately to avoid exposing sensitive data to potential attackers.

Permissions

  • Request only the permissions your app truly needs and avoid over-requesting permissions.
  • Follow Android and iOS best practices for requesting and handling permissions.


Secure Cloud Services

  • If your app relies on cloud services, ensure that these services are configured securely, with proper access controls and encryption.

Security Testing

  • Conduct regular security testing, including vulnerability scanning and penetration testing, to identify and remediate security issues.

User Education

  • Educate your users about best practices for security, such as creating strong passwords and being cautious with sharing personal information.

Compliances

  • Ensure your app complies with relevant privacy and security regulations and standards, such as GDPR for data protection.

Security Policies

  • Establish and document your development team’s security policies, procedures, and guidelines.

Are vetting Flutter mobile application developers? Honing your practice? Build a principle-driven workflow.

Testing and security are integral components of any application and to the practice of Flutter mobile application developers across the spectrum. Flutter provides libraries and commands to boost testing coverage and fortify security against potential threats.

In this post, we explored Flutter Testing and Security, highlighting several commands and flags to uphold high testing standards and strengthen security measures in projects.

By now, you’ve gained familiarity with Flutter and acquired numerous techniques, tips, and best practices, ensuring maintainability, readability, and overall code quality. I trust this series has been beneficial and sparked your curiosity to delve deeper into Flutter Application Development and its vast capabilities.

If you’re seeking Flutter mobile application developers for hire or want to learn more, contact us!

Ricardo Colin Picture

Ricardo Colin

Sr. Mobile Developer

Ricardo comes to Fresh with experience in creating websites and custom applications for smaller companies from when he was in college, with a focus on mobile development. After developing Android applications on Java, he transitioned to iOS using Swift. Then, once Flutter developed a cross-platform technology, Ricardo became intrigued and began using that framework for future applications.

Ricardo also enjoys guiding and helping other devs and peers with projects, previously acting as a part-time coach to students with Flutter and Firebase projects. In addition, Ricardo has two Flutter game applications in the App Store and the Play Store: “Math Circle Roulette,” which helps with practicing math problems, and “What’s My Number?” a guessing game that you can play with other friends or with a robot.

Ricardo loves to learn and talk about almost anything, especially about new technology and finding new opportunities to exchange knowledge. Outside of work, he loves to learn about new places to hike and will play soccer from time to time.