【发布时间】:2020-06-05 23:43:06
【问题描述】:
我正在使用 Cloud Firestore 在 Flutter 中创建卡片布局。 Card 包含一个图像、两个 IconButtons 和一些文本。我希望 IconButtons 打开我在 Firebase 中作为字符串添加的 URL 链接。如何使用 url_launcher 包执行此操作?我尝试用const url = record.url 替换_launchURL 函数中的const url = 'example.com';,但哥特为record 抛出了一个未定义的错误。
我对开发和 Stackoverflow 非常陌生,所以如果我能更好地构建问题,请告诉我。谢谢!
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
IconButton(
icon: Icon(FontAwesomeIcons.youtube),
onPressed: _launchURL(),
),
IconButton(
icon: Icon(FontAwesomeIcons.google),
onPressed: _launchURL(),
),
],
),
Container(
child: Text(record.gameParagraph),
),
],
),
_launchURL() async {
const url = 'example.com'; // URL to be added from Cloud Firestore
if (await canLaunch(url)){
await launch(url);
} else {
throw 'Could not launch $url';
}
}
class Record {
final String gameParagraph;
final String url;
final DocumentReference reference;
Record.fromMap(Map<String, dynamic> map, {this.reference})
: assert(map['gameParagraph'] != null),
assert(map['url'] != null),
gameParagraph = map['gameParagraph'],
url = map['url'];
Record.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data, reference: snapshot.reference);
@override
String toString() => "Record<$gameParagraph:$url>";
}
【问题讨论】: