英文:
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 '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();
//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(() {});
}
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文件中为lint和flutter_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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论