Keshav Aditya R P
Keshav Aditya R P

Reputation: 3221

Flutter connecting to a database in server

I'm new to flutter and i really want to know, is there a way to connect to a database server (for instance mysql) in flutter rather using firebase. I'm working on a smart parking system project where i need to insert the latitude and longitude of the parking area which is free into the database which is created in server and retrieve it whenever user requests for it. It would be great if anyone gives solution for above mentioned problem (Flutter with database).

Upvotes: 2

Views: 22651

Answers (2)

Luis Negron
Luis Negron

Reputation: 29

Try using sqflite. It's a package you can include in your Flutter app that allows you to persist data to the local device. You will need to use the path_provider as well. Here is the link to the repository on Github https://github.com/tekartik/sqflite

Upvotes: 2

Ashton Thomas
Ashton Thomas

Reputation: 17849

Since Flutter is just a UI Framework, topics such as persistence and databases may be out of scope or may depend on the use case.

Flutter UI's can persist data (application state) for short periods of time in a manner that is really only useful for the purposes of creating a good User Experience (is this button click? is it green? etc.)

For persisting more useful data outside of the application and on the actual device, you may want to consider the Shared Preferences Plugin for Flutter.

Wraps NSUserDefaults (on iOS) and SharedPreferences (on Android), providing a persistent store for simple data. Data is persisted to disk automatically and asynchronously.

Now, if you require persisting data in any centralized manner (e.g. RDMS, Firebase, or any data persistence service) your options are:

  • Persistence options that have a Flutter plugin (e.g. Firestore, Firebase)
  • Build your own service layer using HTTP, gRPC that talks to some backend service that provides access to a data store. You can do this with Express, Rails, CloudFunctions, etc.
  • As for connecting directly to a database such as MySQL, I don't see why you couldn't do that (maybe there is some technical limitation), but this would be a very bad idea in any practical situations as (unlike Firebase/Firestore) you won't be able to protect your data store once any client application has write access.

It sounds like you need a central read/write data store, so your best bet may be to host a server that provides access to a database while exposing an API to Flutter for which you can use dart:io to make requests.

Upvotes: 6

Related Questions