[英]Url Launcher using Flutter/Dart
各位晚安,
我对 URL 启动器有一点小问题。 基本上,我想做的是从 Future function (从 API 检索)获取 URL (从 API 检索),然后在按下凸起的按钮 D 后导航到该 ZE6B391A8D2C4D45902A23A8B6585703。 我实现的代码有效,但并非没有一个小而讨厌的错误。 该错误出现大约半秒到一秒(我假设直到 API 返回 URL),然后将凸起的按钮绘制到屏幕上并且工作正常; 然后我可以导航到该站点。 我试图完全摆脱这个错误。 为了节省您一些时间,相关的 FutureBuilder 是第二个。 下面是我的代码:
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,),
),
);
}
},
),
),
)
],
),
);
}
}
在此先感谢您提供有关此问题的所有帮助!
在第二个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.