LoginSignup
2
0

More than 1 year has passed since last update.

初心者、リンクを埋める

Last updated at Posted at 2022-12-14

userとしては毎日やっていることなのに・・・

 あまりに当たり前で、きっと簡単だ、となめていたら、なかなかうまくいかなかった。 
 やりたいことは、リンクを埋め込んで、他サイトに飛ぶ。 

「未知×未知」は沼の入り口

 何が問題だったかというと、基礎知識の足らない初心者には、前後関係のわからない記事ばかりだったこと。このcodeは全体のどこにどう組み込めばいいんだ? 
 もう一つは、単純にURLを埋めるのではなく、firestoreから取ってきたデータを埋める作業だったこと。ここで「型ってなんなんだ」問題にはまる。 

この関数はどこに書くのか 

 まずはこちらを参考に。ほかにも記事はいろいろあったが、これが一番シンプルだった。
 シンプルはシンプルなんだが、シンプルすぎて初心者泣かせ。自分のcodeのどの辺に入れたらいいか悩む。 

Future onLaunchUrl () async {
  final url = "https://www.google.com/";
  if (await canLaunch(url)) {
    await launch(url);
  }
}

そんなこともわかってなくて、よく毎日コード書いているな、という・・(>_<)

StringはUriではない、ObjectはUriではない、それはStringではない etc

 もう一つ、Samplecodeのurl部分にfirestoreから取ってきたList内のデータを入れたい。 

storeUrl = doc.data()!['store_url'];

/// 商品ページurl
String? storeUrl;

 すると、型違いだというエラーメッセージが出る。で、String? をUri?にしてみたり、あっちいじって、こっち変えて、右往左往した挙げ句に、もう一つの記事に出会う。 

 こうしなさいって。 

final response = await http.get(Uri.parse('https://randomuser.me/api/?results=20'));

ところがここにstoreUrlを入れると、また、型が違うぞという。そうか、さっきUri?に変えたのが徒らしい。

はい、お疲れ様でした。着地点はこちら。

class Item {
  Item(DocumentSnapshot<Map<String, dynamic>> doc) {
    Firebase.initializeApp();
    storeUrl = doc.data()!['store_url'];
         ーー略ーー
  }

  /// 商品ページurl
  String? storeUrl;
            ーー略ーー 
class ResultPage extends StatelessWidget {
  const ResultPage({super.key, required this.item});
  final Item item;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.orange[50],
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Padding(
              padding: const EdgeInsets.all(20.0),
              child: Card(
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(36),
                ),
                child: Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: Column(
                            ーー略ーー
             
                      const SizedBox(height: 8),
                      SizedBox(
                        width: 123,
                        height: 36,
                        child: ElevatedButton(
                            style: ElevatedButton.styleFrom(
                              shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(10),
                              ),
                            ),
           ここ=>           onPressed: () {onLaunchUrl();},
                            child: const Text(
                              'Link',
                              style: TextStyle(
                                fontSize: 20,
                                color: Colors.white,
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                          ),
                        ),
                               ーー略ーー
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

// そして、ここ 

  Future onLaunchUrl () async {
    final url = Uri.parse(item.storeUrl ?? "");
    if (await canLaunchUrl(url)) {
      await launchUrl(url);
    }
  }
}

あ、url_launcher ^6.1.7 をお忘れなく

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0