在Flutter 3 WebView的URL中连接字符串和变量是可能的吗?

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

Is it possible to concatenate strings and variables in a Flutter 3 WebView URL?

问题

我正在使用Flutter 3创建一个WebView页面,使用的是webview_flutter: 4.2.2。我使用的URL包含多个参数,所以我需要使用一个动态URL。我尝试了类似以下示例的方式,但在loadRequest(Uri.parse(urlfull))部分出现了错误。

以下是完整的代码:

import 'dart:html';

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

class HomeTab extends StatefulWidget {
  const HomeTab({super.key});
  @override
  // ignore: library_private_types_in_public_api
  _HomeTabState createState() => _HomeTabState();
}

class _HomeTabState extends State<HomeTab> {
  late String urlfull;

  @override
  void dispose() {
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
    startLaunching();
    // 用于刷新视图
  }

  startLaunching() async {
    final prefs = await SharedPreferences.getInstance();
    final authUserId = prefs.getString("auth_user_id")!;
    String username = prefs.getString("username")!;
    final urlfull = baseUrl + "mobile/index?q=$authUserId&u=$username";
    setState(() {});
  }

  WebViewController controller = WebViewController()
    ..setJavaScriptMode(JavaScriptMode.unrestricted)
    ..setBackgroundColor(const Color(0x00000000))
    ..setNavigationDelegate(NavigationDelegate(
      onProgress: (int progress) {},
      onPageStarted: (String url) {},
      onPageFinished: (String url) {},
      onWebResourceError: (WebResourceError error) {},
    ))
    ..enableZoom(false)
    ..loadRequest(Uri.parse(urlfull));

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: WebViewWidget(controller: controller),
      ),
    );
  }
}

在loadRequest(Uri.parse(urlfull))部分,我已经将双引号替换为单引号,以解决"Extract Method"显示的问题。

英文:

I am creating a WebView page using Flutter 3, and I'm using webview_flutter: 4.2.2. The URL I'm using contains multiple parameters, so I need to use a dynamic URL. I've tried something like the following example, but there's an error in the loadRequest(Uri.parse(urlfull)) part.

Here's the complete code:
import 'dart:html';

import &#39;package:flutter/material.dart&#39;;
import &#39;package:webview_flutter/webview_flutter.dart&#39;;
import &#39;package:shared_preferences/shared_preferences.dart&#39;;
import &#39;../config.dart&#39;;

class HomeTab extends StatefulWidget {
  const HomeTab({super.key});
  @override
  // ignore: library_private_types_in_public_api
  _HomeTabState createState() =&gt; _HomeTabState();
}

class _HomeTabState extends State&lt;HomeTab&gt; {
  late String urlfull;

  @override
  void dispose() {
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
    startLaunching();
    //untuk melakukan refresh tampilan
  }

  startLaunching() async {
    final prefs = await SharedPreferences.getInstance();
    final authUserId = prefs.getString(&quot;auth_user_id&quot;)!;
    String username = prefs.getString(&quot;username&quot;)!;
    final urlfull = baseUrl + &quot;mobile/index?q=$authUserId&amp;u=$username&quot;;
    setState(() {});
  }

  WebViewController controller = WebViewController()
    ..setJavaScriptMode(JavaScriptMode.unrestricted)
    ..setBackgroundColor(const Color(0x00000000))
    ..setNavigationDelegate(NavigationDelegate(
      onProgress: (int progress) {},
      onPageStarted: (String url) {},
      onPageFinished: (String url) {},
      onWebResourceError: (WebResourceError error) {},
    ))
    ..enableZoom(false)
    ..loadRequest(Uri.parse(urlfull));

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        home: Scaffold(
          body: WebViewWidget(controller: controller),
        ));
  }
}

I have tried editing in several ways in the loadRequest(Uri.parse(urlfull)) part, but it always displays "Extract Method".

答案1

得分: 3

你实际上有两个名为urlfull的变量。在startLaunching中,你应该可能在urlfull之前移除"final",这样你就会更新你的状态变量urlfull,而不是一个新的urlfull变量。

如果你在pubspec.yaml文件中为lintflutter_lints添加dev_dependencies,你的IDE将突出显示潜在的错误,就像这个错误一样。

英文:

You actually have two variables named urlfull. Within startLaunching you should probably remove "final" before urlfull so that you are updating your state variable urlfull instead of a new urlfull variable.

If you add dev_dependencies in your pubspec.yaml file for lint and flutter_lints your IDE will highlight potential errors like this one.

huangapple
  • 本文由 发表于 2023年8月9日 08:57:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76863949-2.html
匿名

发表评论

匿名网友

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

确定