为什么未收到功能列表数据
这是我的代码
// 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());
}
},
);
}
}
心脏列表小部件
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']);
},
),
);
}
问题
我从Fire Store中获取数据(这是成功的。 快照。
- 。
- 但是,stockcardlist列表已交付给心脏列表小部件,但是的值
(stockcardlist.length(); 始终为零。同样,下面的列表视图未执行我
,我认为原因是在执行之前是执行的 。数据是从Firestore传递的,但是IF语句是正常执行的,即使列表是用我之前的代码构建的,列表也正常
提供
This is my code
// 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
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']);
},
),
);
}
Problem
I get data from the fire store ( And it is successfull.
snapshot.connectionState == ConnectionState.done complete)store it in the parameter stockcardlist using add
However, the stockcardlist list was delivered to the Cardlist Widget, but the value of
print(stockcardlist.length();
is always zero. Similarly, the listview below is not executed
I thought the cause was execution before the data was delivered from the Firestore, but the if statement was executed normally and the list was delivered normally even if the list was built with the code that I did before.
please help me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论