I am using the getx package in my application and there is something I am wondering about. Is it possible to reset all states of my app using getx?
So it's kind of like a restart.
Future<void> logoutUser() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.clear(); //clearing my token data
await Get.deleteAll(force: true); //deleting all controllers
Phoenix.rebirth(Get.context!); // Restarting app
Get.reset(); // resetting getx
}
used flutter_phoenix library to restart the app.
Deleted all GetX controllers using Get.deleteAll(force: true);
Works like a charm for me.
Related
I am using Awesome notifications to display notifications in my app. In the notifications, I have an action button, and when pressing it, the app is supposed to read and write some simple data from/to the memory of the phone using shared preferences. This is supposed to happen in the background without opening the app in the foreground.
I tried the following code:
#pragma("vm:entry-point")
static Future<void> onActionReceivedMethod(ReceivedAction action) async {
print('It works');
print(action.toMap());
final SharedPreferences prefs = await SharedPreferences.getInstance();
List<PinnedFolder> pinnedList = [];
try {
final String? pinnedString = prefs.getString('pinnedKey');
if (pinnedString != null) {
pinnedList = PinnedFolder.decode(pinnedString);
print('PinnedList got from memory, length: ${pinnedList.first.pinnedList.length}');
}
} catch (error) {
debugPrint('Error: couldnt get pinned folders: $error');
}
The "It works" and 'action.toMap()' get printed, but I can't get data from shared preferences. Is it so, that I can't use added packages in #pragma("vm:entry-point") functions? What would be the best way to fix the code? The action doesn't need to happen right after the button press, it can also happen the next time when the app is in the foreground, but so that the button action information is still available.
I found a solution using the package flutter_secure_storage.
const storage = FlutterSecureStorage(); await storage.write(key: my_key, value: my_value);
It seems to be so that only a few packages work in #pragma("vm:entry-point") functions, but this is one of them. I implemented it so that every time the app opens, it gets and handles the data from the secure storage before the app is launched.
In Flutter integration tests, is there any way we can kill the app & reinstall it for every testWidgets?
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
testWidgets('TC_01 Home page validation', (tester) async {
app.main();
await tester.pumpAndSettle(const Duration(seconds: 5));
await tester.tap(find.byKey(const ValueKey('startButton')));
await tester.pumpAndSettle();
});
testWidgets('TC_02 Home page text validation ', (tester) async {
app.main();
await tester.pumpAndSettle(const Duration(seconds: 5));
expect(find.text('Home Screen'), findsOneWidget);
expect(find.text('Transactions'), findsOneWidget);
});
}
I want to reinstall the app for every new test that runs.
This is not possible, because, in Flutter, the tests (integration tests included) live inside your app. If you uninstalled the app after the first test passed, you'd also uninstall the test suite (which runs inside your app) - which doesn't make sense.
A better question is to ask: what do you want to achieve? If you want to reset state, you can pump your app once again and it will reset the whole widget tree, along with all blocs/providers you might've created.
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: ...)
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
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