Is there any way to add a plugin only for a particular platform (like just for iOS) in pubspec.yaml file?
Something like this.
dependencies:
flutter:
sdk: flutter
isIos ? http: ^1.0 : null
PS: I also know I can import the plugin normally and in code, I can make changes but I don't want this solution.
if (Theme.of(context).platform != TargetPlatform.iOS) {
// don't use that plugin part
}
I simply don't want to add plugin in my pubspec.yaml for a particular platform. Possible?
For the pubspec.yaml, you wouldn't be able to specify the platform under dependencies: so
dependencies:
flutter:
sdk: flutter
http: ^1.0
Then inside your dart code,
import 'dart:io';
if (Platform.isAndroid) {
//code here will run only when device is android
} else if (Platform.isIOS) {
//code here will run only when device is iOS
}
Adding a plugin for only a specific platform is not possible. There is an easier way to access the current platform though.
import 'dart:io';
if(Platform.isIOS) {
// Use plugin
}
Not sure, but one possible way around is to install that pod natively and write its implementation in Xcode, and then invoking that native code via the flutter application.
Related
Since dart:ffi is available from Dart 2.2.0-dev.2.0, I've been trying to use that library. The sample app works fine for me and I also tried to use it for my Flutter app but I couldn't call it with import "dart:ffi", although Flutter on my machine was HEAD of master that used Dart 2.3.0-dev.0.0.
I checked what happened, then I found that sky_engine didn't contain ffi.dart while it contained other libraries (e.g. "dart:core"). Also I noticed that the source files of those libraries were copied from $FLUTTER_ROOT/bin/cache/dart-sdk/lib to $FLUTTER_ROOT/bin/cache/pkg/sky_engine using BUILD.gn or _embedder.yaml and that seemed to be why I couldn't use the dart:ffi in my Flutter app.
However, in the first place, why does Flutter need sky_engine, which is "the interface between Dart and the Flutter Engine"? Why not calling them directly without this glue code?
Flutter has a good documentation for FFI to be able to call native C APIs. Adding import 'dart:ffi' as ffi; on the Flutter app works without issues as of my testing with Flutter 2.5. If you're able to provide a minimal repro of your issue, this will help folks to understand the question better.
As for the question on why sky_engine is used by Flutter, that's just simply because it's the "flutter_engine" - similar to what has been already mentioned in the comments.
I would like to add the pull to refresh behavior in my app. I read about the RefreshIndicator, but it seems to have only the material UI. Can you suggest me a way to have the native iOS pull to refresh in my Flutter App?
Thanks :)
The native iOS-style pull to refresh widget is CupertinoSliverRefreshControl. You can check the official demo here: https://github.com/flutter/flutter/blob/master/examples/flutter_gallery/lib/demo/cupertino/cupertino_refresh_demo.dart
Take a look at this package https://pub.dartlang.org/packages/pull_to_refresh
It does what you need.
Add the dependency in your pubspec.yml file
dependencies:
pull_to_refresh: ^1.1.6
Here you have the samples: https://github.com/peng8350/flutter_pulltorefresh/tree/master/example/lib/ui
I'm having a problem with usage of khttp library (which is supposed to work in Kotlin and provides equal to python's request's library features)
My build.gradle contains those strings:
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib"
compile "com.github.jkcclemens:khttp:-SNAPSHOT"
}
Project builds with success, but importing with import khttp doesn't work
In general, I'm using kotlin as framework to IOS project, and khttp is needed to connect to longpoll server. If khttp isn't supposed to work in my case so what are my options? Using it's sources aint good idea i think
Try using this:
compile "com.github.jkcclemens:khttp:0.1.0"
And add https://jitpack.io/ as a repository
I know that I might be necrobumping, but if anyone is looking for an answer, they wouldn't be able to get it from the above.
Khttp library is build for kotlin JVM and not native. If you take a look at the source code, you'll be able to notice that it's using Java's libraries for it to function, this for example.
That means that sadly you can't run it on iOS and any platform that doesn't run JVM, as khttp will only run on the JVM platform and won't be able to run on native because of missing libraries.
I came across a websocket example that I would like to use. However it uses import 'dart:html';. When I introduce that in my Flutter project seems like its not being picked up. Do I need to add additional dependencies to the pubspec.yaml?
dart:html can't be used in Flutter.
It is for browser applications only.
dart:html also only comes with the regular Dart SDK, not with the Dart SDK shipped with Flutter.
I know this is an old question but let me drop this answer here.
I've searched for a web crawler/scraper for Flutter for a while now. I've tried to use FlutterWebview and also the HTML package but no way. Recently I've found a new package for this.
The advantage of this package is that it is really cross platform as explained:
Cross-platform dart:html that works in the browser, Dart VM, and Flutter.
Typical use cases are:
Cross-platform application development (e.g. Flutter mobile and web versions).
Web crawling and scraping
you can use universal_html for any scraping/crawling purpose
Since the merge of Flutter-web into the main Flutter repository, it's no longer possible to directly add imports for web libraries (e.g. dart:html) in a Flutter project on the main channel when targeting Web, Android and iOS.
Use the universal html package which provides extensive support for multiple platforms and web libraries.
From the root level of your project, command
1.
flutter pub add universal_html
2. import 'package:universal_html/html.dart' as html
This package isn't required to run some web files (e.g. dart:js). In my case, I just had to remove the import 'dart:js' import statement.
Running tests which rely on the SharedPreferences Plugin always result in
MissingPluginException(No implementation found for method getAll on channel plugins.flutter.io/shared_preferences)
My pubspec.yaml
dev_dependencies:
flutter_test:
sdk: flutter
dependencies:
flutter:
sdk: flutter
shared_preferences: 0.2.3
The code for works fine in the application itself.
Am i missing something i need to do in order to run tests which make use of a plugin?
If you're using shared_preferences 0.2.4 and above, use setMockInitialValues:
SharedPreferences.setMockInitialValues({}); // set initial values here if desired
For earlier versions you can do it manually:
const MethodChannel('plugins.flutter.io/shared_preferences')
.setMockMethodCallHandler((MethodCall methodCall) async {
if (methodCall.method == 'getAll') {
return <String, dynamic>{}; // set initial values here if desired
}
return null;
});
I had the exact same problem with the flutter_secure_storage plugin. I believe the issue is that with both plugins you are relying on storage on your phone or emulator (not something in your app) so it's not available in your test environment. Try running the test directly by executing flutter run your_test_file.dart. According to https://flutter.io/testing/ this should execute your test "in your preferred runtime environment such as a simulator or a device." It worked perfectly for me.
#Siman thanks
version shared_preferences: ^0.5.12
ad SharedPreferences.setMockInitialValues({});
before the runApp() function inside the main funation of Flutter App
makes this error fixed for me
A little bit late to the party but if you encounter this problem with any channel or package there is an easy solution. If you can skip testing platform specific issues, you can use a conditional to skip this function call only while testing.
if (!Platform.environment.containsKey('FLUTTER_TEST')) {
//only runs when you are NOT testing
}