当应用程序终止并重新启动时,我面临着在 Flutter 中维护 WebView 缓存的挑战。在典型的 Web 场景中,由于缓存的内容,重新打开以前访问过的页面加载速度更快。在我的 Flutter WebView 中,即使终止并重新打开应用程序后,WebView 内容的加载时间也与最初一样长。
我使用了 webview_flutter 包来加载网页内容,但缓存在应用程序终止后似乎不会保留。这是我的代码:
class WebProvider extends ChangeNotifier {
late WebViewController controller;
bool _isWebViewLoading = true;
final Auth0 auth0 = Auth0(AppConfig.DOMAIN, AppConfig.CLIENTID);
bool get isWebViewLoading => _isWebViewLoading;
void loadWebView({String? url}) {
final dashboardUrl = Uri.parse(AppConfig.login).replace(queryParameters: {
'access_token': PrefUtils.getString(ACCESS_TOKEN),
'token_type': PrefUtils.getString(TOKEN_TYPE),
'refresh_token': PrefUtils.getString(REFRESH_TOKEN),
'expires_in': PrefUtils.getString(EXPIRES_AT),
'id_token': PrefUtils.getString(ID_TOKEN),
'scope': PrefUtils.getString(SCOPES)
}).toString();
controller = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setNavigationDelegate(NavigationDelegate(
onPageStarted: (url) async {
_isWebViewLoading = true;
notifyListeners();
},
onPageFinished: (url) async {
log("url on finish: $url");
_isWebViewLoading = false;
notifyListeners();
},
onNavigationRequest: (navigation) async {
if (navigation.url.contains(AppConfig.LOGOUT_URL)) {
if (PrefUtils.getBool(SOCIAL_LOGIN) == true) {
try {
//注销码 } 捕捉 (e) {} }
PrefUtils.removeValue(ACCESS_TOKEN);
PrefUtils.removeValue(USER_EMAIL);
PrefUtils.setBool(SOCIAL_LOGIN, false);
// redirect to login page
Get.offAll(() => const LoginPage());
}
return NavigationDecision.prevent;
},
))
..loadRequest(Uri.parse(dashboardUrl))
..setBackgroundColor(
Colors.transparent); // Set background color if needed
}
}
我还尝试了 flutter_inappwebview 包和 flutter_webview_plugin ,启用应用程序缓存,但问题仍然存在,网页需要相当长的时间才能加载。 使用 flutter_webview_plugin 编写代码:
class WebProvider extends ChangeNotifier {
final FlutterWebviewPlugin flutterWebviewPlugin = FlutterWebviewPlugin();
bool _isWebViewLoading = true;
final Auth0 auth0 = Auth0(AppConfig.DOMAIN, AppConfig.CLIENTID);
bool get isWebViewLoading => _isWebViewLoading;
void loadWebView({String? url}) {
final dashboardUrl = Uri.parse(AppConfig.login).replace(queryParameters: {
'access_token': PrefUtils.getString(ACCESS_TOKEN),
'token_type': PrefUtils.getString(TOKEN_TYPE),
'refresh_token': PrefUtils.getString(REFRESH_TOKEN),
'expires_in': PrefUtils.getString(EXPIRES_AT),
'id_token': PrefUtils.getString(ID_TOKEN),
'scope': PrefUtils.getString(SCOPES)
}).toString();
flutterWebviewPlugin.launch(
dashboardUrl,
appCacheEnabled: true,
userAgent:
'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Mobile Safari/537.36',
);
flutterWebviewPlugin.onStateChanged
.listen((WebViewStateChanged state) async {
if (state.type == WebViewState.startLoad) {
_isWebViewLoading = true;
notifyListeners();
} else if (state.type == WebViewState.finishLoad) {
_isWebViewLoading = false;
notifyListeners();
}
});
// Additional listeners can be added as needed, such as for URL changes
flutterWebviewPlugin.onUrlChanged.listen((String url) {
if (url.contains(AppConfig.LOGOUT_URL)) {
// Implement logout logic
// Redirect to login page, etc.
}
});
}
@override
void dispose() {
flutterWebviewPlugin.dispose();
super.dispose();
}
}
有人可以建议我可能做错了什么,或者提出一种替代方法来在应用程序重新启动时实现更快的 WebView 加载吗?谢谢你。