Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you pass variables to main in Dart?

Tags:

dart

I'm using Dart in a Django application. There are configuration parameters that need to be passed to Dart. How do you pass values to main()? At this point I'm considering creating hidden elements in the web page and using query to get the values. Is there a better approach?

like image 502
Alan Humphrey Avatar asked Feb 10 '13 06:02

Alan Humphrey


People also ask

How do you declare a variable in darts?

How to Declare Variable in Dart. We need to declare a variable before using it in a program. In Dart, The var keyword is used to declare a variable. The Dart compiler automatically knows the type of data based on the assigned to the variable because Dart is an infer type language.

Is Dart pass by reference?

Dart does not support passing by reference. Dart is only passed by value, just like Java. Java also does not support reference passing.


1 Answers

Sounds like you are building a browser app?

(BTW, you can use new Options().arguments for command-line apps. Works great, but obviously there is no command line in browser apps :)

(Second, main() doesn't take arguments, so we have to find another way.)

OK, for a browser-app, what I might try doing is embedding some JSON into the page inside a <script> tag. Then, using query, find that element, parse the contents, and you're good to go.

In your HTML:

<script id="config">
  {"environment":"test"}
</script>

And in your Dart file:

import 'dart:html';
import 'dart:json' as json;

void main() {
  var config = json.parse(query("#config").innerHtml);

  print(config['environment']);
}

Hope that helps!

like image 165
Seth Ladd Avatar answered Oct 14 '22 16:10

Seth Ladd