Flutter Test MissingPluginException - dart

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
}

Related

the Dart compiler exited unexpectedly

I was debugging my project. After some execution, it started showing an error saying that the Dart compiler exited unexpectedly (see screenshot below). I also updated my dart SDK, but nothing happened. I did search on Google, but found nothing that helped.
try this may help
flutter clean
This issue could also happen when you have migrated to Null-Safety. The reason is that when migrating to Null-Safety, all packages need to also be migrated to Null-Safety, thus it will return an error saying:
Error: Cannot run with sound null safety, because the following dependencies
don't support null safety:
Also, it will give you a link for you to reference:
For solutions, see https://dart.dev/go/unsound-null-safety
Unhandled exception:
Bad state: Unsupported Null Safety mode NonNullableByDefaultCompiledMode.Invalid, in null.
Since this issue results in an error, you won't be able to run your project or package unless all packages in your dependencies are null-safety migrated. The dart compiler will end after this.
the Dart compiler exited unexpectedly.
Failed to compile application.
Exited (sigterm)
If you get the issue and you are sure that all packages are up-to-date and that all support Null-Safety, try running flutter clean. This will clear or packages in your project and reinstall them, cache files will also be removed.
Please leave down a comment if you may have any suggestions, or edit this answer if you wish.
I had this problem after adding package golden_toolkit and one golden test I wrote for it. Removing the test file and the dependency from pubspec.yaml and then running flutter clean, flutter pub get fixed it.
In my case flutter clean cant help. My flutter/bin/cache folder was corrupted. Deleting flutter/bin/cache folder and then running command flutter, downloads dart SDK and then setting up the flutter sdk path by giving your flutter folder path in Android Studio -> Preferences -> Languages and Frameworks -> Flutter , solves my problem.
delete flutter/bin/cache then type in terminal flutter clean then flutter run
that's work for me
My case was really strange and I don't know were it comes from, is it VS or Dart
The point is somewhere here where I set my class as the generic of the base class: class MyClass extends MyBaseClass<MyClass> and add an instance to the VS WATCH and set a beark point after the instance creation.
class MyBaseClass<T> {}
class MyClass extends MyBaseClass<MyClass> {
#override
String toString() => "MyClassObject";
}
main() {
MyClass o = MyClass();
print(o); // If you add 'o' to your WATCH items and add a breakpoint here it will crash
}
if you add any packages it can be the problem.
just use
flutter clean
flutter pub get
flutter run

Why Flutter copies the src of standard libraries to sky_engine?

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.

Flutter: Add plugin only for iOS in pubspec.yaml file

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.

Using full Dart SDK with Flutter

Is there any way to tell Flutter use more newer dart sdk version? I found it a bit tricky, because it checks some hashes in flutter/bin/cache/*.stamp. And when I'm trying to make a symbolic link to homebrew installed dart sdk it cannot build flutter_tool. To do that I've removed flutter_tool.stamp and flutter_tool.snapshot files. Something else needed?
I'm not actually sure if it is a need or not, but these tools are still in beta, for me it sounds logic to use edge versions.
Flutter ships with a customized Dart SDK (for example with the web parts stripped).
The Flutter SDK is tightly bound to a specific Dart SDK version and there is no reliable way to make Flutter work with any other Dart SDK than the one it is installed with.

React-Native with macos target

Trying to create a NEW project which targets -both- macos and ios/android seems to fail.
Steps:
Followed steps on react native website, using create-react-native-app to get a native application. It works fine.
Use react-native-macos init and macos to get the mac app - it also works fine.
Noticing that #1 uses an App.js and #2 uses some sort of index.platform.js model, I change the xcode project in #2 to look for App.js instead. This at least makes sure the same code is reached.
As the react-native-macos github page says to do, I merge #2 into #1.. (unclear whether should overwriting, used 'ditto' command)
Add the rn-cli.config.js file to root, as react-native-macos says to do
Missing modules when I try to run macos - fails to bundle.
It looks to me like the 'react-native' folder in the node modules is somehow screwing up the react-native-macos folder. I'm still trying a variety of things but it's been exhausting considering how easy 'merge into your existing project' sounds.
https://github.com/ptmt/react-native-macos <- what i want to merge in
You likely will not be able to use create-react-native-app and react-native-macos together without some trail and error. This is because create-react-native-app actually creates an Expo app which is built on top of react-native but tries to abstract away a lot of the native details from the end user. For example you can't use any native modules that are not shipped with Expo when using create-react-native-app. The documentation of react-native-macos is likely assuming you are using react-native init which creates a base react-native project that you have to handle building on your own instead of offloading the build process to Expo.

Resources