title: Flutter: avoid UI freeze when massive Database operation is in progress
body: <p><strong>UPDATE</strong> (15 july 2020)</p>
  <blockquote>
  <p><em>mFeinstein's</em> response, for now, is the only answer which gives me the first acceptable solution.</p>
  </blockquote>
  <hr />
  <p><strong>QUESTION</strong></p>
  <p>I have to ask you what is the best approach for doing what i'm trying to do:</p>
  <ol>
  <li>Calling a web service in async mode</li>
  <li>Parsing response</li>
  <li>Performing massive database operations</li>
  </ol>
  <p>All of this without freezing progress animation, like indeterminate progress bar.</p>
  <p>There isn't problem at first and second point. Problem occurs at third, when a massive database insert is in act. And i don't understand yet how is the right way for implementing this stuff.</p>
  <p>Some pseudo piece of code for clarify</p>
  <p><strong>UI (Dialog is shown and progress bar runs...)</strong></p>
  <pre><code>void callWS() async {
      MyProgressDialog _dialog = DialogHelper.showMyProgressDialog(_context, &quot;Data&quot;, &quot;Loading...&quot;);
      await getDataFromService();
      _dialog.close();
    }
  </code></pre>
  <p><strong>CONNECTION (Freeze doesn't occur on progress bar)</strong></p>
  <pre><code>   static Future&lt;void&gt; getDataFromService() async {
      String uri = MY_URI;
      String wsMethod = MY_WS_METHOD;
      String wsContract = MY_WS_CONTRACT;
  
      SoapObject myRequest = SoapObject.fromSoapObject(namespace: my_namespace, name: wsMethod);
  
      MyConnectionResult response = await _openMyConnection(myRequest, uri, wsContract, wsMethod);
      if (response.result == MyResultEnum.OK) {
        await _parseResponse(response.data);
      }
    }
  </code></pre>
  <p><strong>DATABASE (Freeze occurs on progress bar)</strong></p>
  <pre><code>  static Future&lt;void&gt; _parseResponse(xml.XmlElement elements) async {
      Database db = await MyDatabaseHelper.openConnection();
      db.transaction((tx) async {
        Batch batch = tx.batch();
        for (xml.XmlElement oi in elements.children) {
          int id = int.parse(oi.findElements(&quot;ID&quot;).first.text);
          String name = oi.findElements(&quot;NAME&quot;).first.text;
  
          DatabaseHelper.insertElement(
            tx,
            id: id,
            name: name,
          );
        }
        batch.commit(noResult: true);
      });
    }
  </code></pre>
  <p><strong>NOT WORKING ALTERNATIVE</strong></p>
  <p>I saw &quot;compute&quot; function approach too, but it seems there is a problem in <code>sqflite package</code>, when i call a db operation. For example:</p>
  <pre><code>  static Future&lt;void&gt; performDelete() async {
      Database db = await openMyConnection();
      compute(_performDeleteCompute, db);
    }
  
    static void _performDeleteCompute(Database db) async {
      db.rawQuery(&quot;DELETE MYTABLE&quot;);
    }
  
  Console error:'
  -&gt; Unhandled Exception: Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized. 
  -&gt; If you are running an application and need to access the binary messenger before runApp() has been called (for example, during plugin initialization),
  then you need to explicitly call the WidgetsFlutterBinding.ensureInitialized() first.
  -&gt; error defaultBinaryMessenger.&lt;anonymous closure&gt; (package:flutter/src/services/binary_messenger.dart:76:7)
      #1      defaultBinaryMessenger (package:flutter/src/services/binary_messenger.dart:89:4)
      #2      MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:140:62)
      #3      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:146:35)
      #4      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:329:12)
      #5      invokeMethod (package:sqflite/src/sqflite_impl.dart:17:13)
      #6      SqfliteDatabaseFactoryImpl.invokeMethod (package:sqflite/src/factory_impl.dart:31:7)
      #7      SqfliteDatabaseMixin.invokeMethod (package:sqflite_common/src/database_mixin.dart:287:15)
      #8      SqfliteDatabaseMixin.safeInvokeMethod.&lt;anonymous closure&gt; (package:sqflite_common/src/database_mixin.dart:208:43)
      #9      wrapDatabaseException (package:sqflite/src/exception_impl.dart:7:32)
      #10     SqfliteDatabaseFactoryImpl.wrapDatabaseException (package:sqflite/src/factory_impl.dart:27:7)
      #11     SqfliteDatabaseMixin.safeInvokeMethod (package:sqflite_common/src/database_mixin.dart:208:15)
      #12     SqfliteDatabaseMixin.txnRawQuery.&lt;anonymous closure&gt; (package:sqflite_common/src/database_mixin.dart:394:36)
      #13     SqfliteDatabaseMixin.txnSynchronized.&lt;anonymous closure&gt; (package:sqflite_common/src/database_mixin.dart:327:22)
      #14     BasicLock.synchronized (package:synchronized/src/basic_lock.dart:32:26)
      #15     SqfliteDatabaseMixin.txnSynchronized (package:sqflite_common/src/database_mixin.dart:323:33)
      #16     SqfliteDatabaseMixin.txnRawQuery (package:sqflite_common/src/database_mixin.dart:393:12)
      #17     SqfliteDatabaseExecutorMixin._rawQuery (package:sqflite_common/src/database_mixin.dart:126:15)
      #18     SqfliteDatabaseExecutorMixin.rawQuery (package:sqflite_common/src/database_mixin.dart:120:12)
      #19     DatabaseHelper._performDeleteCompute(package:flutter_infocad/Database/DatabaseHelper.dart:368:8)'
  </code></pre>
  <p>And also explicitly calling the <code>WidgetsFlutterBinding.ensureInitialized()</code> as first in runApp(), as suggested in the error log, nothing happens.</p>