【问题标题】:Flutter: The getter "host" was called on nullFlutter:在 null 上调用了 getter“主机”
【发布时间】: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


【解决方案1】:

您将错误的参数传递给.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

【讨论】:

  • 我不需要的“void addCredentials”。但其余的都很完美!非常感谢!
猜你喜欢
  • 2021-08-11
  • 2020-01-08
  • 2021-12-10
  • 2020-03-24
  • 2021-05-25
  • 2023-03-08
  • 2020-08-01
  • 2020-10-12
相关资源
最近更新 更多