I am using flutter and developing a reminder app. I want to insert database after then notification created. These codes run the emulator. After installing the application on the real device, it does not insert. what could be the reason?
I call function IconButton pressed.
createOrganizerNotification(
taskName, time, taskDate, nt, sm, taskIcon);
And I create notification(success) and call insert table function.
void createOrganizerNotification(var taskName, var taskTime, var taskDate,
var notificationTitle, var seeMore, var taskIcon) async {
SharedPreferences preferences = await SharedPreferences.getInstance();
var email = preferences.getString("usermail");
var time = taskTime.split(':');
var date = taskDate.split('-');
taskIcon = taskIcon.split('"');
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: createUniqueId(),
channelKey: 'notification_channel',
title: notificationTitle,
body: taskDate ' - ' taskTime ' - ' taskName,
),
/*actionButtons: [
NotificationActionButton(
key: 'see_more',
label: seeMore,
)
],*/
schedule: NotificationCalendar(
day: int.parse(date[0]),
month: int.parse(date[1]),
year: int.parse(date[2]),
hour: int.parse(time[0]),
minute: int.parse(time[1])));
await DBHelper().insertOrganizerNotification(
email,
date[0].toString(),
date[1].toString(),
date[2].toString(),
time[0].toString(),
time[1].toString(),
taskName.toString(),
'created',
taskIcon[1].toString());
}
This is insert to database function.
Future<bool> insertOrganizerNotification(
String email,
String day,
String month,
String year,
String hour,
String minute,
String title,
String status,
String icon,
) async {
try {
final conn = await MySqlConnection.connect(
ConnectionSettings(
host: _host,
port: _port,
user: _user,
password: _password,
db: _db),
);
await conn.query(
'insert into OrganizerNotificaitonsList ( Email, Day, Month, Year, Hour, Minute, Title, Status, Icon ) values ( ?,?,?,?,?,?,?,?,?)',
[email, day, month, year, hour, minute, title, status, icon]);
await conn.close();
return true;
} catch (e) {
return false;
}
}
And this is my table structure.
Really I don't understand this bug. Because the emulator can insert the database successfully. Only real devices have this bug.
CodePudding user response:
Check the database connection settings and update the host, to an IP or domain. On the emulator the localhost will be allowed as device and database are on same network
var settings = new ConnectionSettings(
host: 'localhost',
port: 3306,
user: 'bob',
password: '*****',
db: 'mydb'
);
var conn = await MySqlConnection.connect(settings);
CodePudding user response:
Looks like internet permissions issue. add the following permission before the Application section, to the Manifest file of your app.
<uses-permission android:name="android.permission.INTERNET" />
Without internet permission your app won't be able to send or receive requests or data from the internet thus preventing SQL operations.
and if you are using a database on the local host it won't work on a device as the device will not be able to access it. so use a database on a cloud server.