Implementing pull-to-refresh in Flutter with Riverpod - flutter

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: ...)

Related

How Can I Restart My Flutter App With Getx

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.

Flutter Web: Right click -> Browser Context Menu -> Open Link in New Tab

I have a basic website using GetX for navigation. I have inkwells with ontap functions which navigate to a new view. Right now, if you right click these buttons there is no "open link in new tab/window", "save link as" or "copy link address".
Is there any way to get this functionality for Flutter Web?
Edit:
Since Flutter version 2.10, you no longer need to switch to channel beta for this.
Maybe I'm answering this late, but this may help someone in the future.
At the moment of writing this it is possible to be done, it is bugged on the stable channel, but it works perfectly on channel beta.
Just switch to channel beta:
flutter channel beta
flutter upgrade
Then follow this instructions to add url_launcher dependency to your project and import this package wherever you want to use it:
import 'package:url_launcher/link.dart';
And finally wrap any widget with this:
Link(
uri: Uri.parse('www.google.com'),
builder: (context, function) {
return InkWell(
onTap: () => print('Do something'),
child: Text('Right clickable text')
);
});

Flutter, how to take full screenshot

i am trying to find a way to take a screenshot of entire screen. Flutter library is only allowing me to make a screenshot of a widget, but i want a screenshot of entire android.
I did find this link:
How to programmatically take a screenshot on Android?
and this is resolwing my problem, but i tried to use that java code as it is in documentation:
https://flutter.dev/docs/development/platform-integration/platform-channels?tab=android-channel-java-tab
sadly i am not able to run that code, maybie im making some kind of mistakes? i find a error, while i whas following docs instruction, after that program whas working (not exacly, java whas not able to find my battery, but code whas compiling), and then i copy first answer from that stack link and i tried to pase it in "MainActivity" folder, sadly program whas not working, my "old" java code whas comipilng, from the docs, not the new one.
If you know the solution how to make a photo of entire android screen using some kind of buttor in flutter, or how to implement code from that link into my MainActivity.java folder, please help
Try this package native_screenshot
https://pub.dev/packages/native_screenshot
You can simply run this function to take the screenshot of your phone:
Future<void> _capturePng() async {
String path = await NativeScreenshot.takeScreenshot();
print(path);
}
You can learn more about this package here
It work fine with Android and IOS
Hope it will be useful
You can use screenshot plugin:https://pub.dev/packages/screenshot to take a widget screenshot.
A screenshot is a simple plugin to capture widgets as Images. This plugin wraps your widgets inside RenderRepaintBoundary
Use this package as a library
Add this to your package's pubspec.yaml file:
dependencies:
screenshot: ^0.2.0
Now in your Dart code, Import it:
import 'package:screenshot/screenshot.dart';
This handy plugin can be used to capture any Widget including full-screen screenshots & individual widgets like Text().
Create Instance of Screenshot Controller
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
File _imageFile;
//Create an instance of ScreenshotController
ScreenshotController screenshotController = ScreenshotController();
#override
void initState() {
// TODO: implement initState
super.initState();
}
...
}
Wrap the widget that you want to capture inside Screenshot Widget. Assign the controller to screenshotController that you have created earlier
Screenshot(
controller: screenshotController,
child: Text("This text will be captured as image"),
),
Take the screenshot by calling the capture method. This will return a File
screenshotController.capture().then((File image) {
//Capture Done
setState(() {
_imageFile = image;
});
}).catchError((onError) {
print(onError);
});
Duplicate: How to take a screenshot of the current widget - Flutter

Flutter using plugins inside flutter workmanager plugin callbackDispatcher function

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

Flutter Integration Testing- How to go back to previous screen when no back button available on screen

How to go back to previous screen when no back button available on screen?
Additional Information: I am trying to automate integration testing for flutter app. There is no back button on screen which I can use to go back screen.
Additional information: I am writing integration tests, for same I need to go back to previous screen. And due to design there is no back button on screen, I need to do by scripts only.
This is what worked for me for OpenContainers from the animations package:
final NavigatorState navigator = tester.state(find.byType(Navigator));
navigator.pop();
await tester.pump();
It's used in the flutter package's tests:
https://github.com/flutter/packages/blob/63040337424f345452a5e07632774e6585329ac0/packages/animations/test/open_container_test.dart#L234
If the purpose of the test is to stress and verify the WillPopScope callbacks as well, the following approach can be used:
final dynamic widgetsAppState = _._tester.state(find.byType(WidgetsApp));
await widgetsAppState.didPopRoute();
This is used in the Flutter official repository for testing: https://github.com/flutter/flutter/blob/master/packages/flutter/test/material/will_pop_test.dart
Note that this approach also works to simulate a back button pressed on Android.
Well, it's maybe a little bit late, however there are two possible ways to achieve this:
final backButton = find.byTooltip('Back');
await driver.tap(backButton);
or:
await driver.tap(find.pageBack());
you can use like this as ios provided
theme: ThemeData(
pageTransitionsTheme: PageTransitionsTheme(
builders: {
TargetPlatform.android: CupertinoPageTransitionsBuilder(),
TargetPlatform.iOS: CupertinoPageTransitionsBuilder(),
}
)
),
My emulador defaulf locale is "en_US" so I use the word "Back" as tooltip and it is working.
final backButton = find.byTooltip("Back");
await tester.tap(backButton);
await tester.pumpAndSettle();