実現したいこと
package:url_launcherを利用してメーラーを起動させる。
前提
スマホで動くアプリを作ろう!ゼロから始めるFlutter実践入門編 ②: 様々なパッケージを使ってみよう(渋谷 エミリ 著)
↑上記、書籍でFlutterの学習中です。
packageの利用方法が説明されている箇所で、url_launcherを使用しています。
書籍の中で掲示されているサンプルコードがエラーになってしまい、対処法がわからず困っております。
発生している問題・エラーメッセージ
Dart
1: Error: The argument type 'Uri' can't be assigned to the parameter type 'String'. 2main.dart:17 3 - 'Uri' is from 'dart:core'. 4 if (!await launchURL(Uri.parse(url))) { 5 ^ 6: Error: This expression has type 'void' and can't be used. 7main.dart:17 8 if (!await launchURL(Uri.parse(url))) { 9 ^ 10: Error: The argument type 'String' can't be assigned to the parameter type 'Uri'. 11main.dart:41 12 - 'Uri' is from 'dart:core'. 13 launchUrl('mailto:${mailAddress}?subject=${mailTitle}&body=${mailContents}');
該当のソースコード
Dart
1import 'package:flutter/material.dart'; 2import 'package:url_launcher/url_launcher.dart'; 3import 'package:url_launcher/link.dart'; 4 5void main() { 6 runApp(myApp()); 7} 8 9class myApp extends StatelessWidget { 10 const myApp({Key? key}) : super(key: key); 11 12 final String mailAddress = 'hoge@ne.jp'; 13 final String mailTitle = '件名です'; 14 final String mailContents = '本文です'; 15 16 Future<void> launchURL(String url) async { 17 if (!await launchURL(Uri.parse(url))) { 18 throw '${url}が表示されません'; 19 } 20 } 21 22 23 Widget build(BuildContext context) { 24 return MaterialApp( 25 home: Scaffold( 26 appBar: AppBar( 27 centerTitle: true, // 中央寄せを設定 28 title: Text('My Profile'), 29 ), 30 body: Center( 31 child: Column( 32 children: [ 33 Image.asset('assets/icon.png', height: 200), 34 Text('hoge hogeo'), 35 Row(children: [Text('所属:'), Text('株式会社hoge')]), 36 Row(children: [Text('電話:'), Text('090-0000-0000')]), 37 Row(children: [ 38 Text('メール:'), 39 Text('hoge@ne.jp'), 40 TextButton(onPressed: () async { 41 launchUrl('mailto:${mailAddress}?subject=${mailTitle}&body=${mailContents}'); 42 }, child: Icon(Icons.mail))]), 43 Row(children: [Text('WebSite:'), Text('http://hoge.com')]), 44 ], 45 ), 46 ), 47 ), 48 ); 49 } 50} 51
試したこと
・https://pub.dev/packages/url_launcherや、同ページ内の例を参照したが、具体的な対処法がわからなかった。
・final String url= 'www.hoge.com';とurlにあらかじめ値を入力させたがエラー表示となった。
回答2件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。