WebViewController如果不使用级联的话会显示错误

huangapple go评论72阅读模式
英文:

WebViewController shows error if I don't use cascade

问题

在使用级联操作符时,不会显示错误。

import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

class HomeScreen extends StatelessWidget {
  HomeScreen({super.key});

  final controller = WebViewController()
    ..setJavaScriptMode(JavaScriptMode.unrestricted);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: WebViewWidget(
        controller: controller,
      ),
    );
  }
}

但是,当我不使用级联操作符时,Flutter 会显示错误。

import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

class HomeScreen extends StatelessWidget {
  HomeScreen({super.key});

  final controller = WebViewController();
  controller.setJavaScriptMode(JavaScriptMode.unrestricted);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: WebViewWidget(
        controller: controller,
      ),
    );
  }
}

这里是错误消息

英文:

When I write a WebViewController with cascade, it does not show an error.


import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

class HomeScreen extends StatelessWidget {
  HomeScreen({super.key});

  final controller = WebViewController()
    ..setJavaScriptMode(JavaScriptMode.unrestricted);
    

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: WebViewWidget(
        controller: controller,
      ),
    );
  }
}

But when I write code without cascade, flutter shows an error.

Does anyone know why?

import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

class HomeScreen extends StatelessWidget {
  HomeScreen({super.key});

  final controller = WebViewController();
  controller.setJavaScriptMode(JavaScriptMode.unrestricted);
    

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: WebViewWidget(
        controller: controller,
      ),
    );
  }
}

Here is error messages

答案1

得分: 0

你在那里不在一个函数内,所以不能只在那里执行代码行,只能声明变量。通过使用级联,它将其作为声明的一部分。如果你想不使用级联来写,你可以在构造函数中这样做,例如

class HomeScreen extends StatelessWidget {
  HomeScreen({super.key}){
    controller.setJavaScriptMode(JavaScriptMode.unrestricted);
  }

  final controller = WebViewController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: WebViewWidget(
        controller: controller,
      ),
    );
  }
}
英文:

You are not inside a function there so you can't just execute lines of code there, only declarations of variables. By using the cascade it makes it part of the declaration. If you want to write it without cascade you could do it in the constructor for example, like

class HomeScreen extends StatelessWidget {
  HomeScreen({super.key}){
    controller.setJavaScriptMode(JavaScriptMode.unrestricted);
  }

  final controller = WebViewController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: WebViewWidget(
        controller: controller,
      ),
    );
  }
}

</details>



huangapple
  • 本文由 发表于 2023年6月26日 19:13:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76556147.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定