初学flutter,初学前端,尝试在dart中直接使用HttpClient时,直接报出Platform not supported,查资料发现他还不支持浏览器。 通过查阅资料发现可以借助axios 与 dart:js 之间的互相调用来实现。目前还不清楚有没有其它更好的方式。实例代码:
main.dart:
import 'package:flutter/material.dart'; import 'dart:js'; void callJS(){ context['console'].callMethod('log', ['开始调用']); JsObject(context['getDartCall'], ['http://*****.com','{ID: 1234 , Name: "Hunter"}']); } void jsCallBack(resData){ //结果回调 print(resData); } void main() { //定义回调方法 context['jsCallBack'] = jsCallBack; runApp(new MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return new MaterialApp( home: new MyHomePage(), ); } } class MyHomePage extends StatefulWidget { MyHomePage({Key key}) : super(key: key); @override _MyHomePageState createState() => new _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { @override Widget build(BuildContext context) { var spacer = new SizedBox(height: 32.0); return new Scaffold( body: new Center( child: new Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ new Text('Dart 与 JS 互相调用实例'), new Text('发起调用'), spacer, new RaisedButton( onPressed: (){ callJS(); }, child: new Text('发起调用'), ), ], ), ), ); } }