【问题标题】:How To Add URL From Cloud Firestore Using Using url_launcher Package如何使用 url_launcher 包从 Cloud Firestore 添加 URL
【发布时间】: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>";

}

【问题讨论】:

    标签: firebase flutter dart


    【解决方案1】:

    你可以添加 make _launchURL() 来获取一个字符串值,这将是你的 url _launchURL(String url) 然后

     _launchURL(String url) async {
    
      if (await canLaunch(url)){
        await launch(url);
      } else {
        throw 'Could not launch $url';
      }
    }
    //
     IconButton(
             icon: Icon(FontAwesomeIcons.google),
              onPressed: (){    
                  _launchURL(url:record.url),// or _launchURL(record.url)}
              ),
    

    【讨论】:

      猜你喜欢
      • 2019-07-18
      • 2019-05-13
      • 1970-01-01
      • 2018-12-12
      • 1970-01-01
      • 2022-11-11
      • 2020-05-05
      • 2018-06-22
      相关资源
      最近更新 更多