[Beginner] dart:html dart:io

108 views
Skip to first unread message

Talel Amira

unread,
Mar 5, 2014, 10:37:41 PM3/5/14
to mi...@dartlang.org
Hi guys,

I'm starting with dart and have some questions about an application I'm trying to translate to dart:
- I'm trying to read JSON file that can be located on local drive or on the net. Should I use File in dart:io or dart:html (because it's a web app finally)? can I use dart:io and dart:html in the same class?

- I can't simply replace backslashes with slashes like in other languages. The API states that I must use a pattern (even if it's just a string that I'm replacing). It's maybe more a regexp question, but does anyone knows what's wrong with this :

String pathToFile = "C:\Users\Talel\Desktop\facturation.json".replaceAll(new RegExp("\Q\\E"), "/");

All I get is a string with no slashes
Uncaught Error: FileSystemException: Cannot open file, path = 'C:UsersTalelDesktop acturation.json'

thanks in advance

Alex Tatumizer

unread,
Mar 5, 2014, 11:07:16 PM3/5/14
to mi...@dartlang.org
print(r"C:\Users\Talel\Desktop\facturation.json".replaceAll(r"\", "/"));
prints
C:/Users/Talel/Desktop/facturation.json

Ivan Zaera Avellon

unread,
Mar 6, 2014, 2:32:33 AM3/6/14
to mi...@dartlang.org
>can I use dart:io and dart:html in the same class?

No. The first only exists in the server. The latter only in a web browser. You should make your own abstraction class for reading the files and include one implementation or the other (client or server implementation) when initializing your application.



El 06/03/14 04:37, Talel Amira escribió:
--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.

signature.asc

Peter Ahé

unread,
Mar 6, 2014, 2:38:46 AM3/6/14
to mi...@dartlang.org
print(new Uri.file(r"C:\Users\Talel\Desktop\facturation.json", windows: true));

Prints: file:///C:/Users/Talel/Desktop/facturation.json

Generally, I recommend always using Uri to represent file names, as it correctly handles UNC paths aka network paths in a compatible way across all platforms.

Cheers,
Peter
--

Lasse R.H. Nielsen

unread,
Mar 6, 2014, 6:34:51 AM3/6/14
to mi...@dartlang.org



On Thu, Mar 6, 2014 at 4:37 AM, Talel Amira <talel...@gmail.com> wrote:
Hi guys,

- I can't simply replace backslashes with slashes like in other languages. The API states that I must use a pattern (even if it's just a string that I'm replacing).

Strings also implement the Pattern interface, so you can use just a string:
   String pathToFile = ".....".replaceAll(r"\", "/");
 
It's maybe more a regexp question, but does anyone knows what's wrong with this :

String pathToFile = "C:\Users\Talel\Desktop\facturation.json".replaceAll(new RegExp("\Q\\E"), "/");

The RegExps used by Dart is the JavaScript variant. There are no "\Q" and "\E" escapes in either Dart strings or regexps.
You can use either  new RegExp(r"\\")  or  new RegExp("\\\\")  to make a RegExp matching a single backslash (by using a source string containing two backslashes).

The r"\\" is a raw string literal, where backslashes don't start escapes. It's very useful for, for example,
regexp patterns.


All I get is a string with no slashes

Backslashes have meaning in Dart strings too. The original path looks like you have a string with a \U. \T and \f escape. Only the \f means something (form-feed), which is why the "f" is missing from your error report too. 
You will want to use a raw string for the path, so:

  String pathToFile = r"C:\Users\Talel\Desktop\facturation.json".replaceAll(r"\", "/");
 
Uncaught Error: FileSystemException: Cannot open file, path = 'C:UsersTalelDesktop acturation.json'

Hope this helps. 
--
Lasse R.H. Nielsen - l...@google.com  
'Faith without judgement merely degrades the spirit divine'
Google Denmark ApS - Frederiksborggade 20B, 1 sal - 1360 København K - Denmark - CVR nr. 28 86 69 84

Talel Amira

unread,
Mar 7, 2014, 8:58:48 AM3/7/14
to mi...@dartlang.org
Thanks everyone! that was helpful.

Last related question: If I'm setting the path into a variable, should I always concatenate it with r in order to get it to work?

Matthew Butler

unread,
Mar 7, 2014, 9:04:38 AM3/7/14
to mi...@dartlang.org


On Friday, March 7, 2014 9:58:48 AM UTC-4, Talel Amira wrote:
Thanks everyone! that was helpful.

Last related question: If I'm setting the path into a variable, should I always concatenate it with r in order to get it to work?

It's safest yes. the r'string' notation means raw string. This will prevent a backslash from being interpreted as an escape sequence. Otherwise you need to write paths like this:
'C:\\Users\\Talel\\Desktop\\....'
Much easier to use use:
r'C:\users\Talel\Desktop\...' and much less prone to errors (eg forgetting to escape a backslash).

Matt

Günter Zöchbauer

unread,
Mar 7, 2014, 10:54:11 AM3/7/14
to mi...@dartlang.org
Only when you want to use string interpolation like '${drive}\\somedir\\${asubdir}\\filename.xxx' you can't use r'string' and have to escape some chars instead. 

Talel Amira

unread,
Mar 7, 2014, 11:36:51 AM3/7/14
to mi...@dartlang.org
Thank you!

I don't find it very elegant to do:
String somePath = getPath();
somePath = 'r' + somePath;

Maybe a string method like somePath.getRaw() will be better


--

Bob Nystrom

unread,
Mar 7, 2014, 11:36:51 AM3/7/14
to General Dart Discussion

On Fri, Mar 7, 2014 at 5:58 AM, Talel Amira <talel...@gmail.com> wrote:
If I'm setting the path into a variable, should I always concatenate it with r in order to get it to work?

If you're working with paths, I highly recommend using the path package instead of doing your own string manipulation. There a bunch of subtle corner cases when manipulating paths and it's best to use code specifically designed for that purpose.

Cheers!

- bob

Matthew Butler

unread,
Mar 7, 2014, 11:44:48 AM3/7/14
to mi...@dartlang.org, m...@talel.org


On Friday, March 7, 2014 12:36:51 PM UTC-4, Talel Amira wrote:
Thank you!

I don't find it very elegant to do:
String somePath = getPath();
somePath = 'r' + somePath;

The r is before the string quotes not within them. It's only a concern when working with string literals. Eg: myString = r'Raw\string';  

If you're working with paths, I highly recommend using the path package instead of doing your own string manipulation. There a bunch of subtle corner cases when manipulating paths and it's best to use code specifically designed for that purpose.

Cheers!

- bob

+1 to that!

Matt 
Reply all
Reply to author
Forward
0 new messages