简体   繁体   English

Url 使用 Flutter/Dart 的启动器

[英]Url Launcher using Flutter/Dart

Good evening folks,各位晚安,

I'm having a slight issue with URL launcher.我对 URL 启动器有一点小问题。 Basically, what I am trying to do is get a URL from a Future function (retrieved from an API) and then navigate to that URL once a raised button is pressed.基本上,我想做的是从 Future function (从 API 检索)获取 URL (从 API 检索),然后在按下凸起的按钮 D 后导航到该 ZE6B391A8D2C4D45902A23A8B6585703。 The code I have implemented works, but not without a small, pesky bug.我实现的代码有效,但并非没有一个小而讨厌的错误。 The bug appears for about half a second to a second (I'm assuming until the API returns the URL), then the raised button is drawn on to the screen and works fine;该错误出现大约半秒到一秒(我假设直到 API 返回 URL),然后将凸起的按钮绘制到屏幕上并且工作正常; I'm then able to navigate to the site.然后我可以导航到该站点。 I'm trying to get rid of the bug completely.我试图完全摆脱这个错误。 To save you some time, the relevant FutureBuilder is the second one.为了节省您一些时间,相关的 FutureBuilder 是第二个。 Below is my code:下面是我的代码:

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart';

import '../models/yahoo_finance_stock_info.dart';
import '../providers/companies_provider.dart';

class BusinessScreen extends StatefulWidget {
  static const routeName = '/business-screen';

  @override
  _BusinessScreenState createState() => _BusinessScreenState();
}

class _BusinessScreenState extends State<BusinessScreen>
    with AutomaticKeepAliveClientMixin<BusinessScreen> {
  @override
  bool get wantKeepAlive => true;

  Future _companyDescription;
  Future _urlForUrlLauncher;

  var _isInit = true;

  @override
  void didChangeDependencies() {
    if (_isInit) {
      final companyTicker = ModalRoute.of(context).settings.arguments as String;
      final loadedCompany = Provider.of<Companies>(context, listen: false)
          .findByTicker(companyTicker);

      _companyDescription =
          Companies().getSecurityExtendStats(loadedCompany.tickerSymbol);

      _urlForUrlLauncher =
          Companies().getHistoricalData(loadedCompany.tickerSymbol);
    }
    _isInit = false;
    super.didChangeDependencies();
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _companyDescription;
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    final companyTicker = ModalRoute.of(context).settings.arguments as String;
    final loadedCompany = Provider.of<Companies>(context, listen: false)
        .findByTicker(companyTicker);

    return Container(
      color: Colors.black87,
      child: Column(
        children: <Widget>[
          Container(
            height: 300,
            width: double.infinity,
            color: Colors.white,
            padding: EdgeInsets.all(10),
            child: SingleChildScrollView(
              padding: EdgeInsets.only(left: 15),
              scrollDirection: Axis.vertical,
              child: FutureBuilder<StockInformation>(
                future: _companyDescription,
                builder: (BuildContext context,
                    AsyncSnapshot<StockInformation> snapshot) {
                  if (snapshot.data != null) {
                    return Text(snapshot.data.assetProfile.longBusinessSummary);
                  }
                  return Container(
                    height: 300,
                    child: Center(
                      child: CircularProgressIndicator(),
                    ),
                  );
                },
              ),
            ),
          ),
          Container(
            height: 75,
            width: double.infinity,
            child: Center(
              child: FutureBuilder(
                future: _urlForUrlLauncher,
                builder: (BuildContext context, AsyncSnapshot snapshot) {
                  if (snapshot.data[0]['finalLink'] != null) {
                    String url10K = snapshot.data[0]['finalLink'];
                    return RaisedButton(
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(10),
                        side: BorderSide(
                          color: Colors.grey,
                          width: 1,
                        ),
                      ),
                      onPressed: () async {
                        var url = url10K;
                        if (await canLaunch(url)) {
                          await launch(url);
                        } else {
                          throw 'Could not launch $url';
                        }
                      },
                      child: Text(
                        'Go to company\'s filings',
                        style: TextStyle(
                          color: Colors.white,
                        ),
                      ),
                    );
                  } else {
                    return RaisedButton(
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(10),
                        side: BorderSide(
                          color: Colors.grey,
                          width: 1,
                        ),
                      ),
                      onPressed: null,
                      child: Text(
                        'Go to company\'s filings',
                        style: TextStyle(color: Colors.white,),
                      ),
                    );
                  }
                },
              ),
            ),
          )
        ],
      ),
    );
  }
}

Thanks in advance for any and all help with this issue!在此先感谢您提供有关此问题的所有帮助!

In the second FutureBuilder change the condition,在第二个FutureBuilder中更改条件,

builder: (BuildContext context, AsyncSnapshot snapshot) {
  if (snapshot.data != null && snapshot.data[0]['finalLink'] != null) {
    // raised button with onpressed
  } else {
    // raised button with onpressed as null
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM