提问人:idkanything 提问时间:4/28/2022 更新时间:4/28/2022 访问量:68
为什么函数列表数据没有收到
Why function List data is not received out
问:
这是我的代码
// Copyright 2018 The Flutter team. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// ignore_for_file: prefer_const_literals_to_create_immutables, non_constant_identifier_names, prefer_const_constructors_in_immutables, constant_identifier_names, prefer_typing_uninitialized_variables, unnecessary_string_interpolations, prefer_const_constructors
import 'package:flutter/material.dart';
import 'package:flutter_application_1/Components/main_app_bar.dart';
import 'package:flutter_application_1/Components/setting_button.dart';
import 'package:flutter_application_1/Components/stock_list.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
class InterestScreen extends StatefulWidget {
InterestScreen({Key? key}) : super(key: key);
@override
State<InterestScreen> createState() => _InterestScreenState();
}
class _InterestScreenState extends State<InterestScreen> {
FirebaseFirestore firestore = FirebaseFirestore.instance;
Future<void> _getstockList( List<Map<String,dynamic>> stockcardlist) async {
Map<String, dynamic> docdata;
Map<String, dynamic> stockdata;
await firestore
.collection('users')
// user의 device token
.doc('NVPjZEAZneKblrubGZSW')
.get()
.then((DocumentSnapshot ds) {
docdata = ds.data() as Map<String, dynamic>;
List<dynamic> list = docdata['favorite'];
for(var element in list){
firestore
.collection('stock')
.where("name", isEqualTo: "${element}")
.get()
.then((QuerySnapshot qs) {
stockdata = qs.docs[0].data() as Map<String, dynamic>;
stockcardlist.add(stockdata);
});
}
});
}
@override
Widget build(BuildContext context) {
List<Map<String, dynamic>> stockcardlist = [];
Size size = MediaQuery.of(context).size;
return FutureBuilder(
future: _getstockList(stockcardlist),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.connectionState == ConnectionState.done ) {
return Scaffold(
appBar: mainAppBar(
context,
"관심 종목",
SettingButton(context),
),
body: Column(
children: [
Cardlist(size, stockcardlist),
],
),
);
} else {
return Center(child: CircularProgressIndicator());
}
},
);
}
}
CardList 小部件
Widget Cardlist(Size size, List<Map<String, dynamic>> stockcardlist ) {
print(stockcardlist.length();
return Expanded(
child: ListView.builder(
scrollDirection: Axis.vertical,
padding: EdgeInsets.symmetric(horizontal: size.width * 0.05),
itemCount: stockcardlist.length,
itemBuilder: (BuildContext context, int index) {
return Stockcard(context, size, stockcardlist[index]['name'], stockcardlist[index]['price'],
stockcardlist[index]['perc'], stockcardlist[index]['volume']);
},
),
);
}
问题
我从消防商店获取数据(并且成功了。 snapshot.connectionState == ConnectionState.done 完成)
使用 add 将其存储在参数 stockcardlist 中
但是,stockcardlist 列表被传送到 Cardlist Widget,但
print(stockcardlist.length();
始终为零。同样,下面的列表视图也不会执行
我以为原因是在从Firestore交付数据之前执行,但是if语句正常执行,即使列表是用我之前做的代码构建的,列表也正常交付。
请帮帮我
答: 暂无答案
评论
unawaited_futures
棉绒。此外,您应该避免使用 ,而只是使用 ,这也可以避免这个问题。for
.then((QuerySnapshot qs) ...
Future
await
_getStocklist
Future
Future.then
await
Future
await
unawaited_future
Future
await
unawaited