Search code examples
fluttersqflite

How can I send data SQLite (offline) to server MySQL using Flutter?


When there is no signal I use offline mode by storing data to the device (SQLite). After there is a signal I try to send the data to the Mysql server.

How can I send data from SQLite (offline mode) to MySQL server?


Solution

  • create a DB similar to your sqflite db's table in a remote server. then, create a rest api using your desired language(php is easy to start). then, when the app is connected to internet, use HTTP client to send the data to the remote server.

    you can use a code like below for the post data call:

    Future<dynamic> post(String url, {Map headers, body, encoding}) {
    print(url);
    print(body);
    return http
        .post(BASE_URL+url, body: body, headers: headers, encoding: encoding)
        .then((http.Response response) {
      final String res = response.body;
      final int statusCode = response.statusCode;
    
      print(res);
    
      if (statusCode < 200 || statusCode > 400 || json == null) {
        throw new Exception("Error while fetching data");
      }
      return _decoder.convert(res);
    });
    }