【发布时间】:2019-12-12 17:25:47
【问题描述】:
我正在编写一个使用摘要式身份验证的 HttpRequest。这是错误:未处理的异常:NoSuchMethodError:在 null 上调用了 getter 'host'。不知道怎么处理。
我是 Flutter 的新手,我相信我的代码有点乱,简而言之,我收到了错误;
[ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception:NoSuchMethodError: The getter 'host' was called on null.
Receiver: null
Tried calling: host
这是我的代码。
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:connectivity/connectivity.dart';
import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:http/io_client.dart';
class Router extends StatefulWidget {
@override
_RouterState createState() => _RouterState();
Router(this.listType);
final String listType;
Connectivity connectivity = Connectivity();
}
class _RouterState extends State<Router> {
var username = 'dslf-config';
var password = '80567851';
String url = 'https://192.168.2.1:8443';
static var parameter = 'Device.DeviceInfo.ModelName';
final body = '''
<?xml version="1.0" encoding= "UTF-8" ?>
<soap-env:Envelope soap-enc="http://schemas.xmlsaop.org/soap/encoding/" xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:cwmp="urn:telekom-de.totr64-2-n">
<soap-env:Body>
<cwmp:GetParameterValues xmlns:cwmp="urn:dslforum-org:cwmp-1-0">
<cwmp:ParameterNames soap-env:arrayType="xsd:string[10]">
<xsd:string>Device.DeviceInfo.ModelName</xsd:string>
</cwmp:ParameterNames>
</cwmp:GetParameterValues>
</soap-env:Body>
</soap-env:Envelope>
''';
Future<http.Response> getData2() {
Map<String, String> headers = {
"Content-Type": "text/xml",
"SOAPAction": "urn:telekom-de:device:TO_InternetGatewayDevice:2#GetParameterValues"
};
bool trustSelfSigned = true;
HttpClient httpClient = new HttpClient()
..badCertificateCallback =
((X509Certificate cert, String host, int port) => trustSelfSigned);
httpClient.addCredentials(null , "https://192.168.2.1:8443", new HttpClientDigestCredentials(username, password));
IOClient ioClient = new IOClient(httpClient);
return ioClient.post("https://192.168.2.1:8443",headers: headers, body: body);
}
getData3() async {
final response = await getData2();
print(response.body.toString());
}
Widget build(BuildContext context) {
return new Scaffold(
body: new Center(
child: new RaisedButton(
child: new Text("Get data",
style: new TextStyle(
color: Colors.white,
fontStyle: FontStyle.italic,
fontSize: 20.0)),
onPressed: getData3,
)));
}
}
感谢您的帮助。
【问题讨论】:
-
我复制粘贴你的代码,但我没有发生空异常。另请注意,您应该在发布
return await ioClient.post("https://192.168.2.1:8443", headers: headers, body: body)之前await
标签: http flutter https request httprequest