- Популярные видео
- Авто
- Видео-блоги
- ДТП, аварии
- Для маленьких
- Еда, напитки
- Животные
- Закон и право
- Знаменитости
- Игры
- Искусство
- Комедии
- Красота, мода
- Кулинария, рецепты
- Люди
- Мото
- Музыка
- Мультфильмы
- Наука, технологии
- Новости
- Образование
- Политика
- Праздники
- Приколы
- Природа
- Происшествия
- Путешествия
- Развлечения
- Ржач
- Семья
- Сериалы
- Спорт
- Стиль жизни
- ТВ передачи
- Танцы
- Технологии
- Товары
- Ужасы
- Фильмы
- Шоу-бизнес
- Юмор
How can I keep a variable value inside an isolate/FlutterBackgroundService?
Hello everyone! I hope this video has helped solve your questions and issues. This video is shared because a solution has been found for the question/problem. I create videos for questions that have solutions. If you have any other issues, feel free to reach out to me on Instagram: https://www.instagram.com/ky.emrah
Below, you can find the text related to the question/problem. In the video, the question will be presented first, followed by the answers. If the video moves too fast, feel free to pause and review the answers. If you need more detailed information, you can find the necessary sources and links at the bottom of this description. I hope this video has been helpful, and even if it doesn't directly solve your problem, it will guide you to the source of the solution. I'd appreciate it if you like the video and subscribe to my channel!How can I keep a variable value inside an isolate/FlutterBackgroundService?
I'm currently working on implementing an isolate with a notification handler in Flutter.
Flutter
It's already working, I have a MQTT listener that shows a notification using AwesomeNotification with 2 actions for accepting or rejecting the notification.
MQTT
AwesomeNotification
When a notification is accepted, I want to make an API call to update the record related to that notification.
To make it a bit easy for me, I included some details with the MQTT payload that I need to use to make the API call, I set that detail to a variable so I can use it later on.
MQTT
The problem is that when the app reaches AwesomeNotification's onActionReceivedMethod, the variable's value is already gone.
AwesomeNotification's onActionReceivedMethod
I think the reason for this is because, down the line, the executing code is already outside the scope of the service/isolate so the variable can't be reached already.
service/isolate
I then tried to use SharedPreferences, but a key doesn't get updated as fast as I wanted/expected it to be. Now, I'm at a loss as to how I could keep a bit of value around so I could proceed with the process.
SharedPreferences
Here's my current code:
import 'dart:async';
import 'dart:convert';
import 'dart:developer';
import 'dart:isolate';
import 'dart:ui';
import 'package:baraco_frontend/helpers/api/request_vehicle_change_api_helper.dart';
import 'package:baraco_frontend/helpers/sharedPref/shared_pref_helper.dart';
import 'package:flutter/material.dart';
import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:intl/intl.dart';
import 'package:mqtt_client/mqtt_client.dart';
import 'package:vibration/vibration.dart';
import 'package:awesome_notifications/awesome_notifications.dart';
import '../globals.dart';
import '../models/user.dart';
import 'mqtt_helper.dart';
final _service = FlutterBackgroundService();
String notifTxt = 'Waiting for notification';
const String approveKey = 'approve';
const String rejectKey = 'reject';
const String approveLabel = 'Approve';
const String rejectLabel = 'Reject';
String _requestVehicleChangeId = '';
ReceivePort processRequestVehicleChangeRecvPort = ReceivePort('processRequestVehicleChangeRecv');
Future void initializeService() async{
log('initializeService');
await _service.configure(
androidConfiguration: AndroidConfiguration(
autoStart: true,
onStart: _onServiceStart,
isForegroundMode: true,
),
iosConfiguration: IosConfiguration(
autoStart: true,
onForeground: _onServiceStart,
onBackground: _onServiceIosBackground,
),
);
await initializeLocalNotifications();
await initializeIsolateReceivePort();
startListeningNotificationEvents();
}
@pragma('vm:entry-point')
Future void _onServiceStart(ServiceInstance service) async {
DartPluginRegistrant.ensureInitialized();
log('service started');
if (service is AndroidServiceInstance) {
service.on('setAsForeground').listen((ev) {
service.setAsForegroundService();
});
service.on('setAsBackground').listen((ev) {
service.setAsBackgroundService();
});
}
service.on('stopService').listen((ev) {
service.stopSelf();
});
MqttHelper().connect().then((val) async {
var user = User.fromJson( jsonDecode(await SharedPrefHelper().getPrefWithKey( Globals.USER_PREF_KEY )) );
MqttHelper().send('hi from the app', user.mqttClientId);
MSource of the question:
https://stackoverflow.com/questions/79031816
Question and source license information:
https://meta.stackexchange.com/help/licensing
https://stackoverflow.com/
Видео How can I keep a variable value inside an isolate/FlutterBackgroundService? канала Emrah KAYA
Below, you can find the text related to the question/problem. In the video, the question will be presented first, followed by the answers. If the video moves too fast, feel free to pause and review the answers. If you need more detailed information, you can find the necessary sources and links at the bottom of this description. I hope this video has been helpful, and even if it doesn't directly solve your problem, it will guide you to the source of the solution. I'd appreciate it if you like the video and subscribe to my channel!How can I keep a variable value inside an isolate/FlutterBackgroundService?
I'm currently working on implementing an isolate with a notification handler in Flutter.
Flutter
It's already working, I have a MQTT listener that shows a notification using AwesomeNotification with 2 actions for accepting or rejecting the notification.
MQTT
AwesomeNotification
When a notification is accepted, I want to make an API call to update the record related to that notification.
To make it a bit easy for me, I included some details with the MQTT payload that I need to use to make the API call, I set that detail to a variable so I can use it later on.
MQTT
The problem is that when the app reaches AwesomeNotification's onActionReceivedMethod, the variable's value is already gone.
AwesomeNotification's onActionReceivedMethod
I think the reason for this is because, down the line, the executing code is already outside the scope of the service/isolate so the variable can't be reached already.
service/isolate
I then tried to use SharedPreferences, but a key doesn't get updated as fast as I wanted/expected it to be. Now, I'm at a loss as to how I could keep a bit of value around so I could proceed with the process.
SharedPreferences
Here's my current code:
import 'dart:async';
import 'dart:convert';
import 'dart:developer';
import 'dart:isolate';
import 'dart:ui';
import 'package:baraco_frontend/helpers/api/request_vehicle_change_api_helper.dart';
import 'package:baraco_frontend/helpers/sharedPref/shared_pref_helper.dart';
import 'package:flutter/material.dart';
import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:intl/intl.dart';
import 'package:mqtt_client/mqtt_client.dart';
import 'package:vibration/vibration.dart';
import 'package:awesome_notifications/awesome_notifications.dart';
import '../globals.dart';
import '../models/user.dart';
import 'mqtt_helper.dart';
final _service = FlutterBackgroundService();
String notifTxt = 'Waiting for notification';
const String approveKey = 'approve';
const String rejectKey = 'reject';
const String approveLabel = 'Approve';
const String rejectLabel = 'Reject';
String _requestVehicleChangeId = '';
ReceivePort processRequestVehicleChangeRecvPort = ReceivePort('processRequestVehicleChangeRecv');
Future void initializeService() async{
log('initializeService');
await _service.configure(
androidConfiguration: AndroidConfiguration(
autoStart: true,
onStart: _onServiceStart,
isForegroundMode: true,
),
iosConfiguration: IosConfiguration(
autoStart: true,
onForeground: _onServiceStart,
onBackground: _onServiceIosBackground,
),
);
await initializeLocalNotifications();
await initializeIsolateReceivePort();
startListeningNotificationEvents();
}
@pragma('vm:entry-point')
Future void _onServiceStart(ServiceInstance service) async {
DartPluginRegistrant.ensureInitialized();
log('service started');
if (service is AndroidServiceInstance) {
service.on('setAsForeground').listen((ev) {
service.setAsForegroundService();
});
service.on('setAsBackground').listen((ev) {
service.setAsBackgroundService();
});
}
service.on('stopService').listen((ev) {
service.stopSelf();
});
MqttHelper().connect().then((val) async {
var user = User.fromJson( jsonDecode(await SharedPrefHelper().getPrefWithKey( Globals.USER_PREF_KEY )) );
MqttHelper().send('hi from the app', user.mqttClientId);
MSource of the question:
https://stackoverflow.com/questions/79031816
Question and source license information:
https://meta.stackexchange.com/help/licensing
https://stackoverflow.com/
Видео How can I keep a variable value inside an isolate/FlutterBackgroundService? канала Emrah KAYA
Комментарии отсутствуют
Информация о видео
26 октября 2024 г. 4:22:24
00:01:12
Другие видео канала
