文本要点
- HTTP Get 请求
- Dart 类方法(或称为:命名的构造函数)
- String 转 JSON
TestModel
import 'dart:convert';
class TestModel {
String method;
num number;
bool isAjax;
TestModel(this.method, this.number, this.isAjax);
/**
* 类方法
*/
TestModel.fromJSON(jsonObject) {
if (jsonObject.runtimeType == String) {
Map<String, dynamic> temp = json.decode(jsonObject);
if (temp != null) {
method = temp['method'];
number = temp['number'];
isAjax = temp['isAjax'];
}
}
}
}
import 'package:http/http.dart' as http;
import 'TestModel.dart';
void httpRequest() async {
var url = 'http://api.muco.vip/test.php?a=1&b=2&c=3';
http.get(url).then((response) {
print('内容:${response.body}');
var temp = TestModel.fromJSON(response.body);
print('temp method : ${temp.method}');
});
}