Flutterで同時実行を処理しようとしているので、サーバーから3つのget-requestがあり、それらから同時に値を取得する必要があります。各リクエストはモデルに解析されました。ここで、すべてのモデルを3つのget-requestを使用して1つのリストに結合し、これをListView.builderの最終リストで実行する方法を理解しようとしています。また、APIで述べたように、すべてのリクエストはネストされてIDに依存するため、3つのリストすべてを同時に解析する以外に、それらをフィルターで除外する必要があるため、私のような初心者にとっては非常に困難です。どうすればこれを解決できますか?
これは私のモデルです:
ScheduleVariants{
final int mrId;
final int mvId;
ScheduleVariants({this.mrId, this.mvId});
}
FlightCard{
final int mvId;
final int stId;
FlightCard({this.mrId, this.stId});
}
Stop{
final int stId;
Stop({this.stId})
}
Stopモデルから最終値を取得する必要があります。ご覧のとおり、すべてのモデルにネストされた構造があり、これを回避することはできません。
今、私は次のような同時呼び出しを行おうとしています:
class Dire extends StatefulWidget {
final int mrId;
final int mvId;
final int stId;
const Dire({Key key, this.mrId, this.mvId, this.stId}) : super(key: key);
@override
_DireState createState() => _DireState();
}
class _DireState extends State<Dire> {
@override
void initState() {
fetchData();
super.initState();
stops.where((element) => element.stId == widget.stId).toList();
card.where((element) => element.mvId == widget.mvId).toList();
sheduler.where((element) => element.mrId == widget.mrId).toList();
}
List<ScheduleVariants> sheduler;
List<FlightCard> card;
List<Stop> stops;
Future fetchData() async {
String username = '';
String password = '';
String basicAuth =
'Basic ' + base64Encode(utf8.encode('$username:$password'));
print(basicAuth);
final result = await Future.wait([
http.get(
Uri.parse(
"http://mysecurelink/getMarshVariants.php?fmt=json"),
headers: <String, String>{'authorization': basicAuth}),
http.get(
Uri.parse(
"http://mysecurelink/getFlightCard.php?fmt=json&mv_id"),
headers: <String, String>{'authorization': basicAuth}),
http.get(
Uri.parse(
"http://mysecurelink/getStops.php?fmt=json"),
headers: <String, String>{'authorization': basicAuth}),
]);
setState(() {
sheduler = json.decode(result[0].body) as List;
card = json.decode(result[1].body) as List;
stops = json.decode(result[2].body) as List;
});
}
@override
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: FutureBuilder(
future: fetchData(),
builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: stops.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(stops[index].stTitle),
);
});
} else {
return CircularProgressIndicator();
}
}));
}}
メインタスクの最後に、IDでフィルタリングされた3つの並列リクエストを実行し、Stopsモデルからデータを取得します。どうすればそれを正しく行うことができますか?
私は初心者で、それを適切に行う方法がわかりません。誰かがこのタスクを理解するのを手伝ってくれたら本当にうれしいです。
十分な情報がないので、私はいくつかの仮定をするつもりです:
Dire
三つのクラスの組み合わせであるScheduleVariants
、FlightCard
そしてStop
どこScheduleVariants.mvId == FlightCard.mvId
とFlightCard.stId == Stop.stId
。ScheduleVariants
が一意でありmvId
、すべてFlightCards
が一意でmvId
ありstId
、すべてStops
が一意であるstId
。複数の非同期リクエストを実行する方法に問題はありません。Future.wait
この場合、先物のリストを取り込んで、応答のリストを返します。あなたが直面している問題は、3つのAPIリクエストからの応答をマージする方法がわからないということです。
あなたはまた、国家の使用と先物の使用を混同しているようです。少なくとも、提供したコードスニペットでは、初期化後に状態を変更する必要はないようです。つまり、状態を使用する必要はまったくありません。
Dire
モデルクラスである必要があります。
class Dire {
final ScheduleVariants scheduleVariant;
final FlightCard flightCard;
final Stop stop;
Dire(this.scheduleVariant, this.flightCard, this.stop);
}
Dires
APIからを取得したいウィジェットでは、この関数をFutureBuilder
:で使用できます。
Future<List<Dire>> fetchData() async {
String username = '';
String password = '';
String basicAuth =
'Basic ' + base64Encode(utf8.encode('$username:$password'));
print(basicAuth);
final result = await Future.wait([
http.get(
Uri.parse(
"http://mysecurelink/getMarshVariants.php?fmt=json"),
headers: <String, String>{'authorization': basicAuth}),
http.get(
Uri.parse(
"http://mysecurelink/getFlightCard.php?fmt=json&mv_id"),
headers: <String, String>{'authorization': basicAuth}),
http.get(
Uri.parse(
"http://mysecurelink/getStops.php?fmt=json"),
headers: <String, String>{'authorization': basicAuth}),
]);
flightCardMap = HashMap.fromIterable(json.decode(result[1].body), (fc) => fc["mvId"], (fc) => FlightCard(fc));
stopMap = HashMap.fromIterable(json.decode(result[2].body), (s) => s["stId"], (s) => Stop(s));
return json.decode(result[0].body).map((sv) => {
flightCard = flightCardMap[sv["mvId"]];
return Dire(ScheduleVariants(sv), flightCard, stopMap[flightCard["stId"]]);
}).toList();
}
免責事項:このコードスニペットで構文エラーをチェックしなかったため、いくつかある可能性がありますが、一般的な考え方はそこにあります。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加