Search code examples
androidcordovafilesystemsdartrikulo

How to use the Cordova/Phonegap Filesystem API with Rikulo?


execuse my english... I try to write a Cross-Platform Webapp initially just for the Android platfrom. I have to write and read Files to the Filesystem of the mobile. I've already seen that the package rikulo_gap not natively supports the Filesystem API. The Fileystem API used by Rikulo is that one, which of course doesn't work on a mobile platform.

So I've seen that Rikulo's Js-Package should make my Dart-Code interoperable with my Cordova.js-File (v4.0.0), which includes the global function requestFileSystem(persistence,size,success-funct,fail-funct). Now i've tried to make my code work, but i don't get a Filesystem back - i'm getting errors...

Here's my Dart-Code:

import 'package:rikulo_gap/device.dart';
import 'dart:js' as js;

void main() { 
 Device.init().then((device) => onDeviceReady(device)).
 catchError((ex) => print("AN ERROR OCCURED: " + ex.toString()));
}

void onDeviceReady(device) {
 var persistent = js.context['PERSISTENT']; 
 print("Should be Constant PERSISTENT of LFS: " + persistent.toString());
 var fsName = js.context.callMethod('requestFileSystem',[persistent,0,dofs,fail]);
}

void dofs(fs) {
 print("Success");
 var myFs = js.context[fs];
 print("The Return of RequestFileSystem is a: " + myFs.toString());
}

LogCat says:

10-23 18:08:32.589: D/CordovaLog(951): file:///android_asset/www/adam.dart.js: Line 12111 : Should be Constant PERSISTENT of LFS: 1
10-23 18:08:32.624: D/CordovaLog(951): file:///android_asset/www/adam.dart.js: Line 12111 : Success
10-23 18:08:32.649: D/CordovaLog(951): file:///android_asset/www/cordova.js: Line 1060 : processMessage failed: Stack: Error
10-23 18:08:32.649: D/CordovaLog(951):     at dart.wrapException (file:///android_asset/www/adam.dart.js:2520:15)
10-23 18:08:32.649: D/CordovaLog(951):     at JsObject.$index (file:///android_asset/www/adam.dart.js:11549:17)
10-23 18:08:32.649: D/CordovaLog(951):     at dart.J.$index$asx (file:///android_asset/www/adam.dart.js:19278:41)
10-23 18:08:32.649: D/CordovaLog(951):     at dofs (file:///android_asset/www/adam.dart.js:4185:7)
10-23 18:08:32.649: D/CordovaLog(951):     at dart.Primitives_applyFunction (file:///android_asset/www/adam.dart.js:2489:23)
10-23 18:08:32.649: D/CordovaLog(951):     at _callDartFunction (file:///android_asset/www/adam.dart.js:11464:29)
10-23 18:08:32.649: D/CordovaLog(951):     at file:///android_asset/www/adam.dart.js:11671:18
10-23 18:08:32.649: D/CordovaLog(951):     at file:///android_asset/www/plugins/org.apache.cordova.file/www/requestFileSystem.js:52:25
10-23 18:08:32.649: D/CordovaLog(951):     at success (file:///android_asset/www/plugins/org.apache.cordova.file/www/fileSystems-roots.js:40:13)
10-23 18:08:32.649: D/CordovaLog(951):     at Object.cordova.callbackFromNative (file:///android_asset/www/cordova.js:293:54)
10-23 18:08:32.649: D/CordovaLog(951): file:///android_asset/www/cordova.js: Line 1061 : processMessage failed: Message: S01 File1158866550 [{"fullPath":"\/","filesystemName":"temporary","isDirectory":true,"nativeURL":"file:\/\/\/storage\/emulated\/0\/Android\/data\/de.htwg.myAndroid\/cache\/","filesystem":0,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"persistent","isDirectory":true,"nativeURL":"file:\/\/\/storage\/emulated\/0\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"content","isDirectory":true,"nativeURL":"cdvfile:\/\/localhost\/content\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"files","isDirectory":true,"nativeURL":"file:\/\/\/data\/data\/de.htwg.myAndroid\/files\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"files-external","isDirectory":true,"nativeURL":"file:\/\/\/storage\/emulated\/0\/Android\/data\/de.htwg.myAndroid\/files\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"documents","isDirectory":true,"nativeURL":"file:\/\/\/data\/data\/de.htwg.myAndroid\/files\/Documents\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"sdcard","isDirectory":true,"nativeURL":"file:\/\/\/storage\/emulated\/0\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"cache","isDirectory":true,"nativeURL":"file:\/\/\/data\/data\/de.htwg.myAndroid\/cache\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"cache-external","isDirectory":true,"nativeURL":"file:\/\/\/storage\/emulated\/0\/Android\/data\/de.htwg.myAndroid\/cache\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"root","isDirectory":true,"nativeURL":"file:\/\/\/","filesystem":1,"isFile":false,"name":""}]       

What annoys me is, that the first Exception on the Stack is a dart.wrapException, so i guess it's a general Dart<>Js Interop problem.

I followed the official cordova instruction for Building with Android and currently i'm building with the Windows CommandLine with cordova run android. (And yes i've installed the plugins (File, FileUtil, Device via CommandLine and they are added to the project).

So can anybody tell me:

  1. Is it like i think and there is not a common way to use the cordova-Filesystem API with Rikulo/Dart?
  2. If 1. is true, is there a workaround? Anybody a Idea, a loose note oder anything else which could help me?

Solution

    1. Rikulo doesn’t support Cordova filesystem API yet. You can contribute one or try to use window.requestFileSystem instead.

    2. The object returned to dofs is a JsObject instance, so you don’t need to use js.context to convert it. That is,

      var myFs = js.context[fs];
      

      shall be revised to

      var myFs = fs;