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

5,536

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);
});
}
Share:
5,536
Author by

Dwi Nur Rohman

Updated on December 08, 2022

Comments

  • Dwi Nur Rohman 11 minutes

    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?

    • Diego Garcia
      Diego Garcia almost 4 years
      Are you using any webserver in the middle of your app and the database ? usually its done that way, your app sends / gets the requests from server, so wen offline just cache it to sqllite after get online just send it all to the server that will persist to the mysql database.
    • Dwi Nur Rohman almost 4 years
      Ok i ll try.. many thanks
  • Dwi Nur Rohman almost 4 years
    Is rest api like when we insert data to mysql server?
  • Diego Garcia
    Diego Garcia almost 4 years
    nope a rest api is just a url link accomplish the format of its specification, that maps the post request to the method in an application running at the server, that will insert your posted data to mysql server.
  • Diego Garcia
    Diego Garcia almost 4 years
    Basically its the same thing that I've told you in my previous comment :)
  • Star Lut almost 4 years
    @DwiNurRohman, as Diago Garcia explained, rest api is a medium by which you send data to server, and the data gets inserted to mysql. you cant actually insert data any mysql database, from android. you need a medium for that. theres lots of example in the internet. check please. here is a [link[(codeofaninja.com/2017/02/create-simple-rest-api-in-ph‌​p.html)
  • Dwi Nur Rohman almost 4 years
    @diegogarcia and Starlut
  • Dwi Nur Rohman almost 4 years
    Sorry offline for some day..I mean whether the rest api that is used is the same as the rest api when we will insert data with 1 row of data, whereas when using sqflite there are several rows of data want to insert
  • Star Lut almost 4 years
    what do you mean by 1row of data? did you mean column?
  • Dwi Nur Rohman almost 4 years
    no, I mean 1 row of data like we will enter data into the table
  • Star Lut almost 4 years
    Sorry my bad. Kind of the same. You send a list of object to bring entered in db. Same is done in rest API. @Dwi Nur Rohman
  • Star Lut almost 4 years
    Rest API is basically your dbhelper class for server
  • Star Lut almost 4 years
    no problem :) if i solved your problem, dont forget to accept the answer :P :)