晚上好。我正在尝试分配共享首选项 url,但页面未加载,它仅在我将值分配给这样的变量时才有效:
String url = 'https://www.youtube.com/';
我试了很多方法还是没能找出哪里出错了,我是flutter的新手,希望大家能帮帮我。
这是代码...
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:shared_preferences/shared_preferences.dart';
class Webpage extends StatefulWidget {
const Webpage({super.key});
@override
State<Webpage> createState() => _WebpageState();
}
class _WebpageState extends State<Webpage> {
String initialUrl = '';
@override
void initState() {
super.initState();
main();
}
String miStringGlobal = '';
Future<void> obtenerValorPrefCompartida() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
miStringGlobal = prefs.getString('link') ?? "https://www.google.com";
}
void main() async {
await obtenerValorPrefCompartida();
}
double _progress = 0;
late InAppWebViewController webView;
GlobalKey<ScaffoldState> ScaffoldKey = GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return Scaffold(
key: ScaffoldKey,
appBar: AppBar(
backgroundColor: Color(0xff041f40),
elevation: 0,
centerTitle: true,
title: Text(
"TEST",
style: TextStyle(color: const Color.fromARGB(255, 255, 255, 255)),
),
),
body: Stack(
children: [
InAppWebView(
initialUrlRequest: URLRequest(url: Uri.parse(miStringGlobal)),
onWebViewCreated: (InAppWebViewController controller) {
webView = controller;
},
onProgressChanged:
(InAppWebViewController controller, int progress) {
setState(() {
_progress = progress / 100;
});
},
),
_progress < 1
? SizedBox(
height: 3,
child: LinearProgressIndicator(
value: _progress,
backgroundColor: Colors.red.withOpacity(0.2),
),
)
: SizedBox()
],
),
);
}
}
我附上了代码,我希望你能告诉我我的错误在哪里。
改变
setState()
值时需要调用miStringGlobal
来更新UI
更新你的功能
obtenerValorPrefCompartida
到:
Future<void> obtenerValorPrefCompartida() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(() {
miStringGlobal = prefs.getString('link') ?? "https://www.google.com";
});
}
尝试改变 inti var miStringGlobal
从此
String miStringGlobal = '';
到这个String miStringGlobal = "https://www.google.com";
如果它加载谷歌那么问题是你的小部件在初始化之前开始构建,在这种情况下
添加一些加载标志
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:shared_preferences/shared_preferences.dart';
class Webpage extends StatefulWidget {
const Webpage({super.key});
@override
State<Webpage> createState() => _WebpageState();
}
class _WebpageState extends State<Webpage> {
String initialUrl = '';
bool isloading=false;
@override
void initState() {
super.initState();
isloading=true;
main();
}
String miStringGlobal = '';
Future<void> obtenerValorPrefCompartida() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
miStringGlobal = prefs.getString('link') ?? "https://www.google.com";
setState(() {
isloading=false;
});
}
void main() async {
await obtenerValorPrefCompartida();
}
double _progress = 0;
late InAppWebViewController webView;
GlobalKey<ScaffoldState> ScaffoldKey = GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return Scaffold(
key: ScaffoldKey,
appBar: AppBar(
backgroundColor: Color(0xff041f40),
elevation: 0,
centerTitle: true,
title: Text(
"TEST",
style: TextStyle(color: const Color.fromARGB(255, 255, 255, 255)),
),
),
body:isloading?Text("loading"): Stack(
children: [
InAppWebView(
initialUrlRequest: URLRequest(url: Uri.parse(miStringGlobal)),
onWebViewCreated: (InAppWebViewController controller) {
webView = controller;
},
onProgressChanged:
(InAppWebViewController controller, int progress) {
setState(() {
_progress = progress / 100;
});
},
),
_progress < 1
? SizedBox(
height: 3,
child: LinearProgressIndicator(
value: _progress,
backgroundColor: Colors.red.withOpacity(0.2),
),
)
: SizedBox()
],
),
);
}
}