[英]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.