各位晚上好
我在使用 URL 启动器时遇到了一个小问题。基本上,我想要做的是从 Future 函数(从 API 检索)获取 URL,然后在按下凸起按钮后导航到该 URL。我实现的代码有效,但并非没有一个小而讨厌的错误。该错误出现大约半秒到一秒(我假设直到 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
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句