title: Prepolulated SQLite database, on first launch is always empty - Flutter
body: <p>Every time my app on her first launch or when the database is deleted, a copy of my prepopulated database is copied from the assets folder, after copying the app is restarted (if the database was deleted, but if it was the first time launching it will not restart) and this error always occurs:</p>
  <blockquote>
  <p>RangeError (index): Invalid value: Valid value range is empty: 0</p>
  </blockquote>
  <p>It happens because the widgets down on the widget tree need a list, but the database supposedly has no values on it.</p>
  <p>But if I do a hot restart the database now has values and works normally</p>
  <p>Part of the db_helper.dart responsible to copy the database and check if it already exists</p>
  <pre><code> Future startDB() async {
      final dbDir = await getDatabasesPath();
      final dbPath = join(dbDir, &quot;visit_sps.db&quot;);
  
      bool exist = await databaseExists(dbPath);
  
      if (exist) {
        print(&quot;db ja existe&quot;);
      } else {
        print(&quot;a criar copia dos assets&quot;);
        try {
          await Directory(dirname(dbPath)).create(recursive: true);
        } catch (_) {}
  
        ByteData data = await rootBundle.load(&quot;assets/database/visit_sps.db&quot;);
        List&lt;int&gt; bytes =
            data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
        await File(dbPath).writeAsBytes(bytes, flush: true);
  
        print(&quot;db copiada&quot;);
      }
      openDatabase(dbPath);
    }
  </code></pre>
  <p>My FutureBuilder and code to delete the database if the app was reinstalled and a older database is still present on the user device, using the Shared Preferences Package - pagina_principal.dart</p>
  <pre><code>Future checkFirstSeen() async {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      bool _seen = (prefs.getBool('seen') ?? false);
  
      if (_seen) {
        DBHelper().startDB();
      } else {
        await prefs.setBool('seen', true);
        DBHelper().deleteDB();
      }
    }
  
  return FutureBuilder(
        future: DBHelper().getLocais(), //Obter todos os Locais
        builder: (context, AsyncSnapshot&lt;List&lt;Local&gt;&gt; snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            List&lt;Local&gt; locais = snapshot.data;
            return Scaffold(
  </code></pre>
  <p>If I print the locais length is 0, but after hot restarting is now the correct number of elements on my database.</p>
  <p>GetLocais()</p>
  <pre><code> Future&lt;List&lt;Local&gt;&gt; getLocais() async {
      try {
        final db = await database;
        final List&lt;Map&lt;String, dynamic&gt;&gt; maps = await db.query('local');
        return List.generate(
          maps.length,
          (i) {
            return Local.fromMap(maps[i]);
          },
        );
      } catch (ex) {
        print(ex);
        return new List&lt;Local&gt;();
      }
    }
  </code></pre>
  <p>What I think is the database is getting done being copied only after the getLocais() is done</p>
  <p>I would really appreciate if someone could help, I am still new to Flutter ,if you didn't understand my explanation, I am sorry because English is not my main language!</p>