getter“流”被调用为null

时间:2020-10-25 13:37:00

标签: flutter webview stream-builder

我尝试制作一个webview,我想获取该webView的高度,然后发现this

但是出现以下错误:

enter image description here

The following NoSuchMethodError was thrown building AnnouncementWebView(dirty, state: _AnnouncementWebViewState#d4695):
The getter 'stream' was called on null.
Receiver: null
Tried calling: stream

这是我的代码:

  num _stackToView = 1;
  double contentHeight = 0;
  WebViewController _webViewController;
  StreamController<double> streamController;


      body: IndexedStack(
        index: _stackToView,
        children: [
          StreamBuilder<double>(
            initialData: 100,
            stream: streamController.stream,
            builder: (context, snapshot) {
              return Container(
                height: snapshot.data,
                child: WebView(
                  javascriptMode: JavascriptMode.unrestricted,
                  initialUrl: widget.slug,
                  onPageFinished: (String url) async {
                    if (snapshot != null) {
                      double height = double.parse(
                          await _webViewController.evaluateJavascript(
                              "document.documentElement.scrollHeight;"));
                      streamController.add(height);
                    }
                    setState(() {
                      _stackToView = 0;
                    });
                  },
                  onWebViewCreated: (WebViewController c) {
                    _webViewController = c;
                  },
                ),
              );
            },
          ),
          Container(child: Center(child: CircularProgressIndicator())),
        ],
      ),

我不知道为什么会收到上述错误。预先感谢您的宝贵时间。

1 个答案:

答案 0 :(得分:0)

您尚未初始化流控制器,因此当您尝试使用它时,它为null。

要解决此问题,请在initState中初始化流控制器(并且不要忘记在处理过程中将其关闭),或者也可以忽略流,而只需将setState与内容高度的双字段一起使用