Flutter using plugins inside flutter workmanager plugin callbackDispatcher function - flutter

I try to use sqflite plugin inside background process with flutter workmanager. I have an SQLHelper class which for sqflite operations. I can use this helper inside application. But in this background process, it gives error as below
Exception has occurred:
MissingPluginException (MissingPluginException(No implementation found for method getDatabasesPath on channel com.tekartik.sqflite))
Probably I have to register plugin inside callbackDispatcher, but I'm not sure. How can I solve this problem?
void callbackDispatcher() {
Workmanager.executeTask((task, inputData) async {
SQLHelper db = SQLHelper();
ServerSetting sItem = await db.settingGetItem();
print(sItem.hidetriggermaintenance);
return Future.value(true);
});
}

you need to register sqflite plugin on native side, as stated here: https://github.com/tekartik/sqflite/blob/master/sqflite/doc/troubleshooting.md

Related

Open a screen when firebase notification received in flutter while app is background or terminated

want to create a screen that launch when a firebase notification is received while the app is in the background or terminated in flutter (like package callKeep ).please anyone help me create that.
You just need to handle its background handler method it will works.You need to initialize this method in main() method.
Future<void> backgroundHandler(RemoteMessage message) async {
debugPrint(message.toString());
debugPrint(message.data.toString());
debugPrint(message.notification!.title);
}
FirebaseMessaging.onBackgroundMessage(backgroundHandler);

Flutter url_launcher not working MissingPluginException

i want to launch google map when tap on button, nothing complicated. i found easy code also from google search. Pasting the code below for reference. i dont know it is the proper way to do.
class MapUtils {
MapUtils._();
static Future<void> openMap(double latitude, double longitude) async {
String googleUrl =
'https://www.google.com/maps/search/?api=1&query=$latitude,$longitude';
if (await canLaunch(googleUrl)) {
await launch(googleUrl);
} else {
throw 'Could not open the map.';
}
}
}
pubspec.yaml added -> url_launcher: ^6.0.17
i called this -> MapUtils.openMap(-33.2445107, 9.1530103)
Whenever i tap on the button return error as "Unhandled Exception: MissingPluginException(No implementation found for method launch on channel plugins.flutter.io/url_launcher)"
i tried to find solution, what i tried is
Flutter clean, flutter pub get.
Close everything reopen
uninstall app from emulator device.
close emulator and restart.
total computer restart.
it is nothing related url_launcher still i add the google map api to androidmanifest and appdelegate. Enables Api.
Lot of popel are asking about the same error. May be i am missing something todo.

Implementing pull-to-refresh in Flutter with Riverpod

I am new to Riverpod and trying to implement pull to refresh with Riverpod in my Flutter app and can't seem to find any good tutorial online. Has anyone implemented pull-to-refresh in their Flutter app while using Riverpod. I have looked for tutorials everywhere, but most I found was simple network request on app load. Thanks in advance!
using these dependencies for riverpod and network requests.
flutter_riverpod: ^0.12.1
dio: ^3.0.10
You can use context.refresh(yourProvider) in your refresh action. Works great.
In Riverpod 1.0.0 and above, you can use ref.refresh(yourProvider)
Turned out the working way of pull-to-refresh the UI using Riverpod (^1.0.0) is this:
Provider:
final yourFutureProvider = FutureProvider<T>(....);
Inside the Build method:
return RefreshIndicator(
onRefresh: () async {
return await ref.refresh(yourFutureProvider);
},
THIS WORKED FOR ME
Declare the provider
final sessionsProvider = FutureProvider<List<Session>>((ref) async {
final sessionServices = ref.read(sessionsServiesProvider);
return await sessionServices.getSessions();
});
add refresh indicator
RefreshIndicator(
onRefresh: () async => ref.refresh(sessionsProvider),
child: ...)

clear shared preferences from myBackgroundMessageHandler

I want to clear shared preferences when I send a FCM message and app is in background. Inside myBackgroundMessageHandler method I am calling a method to clear them.
static Future<dynamic> myBackgroundMessageHandler(
Map<String, dynamic> message) {
clearPreferences();
}
static void clearPreferences() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.clear();
}
I am getting the following error:
Unhandled Exception: MissingPluginException(No implementation found
for method getAll on channel plugins.flutter.io/shared_preferences)
step 1)
go to Application.kt/Application.java (in my case is kotlin)
step 2)
add these line into Application class (in kotlin)
if (!registry!!.hasPlugin("io.flutter.plugins.sharedpreferences")) {
SharedPreferencesPlugin.registerWith(registry!!.registrarFor("io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin"));
}
remember import this as well
import io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin
step 3)
run flutter clean -> flutter get -> uninstall your app
The code is completely fine.
Just restart your emulator. If you didn't do a full restart by closing the emulator and open it up again, this error can propably occur, because it doesnt have the newly added plugins.
Add SharedPreferences.setMockInitialValues({}) to your code before the runApp() function inside the main function of Flutter App.
this fixed the error for me

What is WidgetsFlutterBinding and how it is being used in Flutter app?

When and how we use it?
How it works?
WidgetsFlutterBinding
You have to use it, in this way:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
https://flutter.dev/docs/resources/architectural-overview#architectural-layers
The WidgetFlutterBinding is used to interact with the Flutter engine. Firebase.initializeApp() needs to call native code to initialize Firebase, and since the plugin needs to use platform channels to call the native code, which is done asynchronously therefore you have to call ensureInitialized() to make sure that you have an instance of the WidgetsBinding.
Answerd By https://stackoverflow.com/users/7015400/peter-haddad
Answer Link https://stackoverflow.com/a/63873689
WidgetsBinding.ensureInitialized() This initialised communication between the Dart Layer and Flutter Engine.
We need to call this method if we need the binding to be initialised before calling [runApp]. Flutter cannot directly interact with flutter engine until and unless binding is established.
Example 1: Shows Firebase platform initialisation between flutter and native code, which Firestore class do internally.
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firestore.initializeApp();
runApp(
...
)
}
OR
Example 2: Shows device orientation changes before even app starts, for this also we need to established binding connection.
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown
]);
runApp(
...
)
}
Internally, WidgetsBinding.ensureInitialized() supports various binding likes GestureBinding, SchedulerBinding, ServicesBinding, PaintingBinding, SemanticsBinding, RendererBinding, WidgetsBinding
ServicesBinding listens for platform messages and directs them to the handler for incoming messages (BinaryMessenger).
PaintingBinding is responsible for binding to the painting library.
RenderBinding binds the render tree to the Flutter engine.
WidgetBinding binds the widget tree to the Flutter engine.
SchedulerBinding is the scheduler for running immediate tasks.
SemanticsBinding binds the semantics layer and the Flutter engine.
GestureBinding is a binding for the gesture subsystem.