'error in MethodChannel, IllegalArgumentException
Im a beginner to Flutter development, actually to the whole Software development domain,
The Flutter application in question is Android only, which needs merely getFilesDir()
from the platform, nothing else,
I initially used path_provider
, then switched to path_provider_android
, and after checking its [sourse-code][https://github.com/flutter/plugins/blob/main/packages/path_provider/path_provider_android/], I tried to use getFilesDir()
directly,
main.dart
:
const platform = MethodChannel("com.example/persistence");
privateStoragePath = await platform.invokeMethod("getPrivateStoragePath");
MainActivity.kt
:
package com.example
import android.content.Context
import android.content.ContextWrapper
import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
class MainActivity : FlutterActivity() {
private val CHANNEL = "com.example/persistence"
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
if (call.method == "getPrivateStoragePath") {
val path = getFilesDir()
result.success(path)
} else {
result.notImplemented()
}
}
}
}
Errors:
Syncing files to device Android SDK built for x86 64...
E/MethodChannel#com.example/persistence( 4828): Failed to handle method call
E/MethodChannel#com.example/persistence( 4828): java.lang.IllegalArgumentException: Unsupported value: '/data/user/0/com.example.example/files' of type 'class java.io.File'
E/MethodChannel#com.example/persistence( 4828): at io.flutter.plugin.common.StandardMessageCodec.writeValue(StandardMessageCodec.java:292)
E/MethodChannel#com.example/persistence( 4828): at io.flutter.plugin.common.StandardMethodCodec.encodeSuccessEnvelope(StandardMethodCodec.java:59)
E/MethodChannel#com.example/persistence( 4828): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler$1.success(MethodChannel.java:267)
E/MethodChannel#com.example/persistence( 4828): at com.example.example.MainActivity$configureFlutterEngine$1.onMethodCall(MainActivity.kt:18)
E/MethodChannel#com.example/persistence( 4828): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:262)
E/MethodChannel#com.example/persistence( 4828): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:178)
E/MethodChannel#com.example/persistence( 4828): at io.flutter.embedding.engine.dart.DartMessenger.lambda$handleMessageFromDart$0$DartMessenger(DartMessenger.java:206)
E/MethodChannel#com.example/persistence( 4828): at io.flutter.embedding.engine.dart.-$$Lambda$DartMessenger$6ZD1MYkhaLxyPjtoFDxe45u43DI.run(Unknown Source:12)
E/MethodChannel#com.example/persistence( 4828): at android.os.Handler.handleCallback(Handler.java:938)
E/MethodChannel#com.example/persistence( 4828): at android.os.Handler.dispatchMessage(Handler.java:99)
E/MethodChannel#com.example/persistence( 4828): at android.os.Looper.loop(Looper.java:223)
E/MethodChannel#com.example/persistence( 4828): at android.app.ActivityThread.main(ActivityThread.java:7656)
E/MethodChannel#com.example/persistence( 4828): at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#com.example/persistence( 4828): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
E/MethodChannel#com.example/persistence( 4828): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
E/flutter ( 4828): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: PlatformException(error, Unsupported value: '/data/user/0/com.example.example/files' of type 'class java.io.File', null, java.lang.IllegalArgumentException: Unsupported value: '/data/user/0/com.example.example/files' of type 'class java.io.File'
E/flutter ( 4828): at io.flutter.plugin.common.StandardMessageCodec.writeValue(StandardMessageCodec.java:292)
E/flutter ( 4828): at io.flutter.plugin.common.StandardMethodCodec.encodeSuccessEnvelope(StandardMethodCodec.java:59)
E/flutter ( 4828): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler$1.success(MethodChannel.java:267)
E/flutter ( 4828): at com.example.example.MainActivity$configureFlutterEngine$1.onMethodCall(MainActivity.kt:18)
E/flutter ( 4828): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:262)
E/flutter ( 4828): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:178)
E/flutter ( 4828): at io.flutter.embedding.engine.dart.DartMessenger.lambda$handleMessageFromDart$0$DartMessenger(DartMessenger.java:206)
E/flutter ( 4828): at io.flutter.embedding.engine.dart.-$$Lambda$DartMessenger$6ZD1MYkhaLxyPjtoFDxe45u43DI.run(Unknown Source:12)
E/flutter ( 4828): at android.os.Handler.handleCallback(Handler.java:938)
E/flutter ( 4828): at android.os.Handler.dispatchMessage(Handler.java:99)
E/flutter ( 4828): at android.os.Looper.loop(Looper.java:223)
E/flutter ( 4828): at android.app.ActivityThread.main(ActivityThread.java:7656)
E/flutter ( 4828): at java.lang.reflect.Method.invoke(Native Method)
E/flutter ( 4828): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
E/flutter ( 4828): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
E/flutter ( 4828): )
E/flutter ( 4828): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
E/flutter ( 4828): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:167:18)
E/flutter ( 4828): <asynchronous suspension>
E/flutter ( 4828): #2 main (package:example/main.dart:24:24)
E/flutter ( 4828): <asynchronous suspension>
E/flutter ( 4828):
Thanking you...
Solution 1:[1]
In MainActivity.kt
, use:
response.success(context.filesDir.toString())
Instead of:
val path = getFilesDir()
result.success(path)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | gitman-2021 |