为什么函数列表数据没有收到

Why function List data is not received out

提问人:idkanything 提问时间:4/28/2022 更新时间:4/28/2022 访问量:68

问:

这是我的代码

// 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']);
              },
            ),
        );
}

问题

  1. 我从消防商店获取数据(并且成功了。 snapshot.connectionState == ConnectionState.done 完成)

  2. 使用 add 将其存储在参数 stockcardlist 中

  3. 但是,stockcardlist 列表被传送到 Cardlist Widget,但

print(stockcardlist.length();始终为零。同样,下面的列表视图也不会执行

我以为原因是在从Firestore交付数据之前执行,但是if语句正常执行,即使列表是用我之前做的代码构建的,列表也正常交付。

请帮帮我

列出 Flutter dart 参数 passby-reference

评论

0赞 jamesdlin 4/28/2022
你的循环有 ,但返回的永远不会被编辑,所以会在这些 s 完成之前返回。我强烈建议启用unawaited_futures棉绒。此外,您应该避免使用 ,而只是使用 ,这也可以避免这个问题。for.then((QuerySnapshot qs) ...Futureawait_getStocklistFutureFuture.thenawait
0赞 idkanything 4/28/2022
@jamesdlin真的很感谢你的答案,但我不明白你为什么推荐unawaited_furtures。我发现这意味着“习惯于故意不等待”。但是我的问题是由 unwait 造成的,并且没有返回列表。不是吗?
0赞 jamesdlin 4/28/2022
你的问题是你创建了一个你忘记的.s 将警告您任何未显式 ed 且未显式忽略的 s。Futureawaitunawaited_futureFutureawaitunawaited
0赞 idkanything 4/28/2022
@jamesdlin 你能看看我的新问题吗?stackoverflow.com/questions/72044145/......

答: 暂无答案