2012-04-15 10 views
7

"सही लग रहा है", यह संकलित है, लेकिन नहीं चलता है, सांत्वना संदेश के साथ असफल रहने के लिए नीचे दिए गए कोड:मैं डार्ट आयात कैसे करूं: एचटीएमएल और डार्ट: io एक ही कक्षा में?

Cannot load Dart script dart:io
Failed to load resource

अगर मैं #import('dart:io'); बाहर टिप्पणी, गलत मुझे विश्वास है, मैं एक संकलन त्रुटि है, लेकिन यह लॉन्च करता है और जब तक मैं बटन दबाता हूं, मुझे रनटाइम त्रुटि मिलती है:

Internal error: 'http://127.0.0.1:3030/home/david/dart/samples/htmlIO/htmlIO.dart': Error: line 13 pos 26: type 'HttpClient' is not loaded
var connection = new HttpClient().get('www.google.com', 80, '/');

... जो अपेक्षित है।

तो मेरा सवाल है: मैं डार्ट आयात कैसे करूं: एचटीएमएल & डार्ट: एक ही कक्षा में io? जबकि dart:io एक सर्वर साइड पुस्तकालय है

#import('dart:html'); 
#import('dart:io'); 

class htmlIO { 

    ButtonElement _aButton; 

    htmlIO() { 
    } 

    void handlePress(Event e) { 
    var connection = new HttpClient().get('www.google.com', 80, '/'); 
    write('made it'); 
    } 

    void run() { 
    _aButton = document.query("#aButton"); 
    _aButton.on.click.add(handlePress); 
    write("Hello World!"); 
    } 

    void write(String message) { 
    // the HTML library defines a global "document" variable 
    document.query('#status').innerHTML = message; 
    } 
} 

void main() { 
    new htmlIO().run(); 
} 

उत्तर

10

dart:html, एक ग्राहक के पक्ष पुस्तकालय है। dart:html ब्राउज़र की विशेषताओं का उपयोग करता है, लेकिन dart:io उन सुविधाओं का उपयोग करता है जो ब्राउज़र सुरक्षा द्वारा प्रतिबंधित हैं (जैसे फ़ाइल सिस्टम एक ऑन-ऑन एक्सेस)।

ऐसा हो सकता है कि आप "mocked" ब्राउज़र के साथ सर्वर पर dart:html का उपयोग कर सकते हैं, जो इकाई परीक्षण और इसी तरह के लिए उपयोगी हो सकता है, लेकिन आप अभी तक ऐसा नहीं कर सकते हैं।

4

संक्षिप्त उत्तर, आप नहीं कर सकते। जैसा कि क्रिस का उल्लेख है, डार्ट: आईओ लाइब्रेरी केवल सर्वर पुस्तकालयों के लिए है।

मुझे लगता है कि आप अपने HTML ऐप में किसी HTTP सेवा से कनेक्ट करने का प्रयास कर रहे हैं। आपको HttpRequest लाइब्रेरी को देखना चाहिए। एचटीएमएल और से डार्ट HttpClient: http://c.dart-examples.com/api/dart-html/interface/eventtarget/httprequest/asynchronous

import 'dart:html'; 
import 'dart:convert'; 

void onSuccess(ProgressEvent event, HttpRequest request) { 
    print(event.loaded); // 0 
    print(request.statusText); // ok 
    print(request.responseText); // "(resource text)" 
} 

/** 
* test.txt file must be of the same origin 
* Or the response header must contain "Access-Control-Allow-Origin: [*|origin]" 
*/ 
void main() { 
    String url = "test.txt"; 
    HttpRequest request = new HttpRequest(); 
    request.open("GET", url, async : true); 
    request.onLoadEnd.listen((ProgressEvent e) => onSuccess(e, request)); 
    request.send(); 
} 

वहाँ डार्ट से HttpRequest एकजुट करने के लिए एक अनुरोध है: यहाँ उदाहरण के लिए एक लिंक है कब, देख http://code.google.com/p/dart/issues/detail?id=2677

संबंधित मुद्दे