To preface this i am new to all of this and don\'t need step by step information, just seeing what is possible.
I\'ve been toying around with connecting my flutter/d
For Android we can use SQLite and for Flutter, we have equivalent SQFlite that support both IOS and Android.
And there are other best solutions like Firebase database, Firestore and you can also create API and access through HTTP. Also you can store data in a JSON file.
For you to continue on these may help:
Using mysql in flutter.
Flutter connecting to a database in server.
I think this answer is required here:
Few days ago I had to dealt with a existing MySQL database and there was no way to do it with Dart except over network requests(there is a sqljocky package which is Dart 2 incompatible). Read best options here.
Here Im going to show an example for a simple fetch data using Flutter http request with simple PHP & MySQL.
Note: This is may not 100% secure.
If you are doing in local server, you need to have server(ex: apache) running and MySQL(you can use XAMPP to get both).
Step 2: create a folder in htdocs(in XAMP). Next, inside that file create file called conn.php. Then, add below snippet and change with your db credentials to connect to database.
<?php
$connection = new mysqli("localhost", "username", "password", "db_name");
if (!$connection) {
echo "connection failed!";
exit();
}
?>
Note: above snippet is a simple example and you can customize as you want.
Step 3: Create php file called fetch_data.php and copy paste below code snippet:
<?php
include("conn.php");
$queryResult = $connection->
query("SELECT * FROM your_table");//change your_table with your database table that you want to fetch values
$result = array();
while ($fetchdata=$queryResult->fetch_assoc()) {
$result[] = $fetchdata;
}
echo json_encode($result);
?>
Above code snippet will fetch the data from db table and display it after json_encode
.
Step 4: Open up your Flutter project or create one. Below is a code snippet that requesting for fetched data and display them using a ListView
.
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Login",
debugShowCheckedModeBanner: false,
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
List data = [];
@override
void initState() {
fetchData();
super.initState();
}
void fetchData() async {
final response = await http.get('http://10.0.2.2/fluttertest/fetch_data.php');
if (response.statusCode == 200) {
setState(() {
data = json.decode(response.body);
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: ListView.builder(
itemCount: data.length,
itemBuilder: (BuildContext context, int index) => ListTile(
title: Text(data[index]['title']),
)),
);
}
}
There is an important thing, you should keep in mind: 10.0.2.2
or your machine IP address(type ipconfig
in cmd to get your IP address) have to use because emulator itself localhost(127.0.0.1).
Note: I didn't use any model or advanced things to show you bare minimum example. If you want a great CRUD example, check my github repo and you can also do some pull requests because it's can extended.
If you want to insert or update data, you can use http.post()
and in php use INSERT
, UPDATE
sql statements(It's better using Prepared Statement to prevent sql injections).
Also you can read my article. There I have explained everything using a CRUD application.