Flutter:getter“主机”被调用为null

时间:2019-08-05 07:51:26

标签: http flutter https request httprequest

我正在编写一个使用摘要式身份验证的HttpRequest。这是错误:未处理的异常:NoSuchMethodError:在null上调用了getter'host'。我不知道如何处理。

我是新手,我相信我的代码有些混乱,总之我收到了错误;

[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,
        )));
      }
    }

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您将错误的参数传递给.addCredentials

void addCredentials (
  Uri url,
  String realm,
  HttpClientCredentials credentials
)

因此,您应该执行以下操作:

  httpClient.addCredentials(Uri.parse('https://192.168.2.1:8443', 'theRealm', HttpClient...

要找出领域,请进行测试连接,而无需先设置凭据,而要添加authenticate回调并打印服务器提供的领域。

  httpClient.authenticate = (uri, scheme, realm) async {
    print(realm); // make a note of this and use it in addCredentials
    return false; // this causes the test request to fail - but now you know the realm
  };

了解了领域之后,您就可以删除.authenticate并再次添加.addCredentials