Testing Debugging And Monitoring In Flutter :
Making Production-Ready apps in Flutter :
In this post, I will be discussing some of the points which should be kept in mind while developing an app. It is always a good practice to write apps that are scalable and maintainable.
I will be covering the following topics in this article:
- Setting up the development/production environment
- Best practices for writing clean code
- Testing, debugging, and monitoring
- Distribution of the app through CI/CD
Setting up the environment
- Git Version control and branching strategy
- Version control allows you to keep track of any file changes and identify differences in your code. It is a handy tool while working in a team. But it can become chaotic very quickly if everyone starts committing changes to the master branch. Thus, there should be specific branches defined for different purposes.
- A straightforward convention is to have three branches :
- master: The production-ready code goes here.
- develop: The code on which final tests are done before merging to master branch
- feature-specific branch: These are the branches with the name of the feature that is currently being worked on.
Best practices for writing clean code :
- No matter how great your app is if you can’t understand and debug the code you wrote a few days ago, it is of no use. Everyone should follow at least a basic architecture and separate the business logic from the UI.
- A breakdown of the architecture is as follows:
- UI: It is the section which the user interacts with. This section takes input from the user and sends it to the Provider. Validation of the data can be done here.
- Provider: This section consists of any state management that you want to use. In the above diagram, I have used the provider package.
- Repository(Domain): The repository in the domain layer is an abstract class containing all the methods that the Provider requires.
- Repository(Data): The repository in the data layer is the implementation of the abstraction. This layer decides whether the data required by the user should be fetched from the server or the local cache.
- Remote: As the name suggests, it handles all the server requests.
- Local: This manages all the tasks related to storing data offline and fetching data from the local database whenever required.
Error Handling is one of the most crucial aspects of any app. Errors and crashes can significantly affect your userbase. You can solve this problem by using data types like either.
Dartz: Return Left when error, Right when success. Left and Right are object/data holders.
- Testing, Debugging, and Monitoring
- Testing is essential so that you can identify and correct bugs before delivering the app to the users. Manual testing becomes harder as your app grows. Flutter offers three categories of test:
- Unit Test: To test for a single function, method, or class. There should be a bunch of unit tests as these tests are fast to execute. All unit tests can be carried out in a CI environment.
- Widget Test: To test a single widget; these tests are slower than the unit tests and should be carried out locally. Try not to run them in a CI environment as they will take up a lot of your build minutes.
- Integration Test: To test the complete app or a large part of it; these tests should strictly be carried out locally as they are very slow and not ideal for CI environments.
- Testing also ensures no one breaks the previously working code. A CI workflow could be set up to run the tests whenever there is a pull request to the development branch.
The dart DevTools offers a wide variety of tools and features to help debug Flutter apps. I am fairly new to dev tools, but I like the flutter inspector, which lets to test out different layout parameters without changing the actual code. You can also monitor network requests that your app makes without needing to print them out manually inside the app.
Deploying your app on the play store or the app store is not the end. You need to keep track of bugs and errors which escapes the development phase. Firebase crashlytics offer an easy way to monitor how your app behaves on the user’s phone. You can record the error and even log them.
- Distribution through CI/CD
Why is it important?
- If you push updates for your app frequently, CI/CD can help you automated various tasks so that you don’t have to waste a lot of time.
- Detection of error as early as possible; You can also identify the location of the error easily.
- Faster Release Rate. CI/CD continuously merges codes and deploys them to production after thorough testing, keeping the code in a release-ready state.
- Increases productive time; As CI/CD automates a lot of tasks, developers can spend more time on development.
- Next post learning flutter dartz.
The flutter tutorial is a website that bring you the latest and amazing resources of code. All the languages codes are included in this website. The languages like flutter, android, java,kotlin etc.with the help of this languages any user can develop the beautiful application
For more information about Flutter. visit www.fluttertutorial.in