为什么未收到功能列表数据

发布于 2025-01-24 16:46:15 字数 3662 浏览 1 评论 0原文

这是我的代码

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

问题

  1. 我从Fire Store中获取数据(这是成功的。 快照。

  2. 但是,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

  1. I get data from the fire store ( And it is successfull.
    snapshot.connectionState == ConnectionState.done complete)

  2. store it in the parameter stockcardlist using add

  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文