Creating multiple Flutter nofitications with flutter_local_notification - multithreading

Currently I'm using a loading page to avoid having a big lag when the user is being redirected to the home page while the notifications are being created on the same thread (the UI thread which leads to lots of dropped frames).
I tried using the compute dart function but the issue is that this function requires using static methods and you can't pass it objects.
So I would appreciate some hints on how to use a thread to create the notifications.
PS: in the worst scenario the app is creating 7*24 notifications(24 for each day of the week) which is slow even on high end devices.
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter/material.dart';
import '../pages/home_page/home_page.dart';
import 'package:timezone/data/latest.dart' as tz;
import 'package:timezone/timezone.dart' as tz;
import 'data.dart';
import 'package:flutter_native_timezone/flutter_native_timezone.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:auto_size_text/auto_size_text.dart';
class NotificationLoading extends StatefulWidget {
const NotificationLoading({Key? key}) : super(key: key);
#override
_NotificationLoadingState createState() => _NotificationLoadingState();
}
class _NotificationLoadingState extends State<NotificationLoading> {
#override
void initState() {
super.initState();
manageNotifications();
}
Future<void> manageNotifications() async {
await Future.delayed(
const Duration(seconds: 1),
); // Let time to build the widget
await Notifications(ctx: context).manageNotifications();
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: const [
CircularProgressIndicator(),
Padding(
padding: EdgeInsets.fromLTRB(0, 15, 0, 0),
child: AutoSizeText(
"Loading notifs",
style: TextStyle(fontSize: 30),
),
)
],
),
),
);
}
}
class Notifications {
static const channelId = "coolID";
static const channelName = "cool";
Data data = Data();
int id = 0;
BuildContext ctx;
Notifications({required this.ctx});
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
// check if notifications are already setup, if not setup notifications
// otherwise notifications only need to be changed inside the timer_page
Future<void> manageNotifications() async {
final prefs = await SharedPreferences.getInstance();
bool isNotificationSetup = prefs.getBool('isNotificationSetup') ?? false;
if (!isNotificationSetup) {
await _initialization();
await _scheduleNotifications();
await prefs.setBool('isNotificationSetup', true);
Navigator.pop(ctx);
await Navigator.push(
ctx,
MaterialPageRoute<void>(builder: (context) => const HomePage()),
);
}
}
Future<void> _initialization() async {
const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('app_icon');
const InitializationSettings initializationSettings =
InitializationSettings(
android: initializationSettingsAndroid,
);
await flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: _selectNotification);
}
// Schedule notifications based on user settings
Future<void> _scheduleNotifications() async {
// Init the time zone, needed for notification scheduling
tz.initializeTimeZones();
final String? timeZoneName = await FlutterNativeTimezone.getLocalTimezone();
tz.setLocalLocation(tz.getLocation(timeZoneName!));
await data.getData();
int delta = (data.endTime.minute + data.endTime.hour * 60) -
(data.startTime.minute + data.startTime.hour * 60);
double interval = delta / data.reminderNumber;
data.checkedDays.forEach((day, values) {
if (values[1]) {
double minute = data.startTime.minute + (data.startTime.hour * 60);
for (int reminder = 0; reminder < data.reminderNumber; reminder++) {
int tmpHour = (minute - minute % 60) ~/ 60;
int tmpMinute = (minute.round()) % 60;
_createScheduledNotification(
_nextInstanceOfDayHourMinute(tmpHour, tmpMinute, values[0]), id);
minute += interval;
id++;
}
}
});
}
// Create a scheduled notification
void _createScheduledNotification(tz.TZDateTime time, int id) async {
await flutterLocalNotificationsPlugin.zonedSchedule(
id,
AppLocalizations.of(ctx)!.notificationTitle,
AppLocalizations.of(ctx)!.notificationMessage,
time,
const NotificationDetails(
android: AndroidNotificationDetails(
'weekly notification channel id',
'New citation message',
channelDescription:
'Notifications for new citations configured in the timer page.',
sound: RawResourceAndroidNotificationSound('notification_sound'),
groupKey: "meditation invitation",
),
),
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
matchDateTimeComponents: DateTimeComponents.dayOfWeekAndTime);
}
// Find next instance DateTime object
tz.TZDateTime _nextInstanceOfHourMinute(int hour, int minute) {
final tz.TZDateTime now = tz.TZDateTime.now(tz.local);
tz.TZDateTime scheduledDate =
tz.TZDateTime(tz.local, now.year, now.month, now.day, hour, minute);
if (scheduledDate.isBefore(now)) {
scheduledDate = scheduledDate.add(const Duration(days: 1));
}
return scheduledDate;
}
// Find next instance DateTime object
tz.TZDateTime _nextInstanceOfDayHourMinute(int hour, int minute, int day) {
tz.TZDateTime scheduledDate = _nextInstanceOfHourMinute(hour, minute);
while (scheduledDate.weekday != day) {
scheduledDate = scheduledDate.add(const Duration(days: 1));
}
return scheduledDate;
}
// triggered function when the user tap on a notification
void _selectNotification(String? payload) async {
if (payload != null) {
debugPrint('notification payload: $payload');
}
await Navigator.push(
ctx,
MaterialPageRoute<void>(builder: (context) => const HomePage()),
);
}
}

You can't handle too many notifications at time in iOS platform. Instead of that you need again set or schedule notifications after few old notification received otherwise old notifications overlaps & it will not schedule.
You can check this link & my answer I hope it will help you.
Dart/Flutter is single threaded and not possible multi threading. As each isolate has its own memory,space and everything. To make it work like multi threaded you have to use isolates and the communication will be used through ports by sending message to one another. If you not want to use Future you can use isolates.
Read
https://medium.com/flutter-community/flutter-threading-5c3a7b0c065f
https://www.tutorialspoint.com/dart_programming/dart_programming_concurrency.htm
https://pub.dev/packages/threading
So you can use Future Or isolate.

Great François. You already in a good path. I guess i could give you some hints to accomplish what you want.
First of all Flutter require top level functions to run isolates so you have to put it outside of a class.
To transfer data to an isolate it must be serialized. In my example i use jsonEncode to send it as a string and parse it with jsonDecode to retrieve as a dynamic list inside the isolate runner.
When i wrote this code i read about some limitations of the use of plugins inside isolate (I don`t know about the current state). So i found a solution for this using Flutter Isolate plugin (https://pub.dev/packages/isolate_handler)
I use the function killScheduleNotifications as a way to control when the code is running and avoid to create duplications. I always cancel all the scheduled and recreate it every time.
For reference (https://gist.github.com/taciomedeiros/50472cf94c742befba720853e9d598b6)
final IsolateHandler isolateHandler = IsolateHandler();
void scheduleNotificationsIsolate(String _reminders) async {
await new Future.delayed(new Duration(milliseconds: 500));
// ... (describe settings)
flutterLocalNotificationsPlugin.initialize(
settings,
onSelectNotification: onSelectNotification,
);
await flutterLocalNotificationsPlugin.cancelAll();
List<dynamic> _remindersParsed = jsonDecode(_reminders);
for (// iterate over your entities to show the message) {
int generatedId = id ?? random.nextInt(1000000000);
await flutterLocalNotificationsPlugin.schedule(
generatedId,
title,
message,
scheduledNotificationDateTime,
platformSpecifics,
payload: payload,
);
}
killCurrentScheduleNotifications();
}
startScheduleNotifications(String _remindersAsString) {
killCurrentScheduleNotifications();
isolateHandler.spawn<String>(
entryPoint,
name: "scheduleNotifications",
onReceive: scheduleNotificationsIsolate,
onInitialized: () => isolateHandler.send(
_remindersAsString,
to: "scheduleNotifications",
),
);
}
void killCurrentScheduleNotifications() {
if (isolateHandler.isolates.containsKey('scheduleNotifications'))
isolateHandler.kill('scheduleNotifications');
}
void entryPoint(Map<String, dynamic> context) {
final messenger = HandledIsolate.initialize(context);
messenger.listen((message) {
messenger.send(message);
});
}

Related

how to stream user query instead of awaiting it

so, I am using a search Delegate to allow user search, however, when user is inputting data, it's really slow, it lags and skips frames as they type, because my search delegate loops through a list that contains about 2000 objects and checks user query against each object's title and main text, making about 4000 search criteria in total, this was causing the lag, so to solve this, I simply spawned a new isolate to handle the suggestions building, as such
Future<List<Data>> allData(String query) async {
List<Data> suggestions;
List<Data> myData(String message) {
return allData.where((data) {
final result = data.dataText.replaceAll(RegExp('[^A-Za-z0-9 ]'), '').toLowerCase();
final result1 = data.dataText.replaceAll(RegExp('[^A-Za-z0-9]'), '').toLowerCase();
final result2 = data.dataText.toLowerCase();
final result3 = data.dataText.replaceAll("th'incarnate", "the incarnate").toLowerCase();
final input = query.toLowerCase();
final result4 = data.dataTitle.toLowerCase();
final result5 = data.dataTitle.replaceAll(RegExp('[^A-Za-z0-9 ]'), '').toLowerCase();
final result6 =data.dataTitle.replaceAll(RegExp('[^A-Za-z0-9]'), '').toLowerCase();
return result.contains(input) ||
result1.contains(input) ||
result2.contains(input) ||
result3.contains(input) ||
result4.contains(input) ||
result5.contains(input) ||
result6.contains(input);
}).toList();
}
query.isEmpty
? suggestions = []
: suggestions = await compute<String, List<Data>>(
myData,
"",
);
if (query.isNotEmpty && suggestions.isEmpty) {
return [
Data(
DataTitle: "No Results",
DataText: "No Results",
),
];
} else {
return suggestions;
}
}
then in buildSuggestions I did this
#override
Widget buildSuggestions(BuildContext context) {
return FutureBuilder<List<Data>>(
future: DataBrain().allData(query),
builder: (context, snapshot) {
List<Data> suggestions;
query.isEmpty ? suggestions = [] : suggestions = snapshot.data!;
return ListView.builder(
itemCount: suggestions.length,
itemBuilder: (context, suggestIndex) {
final suggestion = suggestions[suggestIndex];
if (snapshot.connectionState == ConnectionState.done) {
if (suggestion.dataText.toLowerCase() == "no results") {
return const ListTile(
title: Text("No results"),
subtitle: Text("Please check for spelling mistakes"),
);
} else {
return ListTile(
title: Text(suggestion.dataTitle),
trailing: Text(
'${suggestion.dataBookName} ${suggestion.dataBookNumber == 0 ? '30a' : suggestion.dataBookNumber}',
style: const TextStyle(
fontStyle: FontStyle.italic,
fontWeight: FontWeight.w300,
),
),
onTap: () {
close(context, suggestion);
},
);
}
} else {
return const ListTile(
title: Text('Loading...'),
);
}
});
});
}
it works fine without lags, however now, the suggestions don't build as user types, it waits till user is no longer typing, for like 2 seconds, then it builds suggestions, I think this is because it's a future and in a future builder, I'm not sure why. I'm thinking of turning it into a stream rather and listen to query change and build immediately instead of awaiting for two seconds after user stops querying, is there any workaround, what's a good way to eliminate the delay, i need it to filter the list and build suggestions with each keystroke, as it used to do before i moved it to a new isolate and made it a future

Flutter CircularProgressIndicator on top of syncfusion_calendar

I am struggling with correct futurebuilder positioning for last few days. Im using syncfusion_calendar package to display json data from my API, where i call a new reqest to API every time user changes calendars month. The problem is that user is not being told about ongoing data downlad and i would love to do that by showing CircularProgressIndicator instead of calendar while its loading.
my pubspec file just in case :
name: flutter_viaapp_startmenu
description: A new Flutter application.
# The following line prevents the package from being accidentally published to
# pub.dev using `pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1
environment:
sdk: ">=2.7.0 <3.0.0"
dependencies:
firebase_core: ^0.5.0+1
cloud_firestore: ^0.14.4
firebase_messaging: ^7.0.3
firebase_in_app_messaging: 0.2.3
webview_flutter: ^1.0.7
flutter_staggered_grid_view: ^0.3.3
easy_localization: ^2.3.2
intl: ^0.16.1
http: ^0.12.2
syncfusion_flutter_calendar: ^18.3.51
shared_preferences: ^0.5.12
flutter_local_notifications: ^3.0.3
flutter_localizations:
sdk: flutter
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.0
dev_dependencies:
flutter_test:
sdk: flutter
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
assets:
- assets/menu.jpg
- assets/welcome.jpg
- assets/translations/en.json
- assets/translations/lv.json
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.
# For details regarding adding assets from package dependencies, see
# https://flutter.dev/assets-and-images/#from-packages
# To add custom fonts to your application, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts from package dependencies,
# see https://flutter.dev/custom-fonts/#from-packages
My main calendar file :
import 'dart:async';
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:syncfusion_flutter_calendar/calendar.dart';
import 'package:intl/intl.dart';
class Lecture_graph extends StatefulWidget {
Lecture_graph({Key key}) : super(key: key);
#override
State<StatefulWidget> createState() => _MyLecturesGraphState();
}
class _MyLecturesGraphState extends State<Lecture_graph> {
Future<List<Lecture>> _future;
List<Lecture> lectures;
DateTime _selectedDate = new DateTime.now();
List<LectureTime> _times;
//TODO make this empty after SO post
var coursecode = "IT3";
#override
void initState() {
_selectedDate = new DateTime(
_selectedDate.year,
_selectedDate.month,
15,
_selectedDate.hour,
_selectedDate.minute,
_selectedDate.second,
_selectedDate.millisecond,
_selectedDate.microsecond);
_future = downloadData();
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(coursecode + " | Lectures"),
actions: [
LecturesNavigationControls(),
],
),
body: lectureGraphList());
}
Future<String> _checkSavedCourse() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String _coursecode = prefs.getString('savedCourse');
if (_coursecode == "" || _coursecode == null) {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => CourseSelectionPage()),
);
return null;
} else {
return _coursecode;
}
}
Widget lectureGraphList() {
return FutureBuilder<List<Lecture>>(
future: _future,
builder: (BuildContext context, AsyncSnapshot<List<Lecture>> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: SizedBox(
child: const Expanded(
child: Center(child: const CircularProgressIndicator())),
width: 100,
height: 100,
),
);
} else {
if (snapshot.hasError)
return Center(child: Text('Error: ${snapshot.error}'));
else
return showLectures(lectures);
}
},
);
}
//THIS FUNCTION IS CALLED EVERY TIME TO DOWNLOAD NEW DATA FROM API
Future<List<Lecture>> downloadData() async {
//get saved course
if (coursecode == "" || coursecode == null) {
coursecode = await _checkSavedCourse();
//debug
print('courscode recieved from sharedprefs');
}
//if there is no date selected, select today
if (_selectedDate == null) _selectedDate = new DateTime.now();
//build request URL
var requestURL =
'https://lekcijas.va.lv/lekcijas_android/getMonthLectures.php?date=' +
DateFormat('yyyy-MM').format(_selectedDate) +
"&breaks&program=" +
coursecode;
//wait for response
var response = await http.get(requestURL);
var data = json.decode(response.body)["result"];
//clear array after each request
if (lectures != null) lectures.clear();
try {
//create lectures from json response
lectures = List<Lecture>.from(data.map((x) => Lecture.fromJson(x)));
_getDataSource(lectures);
} catch (e) {
print(e.toString());
}
return Future.value(lectures);
}
Widget showLectures(List<Lecture> lectures) {
return Card(
child: Row(
children: [
Expanded(
child: SfCalendar(
view: CalendarView.month,
firstDayOfWeek: 1,
onViewChanged: (ViewChangedDetails details) {
if (_selectedDate.month != details.visibleDates[15].month) {
WidgetsBinding.instance.addPostFrameCallback((_) {
_selectedDate = details.visibleDates[15];
setState(() {
//CALENDAR MONTH CHANGE IS CALLED HERE
downloadData();
});
});
}
},
dataSource: LectureTimeDataSource(_times),
monthViewSettings: MonthViewSettings(
appointmentDisplayMode:
MonthAppointmentDisplayMode.indicator,
showAgenda: true,
agendaStyle: AgendaStyle(
appointmentTextStyle:
TextStyle(color: Colors.black))),
showNavigationArrow: true))
],
),
);
}
void _getDataSource(List<Lecture> lectures) {
var lectureTimes = <LectureTime>[];
lectures.forEach((element) {
lectureTimes.add(LectureTime(
(element.classroom + " " + element.lecture),
DateTime.parse(element.datums + " " + element.start),
DateTime.parse(element.datums + " " + element.end),
hexToColor(element.color),
false));
});
setState(() {
_times = lectureTimes;
});
}
}
class LecturesNavigationControls extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Row(
children: <Widget>[
IconButton(
//TODO find normal icon
icon: const Icon(Icons.wheelchair_pickup),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => CourseSelectionPage()),
);
},
),
],
);
}
}
class LectureTimeDataSource extends CalendarDataSource {
LectureTimeDataSource(List<LectureTime> source) {
appointments = source;
}
#override
DateTime getStartTime(int index) {
return appointments[index].from;
}
#override
DateTime getEndTime(int index) {
return appointments[index].to;
}
#override
String getSubject(int index) {
return appointments[index].eventName;
}
#override
Color getColor(int index) {
return appointments[index].background;
}
#override
bool isAllDay(int index) {
return appointments[index].isAllDay;
}
}
class LectureTime {
LectureTime(
this.eventName, this.from, this.to, this.background, this.isAllDay);
String eventName;
DateTime from;
DateTime to;
Color background;
bool isAllDay;
}
Color hexToColor(String code) {
return new Color(int.parse(code.substring(1, 7), radix: 16) + 0xFF000000);
}
class Lecture{
final String programs;
final String lecture;
final String lecturer;
final String start;
final String end;
final String classroom;
final String color;
final String datums;
Lecture({this.programs, this.lecture, this.lecturer, this.start, this.end, this.classroom, this.color, this.datums});
factory Lecture.fromJson(Map<String, dynamic> json) {
return Lecture(
programs: json['nodala'] as String,
lecture: json['kurss'] as String,
lecturer : json['lektors'] as String,
start: json['sakums'] as String,
end: json['beigas'] as String,
classroom: json['nosaukums'] as String,
color: json['iela'] as String,
datums: json['datums'] as String,
);
}
}
class LectureTime {
LectureTime(
this.eventName, this.from, this.to, this.background, this.isAllDay);
String eventName;
DateTime from;
DateTime to;
Color background;
bool isAllDay;
}
please use "IT3" string as coursecode for API.
API request url example here
You can add a variable called downloadingData and set it to false by default. Then, before calling downloadData() set it to true and when the function finishes, set it back to false.
Finally, inside the build method:
child: downloadingData ? CircularProgressIndicator() : SfCalendar(...)
Based on the provided information and code snippet, we have checked, and your requirement is “Showing the CircularProgressIndicator when loading calendar”. We have prepared a simple sample for loading circular progress indicator with loading online data to the calendar. Please find the sample from the following link,
Sample link:
https://www.syncfusion.com/downloads/support/directtrac/312448/ze/minimum_appointmentduration298839661.zip
Also, we have a KB document for loading the online data to Flutter calendar with a simple loading text message. In the same way, you will use CircularProgressIndicator.
KB link: https://www.syncfusion.com/kb/11568/how-to-load-the-json-data-online-to-the-flutter-event-calendar-sfcalendar-appointments
We hope that the above sample and KB helps you. Please let us know if you need further assistance.

how to implement string list from _ListItem to assetPath?

i've been working with wallpaper app.
i manage to call the path list for wallpaper,
i can make this work on image card list but i can't manage to do it work with assetPath for WallpaperManager
Please help, is there any way to do it?
Future<void> setWallpaperFromAsset() async {
setState(() {
_wallpaperAsset = "Loading";
});
String result;
String assetPath = ('Load List here');
You can add a parameter assetPath to setWallpaperFromAsset
Future<void> setWallpaperFromAsset(String assetPath) async {
setState(() {
_wallpaperAsset = "Loading";
});
String result;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
result = await WallpaperManager.setWallpaperFromAsset(
assetPath, WallpaperManager.HOME_SCREEN);
} on PlatformException {
result = 'Failed to get wallpaper.';
}
}
And you register the onPressed callback with:
RaisedButton.icon(
onPressed: () => setWallpaperFromAsset(item),
icon: Icon (Icons.wallpaper, size: 20),
label: Text('homescreen')),
)

Loading URL with unique Android or iOS device id in webview

I know how to get the device unique id in flutter, but when I add it as a parameter to url and call it with webview, I can't get the result I want. When I call the following function, webview is loaded with empty device id parameter because it is working ASYNC and not yet completed and not returning any value.
Future<String> _getId() async {
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
if (Theme.of(context).platform == TargetPlatform.iOS) {
IosDeviceInfo iosDeviceInfo = await deviceInfo.iosInfo;
return iosDeviceInfo.identifierForVendor; // unique ID on iOS
} else {
AndroidDeviceInfo androidDeviceInfo = await deviceInfo.androidInfo;
return androidDeviceInfo.androidId; // unique ID on Android
}
}
The webview loading function is below;
String webViewUrl;
_getId().then((id) {
webViewUrl = "http://websiteblabla.com/?deviceid="+id;
return WebviewScaffold(
url: webViewUrl,
withJavascript: true,
withZoom: false
);
}
How can I get URL with unique Android or iOS device id in webview when it is ready?
You can create Future function or method which will return WebviewScaffold widget when everything is ready. And the use it with FutureBuilder. It would look something like this.
FutureBuilder(
future: _buildWebview(),
builder: (context, snapshot) {
if(!snapshot.hasData) return Center(
child: CircularProgressIndicator(),
);
return snapshot.data;
},
),
Future<Widget> _buildWebview() async {
await _getId().then((id) {
webViewUrl = "http://websiteblabla.com/?deviceid="+id;
return WebviewScaffold(
url: webViewUrl,
withJavascript: true,
withZoom: false
);
}
}

Executing nested WaterfallDialogs - nodejs

I'm trying to build a requirement system for order dialogs in our bot, so that we can reuse the main structure for different procedures.
enum DialogIds {
// Necessary Ids
oauthPrompt = "oauthPrompt",
// Requirement dialogs
itemWaterfallDialog = "itemWaterfallDialog",
// Reset Dialogs
summaryWaterfallDialog = "summaryWaterfallDialog",
// All other prompts
unrecognizedItemPrompt = "unrecognizedItemPrompt",
beneficiaryConfirmPrompt = "beneficiaryConfirmPrompt",
askBeneficiaryPrompt = "askBeneficiaryPrompt",
reasonPrompt = "reasonPrompt",
orderConfirm = "orderConfirm"
}
export class OrderDialog extends ComponentDialog {
private responseManager: ResponseManager;
private requirementManager: RequirementManager;
private luisResult: RecognizerResult | undefined = undefined;
// TODO: get userState and ConversationState
constructor(
private service: BotServices,
telemetryClient: BotTelemetryClient
) {
super(OrderDialog.name);
this.initialDialogId = OrderDialog.name;
// Response manager serving OrderResponses.json
this.responseManager = new ResponseManager(["fr-fr"], [OrderResponses]);
const routeWaterfallDialog: ((
sc: WaterfallStepContext
) => Promise<DialogTurnResult>)[] = [
this.route.bind(this)
];
this.telemetryClient = telemetryClient;
this.addDialog(
new WaterfallDialog(this.initialDialogId, routeWaterfallDialog)
);
/**
* Order specific dialogs and requirements
*/
const itemWaterfallDialog: WaterfallDialog = new WaterfallDialog(
DialogIds.itemWaterfallDialog,
[this.itemStep.bind(this), this.itemEndStep.bind(this)]
);
this.addDialog(itemWaterfallDialog);
const reqs = [
new Requirement<string>("claimant", false, undefined),
new Requirement<string>(
"item",
true,
undefined,
itemWaterfallDialog,
DialogIds.itemWaterfallDialog
),
];
// Create requirement manager for this dialog
this.requirementManager = new RequirementManager(reqs);
// Add all the prompt
this.addDialog(new ConfirmPrompt(DialogIds.beneficiaryConfirmPrompt));
this.addDialog(new TextPrompt(DialogIds.unrecognizedItemPrompt));
this.addDialog(new TextPrompt(DialogIds.askBeneficiaryPrompt));
this.addDialog(new TextPrompt(DialogIds.reasonPrompt));
this.addDialog(new ConfirmPrompt(DialogIds.orderConfirm));
}
/**
* We save the token, query graph is necessary and
* execute the next dialog if any, if not we'll
* execute the summary waterfallDialog.
* #param sc context
*/
async route(sc: WaterfallStepContext): Promise<DialogTurnResult> {
this.requirementManager.set("claimant", 'nothing');
let next = this.requirementManager.getNext();
while (next) {
await sc.beginDialog(next.dialogId!);
// Execute summary if there are no elements left
if (!this.requirementManager.getNextBool()) {
await sc.beginDialog(DialogIds.summaryWaterfallDialog);
}
next = this.requirementManager.getNext();
}
return sc.endDialog();
}
/**
* ITEM
* #param sc
*/
async itemStep(sc: WaterfallStepContext): Promise<DialogTurnResult> {
// Couldn't recgonize any item
if (this.luisResult!.entities.length === 0) {
await sc.context.sendActivity(
this.responseManager.getResponse(
OrderResponses.itemNotRecognized
)
);
// prompt user for the item again
return await sc.prompt(
DialogIds.unrecognizedItemPrompt,
this.responseManager.getResponse(OrderResponses.rePromptItem)
);
}
const entities = this.luisResult!.entities as generalLuis["entities"];
if (entities.PhoneItem || entities.ComputerItem) {
const item = entities.PhoneItem
? entities.PhoneItem
: entities.ComputerItem;
if (item) {
this.requirementManager.set("item", item[0][0]);
}
}
return await sc.next();
}
async itemEndStep(sc: WaterfallStepContext): Promise<DialogTurnResult> {
// Save result from itemStep(prompt triggered) if any
if (sc.result) {
await sc.context.sendActivity(
this.responseManager.getResponse(OrderResponses.thanksUser)
);
// retrieve item from result and save it
const item = sc.result as string;
this.requirementManager.set("item", item);
}
return sc.endDialog();
}
}
The line
const result = await sc.beginDialog(next.dialogId!);
Is starting a WaterfallDialog declared in the constructor of the Dialog, and the route method is also inside a general waterfallDialog.
The problem is that, when one of the child dialogs prompts the user, the code doesn't wait for the user response, and because of the way the route works it will call the same dialog again(if a value on an object is not filled it will call the indicated dialog, that's what the requirement manager does).
If saving the return from that line, we can see that the status is "waiting", how could I fix it, or should I create independent dialogs for each requirement, and not just waterfallDialogs?
Thanks.

Resources