(Flutter) 如果没有互联网连接,如何启动静态 html 页面而不是 URL?
(Flutter) How to launch static html page instead of URL if there is no internet connection?
我已经安装了 flutter_webview_plugin。如果没有互联网连接,我正在尝试启动自定义静态 html 页面而不是我的 URL(我的网站 'wwww.duevents.in'),因为 "Web Page not available" 页面看起来不非常专业。
我正在使用它来检查设备上的互联网,它工作正常('connectionStatus ==true' 当互联网连接时,反之亦然):
Future check() async {
try {
final result = await InternetAddress.lookup('google.com');
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
connectionStatus = true;
print("connected $connectionStatus");
}
} on SocketException catch (_) {
connectionStatus = false;
print("not connected $connectionStatus");
}
}
如果没有互联网连接,这是我备用 URL 加载的代码:
WebviewScaffold(
url: connectionStatus == true ?"http://www.duevents.in" : Uri.dataFromString('<html><body>hello world</body></html>', mimeType: 'text/html').toString())
不管设备是否有互联网连接,它总是显示带有此代码的 HTML 页面。请告诉我这里有什么问题。
@Mazin Ibrahim 在上面的评论中提供的解决方案对我有用。
所以我在这里发布解决方案:
FutureBuilder(
future: check(), // a previously-obtained Future or null
builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
if (connectionStatus == true) {
//if Internet is connected
return SafeArea(
child: WebviewScaffold(
url: "http://www.duevents.in"))}
else{
//If internet is not connected
return SafeArea(
child: WebviewScaffold(
url: Uri.dataFromString('<html><body>hello world</body></html>',
mimeType: 'text/html').toString()) }})
我建议您直接将 check()
方法更改为 return 和 URL。
Future<String> getURL() async {
try {
final result = await InternetAddress.lookup('google.com');
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
return "http://www.duevents.in";
}
} on SocketException catch (_) {
return Uri.dataFromString('<html><body>hello world</body></html>', mimeType: 'text/html').toString();
}
}
那么在 FutureBuilder
中你可以直接使用 URL returned。
FutureBuilder(
future: getURL(), // a previously-obtained Future or null
builder: (BuildContext context, String url) {
return SafeArea(
child: WebviewScaffold(
url: url))}
})
我已经安装了 flutter_webview_plugin。如果没有互联网连接,我正在尝试启动自定义静态 html 页面而不是我的 URL(我的网站 'wwww.duevents.in'),因为 "Web Page not available" 页面看起来不非常专业。
我正在使用它来检查设备上的互联网,它工作正常('connectionStatus ==true' 当互联网连接时,反之亦然):
Future check() async {
try {
final result = await InternetAddress.lookup('google.com');
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
connectionStatus = true;
print("connected $connectionStatus");
}
} on SocketException catch (_) {
connectionStatus = false;
print("not connected $connectionStatus");
}
}
如果没有互联网连接,这是我备用 URL 加载的代码:
WebviewScaffold(
url: connectionStatus == true ?"http://www.duevents.in" : Uri.dataFromString('<html><body>hello world</body></html>', mimeType: 'text/html').toString())
不管设备是否有互联网连接,它总是显示带有此代码的 HTML 页面。请告诉我这里有什么问题。
@Mazin Ibrahim 在上面的评论中提供的解决方案对我有用。
所以我在这里发布解决方案:
FutureBuilder(
future: check(), // a previously-obtained Future or null
builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
if (connectionStatus == true) {
//if Internet is connected
return SafeArea(
child: WebviewScaffold(
url: "http://www.duevents.in"))}
else{
//If internet is not connected
return SafeArea(
child: WebviewScaffold(
url: Uri.dataFromString('<html><body>hello world</body></html>',
mimeType: 'text/html').toString()) }})
我建议您直接将 check()
方法更改为 return 和 URL。
Future<String> getURL() async {
try {
final result = await InternetAddress.lookup('google.com');
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
return "http://www.duevents.in";
}
} on SocketException catch (_) {
return Uri.dataFromString('<html><body>hello world</body></html>', mimeType: 'text/html').toString();
}
}
那么在 FutureBuilder
中你可以直接使用 URL returned。
FutureBuilder(
future: getURL(), // a previously-obtained Future or null
builder: (BuildContext context, String url) {
return SafeArea(
child: WebviewScaffold(
url: url))}
})