SonarSource Rules
  • Products

    In-IDE

    Code Quality and Security in your IDE with SonarQube Ide

    IDE extension that lets you fix coding issues before they exist!

    Discover SonarQube for IDE

    SaaS

    Code Quality and Security in the cloud with SonarQube Cloud

    Setup is effortless and analysis is automatic for most languages

    Discover SonarQube Cloud

    Self-Hosted

    Code Quality and Security Self-Hosted with SonarQube Server

    Fast, accurate analysis; enterprise scalability

    Discover SonarQube Server
  • SecretsSecrets
  • ABAPABAP
  • AnsibleAnsible
  • ApexApex
  • AzureResourceManagerAzureResourceManager
  • CC
  • C#C#
  • C++C++
  • CloudFormationCloudFormation
  • COBOLCOBOL
  • CSSCSS
  • DartDart
  • DockerDocker
  • FlexFlex
  • GitHub ActionsGitHub Actions
  • GoGo
  • HTMLHTML
  • JavaJava
  • JavaScriptJavaScript
  • JSONJSON
  • JCLJCL
  • KotlinKotlin
  • KubernetesKubernetes
  • Objective CObjective C
  • PHPPHP
  • PL/IPL/I
  • PL/SQLPL/SQL
  • PythonPython
  • RPGRPG
  • RubyRuby
  • RustRust
  • ScalaScala
  • ShellShell
  • SwiftSwift
  • TerraformTerraform
  • TextText
  • TypeScriptTypeScript
  • T-SQLT-SQL
  • VB.NETVB.NET
  • VB6VB6
  • XMLXML
  • YAMLYAML
Dart

Dart static code analysis

Unique rules to find Bugs, Vulnerabilities, Security Hotspots, and Code Smells in your DART code

  • All rules 126
  • Vulnerability4
  • Bug15
  • Security Hotspot8
  • Code Smell99
 
Tags
    Impact
      Clean code attribute
        1. "mounted" should be checked before using a "BuildContext " after an async operation

           Bug
        2. "void" variables should not be assigned a value

           Bug
        3. Constant patterns should not be used with type literals

           Bug
        4. "is!" should be used instead of "!is"

           Bug
        5. Getters should not be recursive

           Bug
        6. Nullable type parameter values should not be null checked with `!`

           Bug
        7. Regular expressions should be syntactically valid

           Bug
        8. Color definitions should be valid

           Bug
        9. "const" modifier should not be redundant

           Bug
        10. Setters should not declare return types

           Bug
        11. Inappropriate collection calls should not be made

           Bug
        12. Unnecessary equality checks should not be made

           Bug
        13. The original exception object should be rethrown

           Bug
        14. "==" operator and "hashCode()" should be overridden in pairs

           Bug
        15. Jump statements should not occur in "finally" blocks

           Bug

        "mounted" should be checked before using a "BuildContext " after an async operation

        intentionality - logical
        reliability
        Bug
        • unpredictable

        The mounted property should be checked before using a BuildContext, after every async operation.

        Why is this an issue?

        How can I fix it?

        More Info

        Asynchronous operations making use of futures or async/await constructs decouple code from its execution. This means that, unlike sequential code, asynchronous code will not necessarily execute in the order it is written.

        For example, in a sequence of await calls, the code following the first await will not be executed until the future returned by the first await completes, and the same is true for the subsequent await call.

        Future<Data> fetchData(String uri) async { /* ... */ }
        
        void sequenceOfAsyncOperations() async {
          final data1 = await fetchData('https://example.com/data1');
          // Executed after the future returned by the 1st fetchData completes
          final data1Info = data1.info;
          print(data1Info);
          final data2 = await fetchData('https://example.com/data2');
          // Executed after the future returned by the 1st fetchData completes
          final data2Info = data2.info;
          print(data2Info);
          // ...
        }
        

        Because the execution suspends on awaiting, and the thread is potentially assigned to another task, all variables which are in scope need to be stored, so that they can be restored when the result of the future is available and the execution can resume. This is done behind the scenes by the Dart compiler, in a way that makes the code appear to be executed sequentially from a syntactical perspective, whereas it is actually not.

        When all variables used in an async function are local to that function, and not visible to the outside world, there is no concern of using them after a future completes. This is not the case, however, for BuildContext, which is typically passed to WidgetBuilder functions, and can be accessed via State.context.

        That means that, the BuildContext instance may change internal state between the time the future has been created and awaited, and the time it completes and the execution restores. That time interval is generally referred to as an "async gap".

        Dart provides a property of BuildContext called mounted, which can be used to check if the widget associated with the BuildContext is still in the widget tree, and can be safely accessed in the current context.

        @override
        Widget build(BuildContext context) => OutlinedButton(
            onPressed: () async {
                await Future<void>.delayed(const Duration(seconds: 1));
                if (context.mounted) {
                  // The context is mounted, so it's safe to use it
                  Navigator.of(context).pop();
                }
            },
            child: const Text('Delayed pop'),
          );
        

        What is the potential impact?

        If the BuildContext is used across an async gap without checking the mounted property, the code may access a BuildContext instance made invalid by the Flutter framework, which can lead to runtime errors and unpredictable behaviors.

          Available In:
        • SonarQube CloudDetect issues in your GitHub, Azure DevOps Services, Bitbucket Cloud, GitLab repositories
        • SonarQube ServerAnalyze code in your
          on-premise CI
          Developer Edition
          Available Since
          10.8

        © 2008-2025 SonarSource SA. All rights reserved.

        Privacy Policy | Cookie Policy | Terms of Use