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

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

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({Key? key}) : super(key: key);

  @override
  _HomeTabState createState() => _HomeTabState();
}

class _HomeTabState extends State<HomeTab> {
  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("auth_user_id")!;
    String username = prefs.getString("username")!;
    final urlfull = baseUrl + "mobile/index?q=$authUserId&u=$username";
    setState(() {});
  }

  late WebViewController controller;

  @override
  Widget build(BuildContext context) {
    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));

    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.html
匿名

发表评论

匿名网友

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

确定