Flutter,使用提供者的 WebView 空页面
Flutter, WebView empty page using Provider
我在 MyApp 上安装了 Provider,但 webview 打开时仍然是空白屏幕。
没有错误,没有建议,它只是打开一个空白屏幕而不是加载。
如果我在 url 中输入一个网址可以正常工作,但我想要这个动态。
runApp(
Provider<Events>.value(
value: Events(),
child: MyApp(),
),
);
class Events {
final String imagePath, site;
Events({
this.imagePath, this.site
});
final events = [
castel,
lake,
];
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:provider/provider.dart';
import '../model/events.dart';
import './flutter_web.dart';
class Site extends StatelessWidget {
@override
Widget build(BuildContext context) {
final events = Provider.of<Events>(context);
return Container(
padding: const EdgeInsets.all(4.0),
child: Container(
child: IconButton(
onPressed: () => Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => FlutterWeb(events.site),
),
),
icon: Icon(
FontAwesomeIcons.internetExplorer,
size: 30.0,
color: Colors.lightBlue,
),
),
),
);
}
}
return WebView(
initialUrl: events.site,
)
您可以复制粘贴 运行 下面的完整代码
在 value
属性中,您需要传递变量而不是 class
在你的代码片段中 events
是 array
它可能是一个错字
代码片段
void main() {
final events = Events(imagePath: "castel", site: "https://flutter.dev/");
runApp(
Provider<Events>.value(
value: events,
child: MyApp(),
),
);
}
工作演示
完整代码
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:webview_flutter/webview_flutter.dart';
class Events {
final String imagePath, site;
Events({this.imagePath, this.site});
}
void main() {
final events = Events(imagePath: "castel", site: "https://flutter.dev/");
runApp(
Provider<Events>.value(
value: events,
child: MyApp(),
),
);
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Site());
}
}
class Site extends StatelessWidget {
@override
Widget build(BuildContext context) {
var events = Provider.of<Events>(context);
return Scaffold(
body: Center(
child: Container(
padding: const EdgeInsets.all(4.0),
child: Container(
child: IconButton(
onPressed: () => Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => FlutterWeb(events.site),
),
),
icon: Icon(
FontAwesomeIcons.internetExplorer,
size: 30.0,
color: Colors.lightBlue,
),
),
),
),
),
);
}
}
class FlutterWeb extends StatelessWidget {
String site;
FlutterWeb(this.site);
@override
Widget build(BuildContext context) {
return WebView(
initialUrl: site,
);
}
}
我在 MyApp 上安装了 Provider,但 webview 打开时仍然是空白屏幕。 没有错误,没有建议,它只是打开一个空白屏幕而不是加载。 如果我在 url 中输入一个网址可以正常工作,但我想要这个动态。
runApp(
Provider<Events>.value(
value: Events(),
child: MyApp(),
),
);
class Events {
final String imagePath, site;
Events({
this.imagePath, this.site
});
final events = [
castel,
lake,
];
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:provider/provider.dart';
import '../model/events.dart';
import './flutter_web.dart';
class Site extends StatelessWidget {
@override
Widget build(BuildContext context) {
final events = Provider.of<Events>(context);
return Container(
padding: const EdgeInsets.all(4.0),
child: Container(
child: IconButton(
onPressed: () => Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => FlutterWeb(events.site),
),
),
icon: Icon(
FontAwesomeIcons.internetExplorer,
size: 30.0,
color: Colors.lightBlue,
),
),
),
);
}
}
return WebView(
initialUrl: events.site,
)
您可以复制粘贴 运行 下面的完整代码
在 value
属性中,您需要传递变量而不是 class
在你的代码片段中 events
是 array
它可能是一个错字
代码片段
void main() {
final events = Events(imagePath: "castel", site: "https://flutter.dev/");
runApp(
Provider<Events>.value(
value: events,
child: MyApp(),
),
);
}
工作演示
完整代码
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:webview_flutter/webview_flutter.dart';
class Events {
final String imagePath, site;
Events({this.imagePath, this.site});
}
void main() {
final events = Events(imagePath: "castel", site: "https://flutter.dev/");
runApp(
Provider<Events>.value(
value: events,
child: MyApp(),
),
);
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Site());
}
}
class Site extends StatelessWidget {
@override
Widget build(BuildContext context) {
var events = Provider.of<Events>(context);
return Scaffold(
body: Center(
child: Container(
padding: const EdgeInsets.all(4.0),
child: Container(
child: IconButton(
onPressed: () => Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => FlutterWeb(events.site),
),
),
icon: Icon(
FontAwesomeIcons.internetExplorer,
size: 30.0,
color: Colors.lightBlue,
),
),
),
),
),
);
}
}
class FlutterWeb extends StatelessWidget {
String site;
FlutterWeb(this.site);
@override
Widget build(BuildContext context) {
return WebView(
initialUrl: site,
);
}
}