I'm currently developing a website with dart replacing JavaScript. I want to have some code hidden for the average user, and some other code visible for users logged in. My idea was to have (at least) two dart files both with main()
functions, one visible and one hidden to the average user. Now I have a class with a factory returning a cached instance. However this instance is not cached from file to file. How do I get the same instance in two different dart files?
EDIT: Code example
File file1.dart
import "some_lib.dart";
main(){
var a = new A("string1");
print(a.string);
}
File file2.dart
import "some_lib.dart";
main(){
var a = new A("string2");
print(a.string);
}
File some_lib.dart
library some_lib;
class A{
String string;
static A _cached;
factory A(String s){
if(_cached == null){
_cached = new A._internal(s);
}
return _cached;
}
A._internal(this.string);
}
File index.html
<!DOCTYPE html>
<html>
<head>
<script type="application/dart" src="file1.dart"></script>
<script type="application/dart" src="file2.dart"></script>
<script type="text/javascript" src="packages/browser/dart.js"></script>
</head>
<body>
<h1>THIS IS DARTA</h1>
</body>
</html>
I expect this to print
string1
string1
in the console, but I get
string1
string2