如何在 StreamBuilder 中构建使用 2 个子函数进行过滤的 if 条件函数

问题描述 投票:0回答:1

开始吧! (使用Firebase从中获取数据)

我想在 Streambuilder 中添加 if 条件

  1. 如果“dropdowncompany”==“ซินเนติก”->使用OverviewPurchase()函数
  2. 如果“dropdowncompany”==“ซินโนวิค”->使用OverviewPurchase1()函数

这是我的 Firebase 中的图片,显示了“dropdowncompany”字段中的值在哪里 ------------------------图片专区------------------------------------------------ enter image description here ------------图片专区------------------------------------------------

这是OverviewPurcahse()和OverviewPurchase1()的两个函数

----------------概览购买码区----------------------

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';

class OverviewPurchase extends StatefulWidget {
  const OverviewPurchase({super.key});

  @override
  State<OverviewPurchase> createState() => _OverviewPurchaseState();
}

class _OverviewPurchaseState extends State<OverviewPurchase> {
  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;

    return Scaffold(
      body: StreamBuilder(
          stream:
              FirebaseFirestore.instance.collection("pr_details").snapshots(),
          builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
            if (!snapshot.hasData) {
              return Center(
                child: CircularProgressIndicator(),
              );
            }
            return Padding(
              padding: EdgeInsets.all(60.0),
              child: Container(
                width: size.width * 0.25,
                child: ListView(
                    children: snapshot.data!.docs.map((document) {
                  return Column(
                    children: [
                      SizedBox(
                        height: 30,
                      ),
                      Card(
                          borderOnForeground: true,
                          color: Colors.lightBlueAccent[100],
                          child: ListTile(
                              leading: IconButton(
                                onPressed: () {},
                                alignment: Alignment.topLeft,
                                icon: Icon(Icons.list_alt_outlined),
                                iconSize: 30,
                              ),
                              title: Column(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceAround,
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Chip(
                                    label: Text("ใบขอซื้อ Synatic"),
                                    backgroundColor: Colors.deepPurpleAccent,
                                    labelStyle: TextStyle(color: Colors.white),
                                  ),
                                  SizedBox(
                                    height: 20,
                                  ),
                                ],
                              ),
                              subtitle: Column(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceAround,
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(document["purchase_req"]),
                                  Text("บริษัท  :  " +
                                      document["dropdowncompany"]),
                                  Text("แผนก  :  " + document["dropdowndep"]),
                                  Text("ประเภทงาน  :  " +
                                      document["dropdownwork"]),
                                  Text(
                                      "ชื่อ-นามสกุล  :  " + document["fsname"]),
                                  //Text("แผนก  :  " + document["department"]),
                                  Text(
                                      "วันที่เขียน  :  " + document["date_pr"]),
                                  Text(
                                      "จำนวนรายการ  :  ${int.parse((document['number1']).length.toString()) + int.parse((document['number2']).length.toString()) + int.parse((document['number3']).length.toString()) + int.parse((document['number4']).length.toString()) + int.parse((document['number5']).length.toString()) + int.parse((document['number6']).length.toString()) + int.parse((document['number7']).length.toString()) + int.parse((document['number8']).length.toString()) + int.parse((document['number9']).length.toString()) + (int.parse((document['number10']).length.toString()) - 1)}")
                                ],
                              ))),
                    ],
                  );
                }).toList()),
              ),
            );
          }),
    );
  }
}

----------------概览购买码区----------------------

----------------概览购买1码区------------------------

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';

class OverviewPurchase1 extends StatefulWidget {
  const OverviewPurchase1({super.key});

  @override
  State<OverviewPurchase1> createState() => _OverviewPurchase1State();
}

class _OverviewPurchase1State extends State<OverviewPurchase1> {
  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return Scaffold(
      body: StreamBuilder(
          stream:
              FirebaseFirestore.instance.collection("pr_details").snapshots(),
          builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
            if (!snapshot.hasData) {
              return Center(
                child: CircularProgressIndicator(),
              );
            }
            return Padding(
              padding: EdgeInsets.all(60.0),
              child: Container(
                width: size.width * 0.25,
                child: ListView(
                    children: snapshot.data!.docs.map((document) {
                  return Column(
                    children: [
                      SizedBox(
                        height: 30,
                      ),
                      Card(
                          borderOnForeground: true,
                          color: Colors.lightGreenAccent[100],
                          child: ListTile(
                              leading: IconButton(
                                onPressed: () {},
                                alignment: Alignment.topLeft,
                                icon: Icon(Icons.list_alt_outlined),
                                iconSize: 30,
                              ),
                              title: Column(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceAround,
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Chip(
                                    label: Text("ใบขอซื้อ Synovic"),
                                    backgroundColor: Colors.deepPurpleAccent,
                                    labelStyle: TextStyle(color: Colors.white),
                                  ),
                                  SizedBox(
                                    height: 20,
                                  ),
                                ],
                              ),
                              subtitle: Column(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceAround,
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(document["purchase_req"]),
                                  Text("บริษัท  :  " +
                                      document["dropdowncompany"]),
                                  Text("แผนก  :  " + document["dropdowndep"]),
                                  Text("ประเภทงาน  :  " +
                                      document["dropdownwork"]),
                                  Text(
                                      "ชื่อ-นามสกุล  :  " + document["fsname"]),
                                  Text(
                                      "วันที่เขียน  :  " + document["date_pr"]),
                                  Text(
                                      "จำนวนรายการ  :  ${int.parse((document['number1']).length.toString()) + int.parse((document['number2']).length.toString()) + int.parse((document['number3']).length.toString()) + int.parse((document['number4']).length.toString()) + int.parse((document['number5']).length.toString()) + int.parse((document['number6']).length.toString()) + int.parse((document['number7']).length.toString()) + int.parse((document['number8']).length.toString()) + int.parse((document['number9']).length.toString()) + (int.parse((document['number10']).length.toString()) - 1)}")
                                ],
                              ))),
                    ],
                  );
                }).toList()),
              ),
            );
          }),
    );
  }
}

----------------概览购买1码区------------------------

接下来这就是我写的

------------------我的if条件代码----------------------------------------

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:inventory_app/screens/purchases/overview-purchases-syna.dart';
import 'package:inventory_app/screens/purchases/overview-purchases-syno.dart';

class Divided1 extends StatefulWidget {
  const Divided1({super.key});

  @override
  State<Divided1> createState() => _Divided1State();
}

class _Divided1State extends State<Divided1> {
  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
        stream: FirebaseFirestore.instance.collection("pr_details").snapshots(),
        builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
          if (snapshot.hasData) {
            final selectD1 = snapshot.data?.docs.reversed.toList();
            snapshot.data!.docs.map((docum) {
              if (selectD1 != null && docum["dropdowncompany"] == "ซินเนติก") {
                OverviewPurchase();
              } else if (selectD1 != null &&
                  docum["dropdowncompany"] == "ซินโนวิค") {
                OverviewPurchase1();
              }
            }).toList();
          }
          
        });
  }
}

------------------我的if条件代码----------------------------------------

最后,这是显示过滤结果的代码

--------------输出代码--------------------

import 'package:flutter/material.dart';
import 'package:inventory_app/pallete.dart';
import 'package:inventory_app/screens/purchases/overview-divided.dart';
import 'package:inventory_app/screens/purchases/po-purchases.dart';
import 'package:inventory_app/screens/purchases/pr-purchases.dart';
import 'package:inventory_app/screens/purchases/quotation-purchases.dart';
import 'package:inventory_app/screens/welcome-screens.dart';

class PipelinePurchase extends StatefulWidget {
  const PipelinePurchase({super.key});

  @override
  State<PipelinePurchase> createState() => _PipelinePurchaseState();
}

class _PipelinePurchaseState extends State<PipelinePurchase> {
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 4,
      child: Scaffold(
        appBar: AppBar(
          title: Text(
            "Purchasing",
            style: kBodyText,
          ),
          centerTitle: true,
          backgroundColor: Colors.green,
          leading: IconButton(
            onPressed: () {
              Navigator.pushReplacement(context,
                  MaterialPageRoute(builder: (context) {
                return CustomCarouselFB2();
              }));
            },
            icon: Icon(
              Icons.arrow_back_ios,
              color: kWhite,
            ),
          ),
          bottom: TabBar(tabs: [
            Tab(
              child: Text(
                "Pipelines",
                style: kBodyText,
              ),
            ),
            Tab(
              child: Text(
                "ใบขอซื้อ",
                style: kBodyText,
              ),
            ),
            Tab(
              child: Text(
                "เปรียบเทียบใบเสนอราคา",
                style: kBodyText,
              ),
            ),
            Tab(
              child: Text(
                "ใบสั่งซื้อ",
                style: kBodyText,
              ),
            ),
          ]),
        ),
        body: TabBarView(children: [
          Divided1(),
          PrPurchase(),
          QuotationPurchase(),
          PoPurchase(),
        ]),
      ),
    );
  }
}

--------------输出代码--------------------

我得到“主体可能正常完成,导致返回‘null’,但返回类型‘Widget’可能是不可为 null 的类型。 尝试在末尾添加 return 或 throw 语句。” 在我的 if 条件构建中:( 像这样 enter image description here

总而言之,您可以在输出代码中看到 Divided1() ,所以我想在 Stream 构建器中将 Divided1() 作为 If 条件完成,过滤 OverviewPurchase() 和 OverviewPurchase1() 的 2

flutter firebase dart if-statement conditional-statements
1个回答
0
投票

build 的返回类型为“Widget”而不是“Widget”?这意味着你总是需要返回一些东西,但现在如果语句“snapshot.hasData”是错误的,你不会返回任何东西,尝试确定如果快照中仍然没有数据你想要返回什么,也许是 CircularProgressIndicator() 对于加载器,或空盒子的 SizedBox.shrink()

看起来像

if(snapshot.hasData) {
  // return your Widget, which depends on your streams data
} else {
  return CircularProgressIndicator();
}
© www.soinside.com 2019 - 2024. All rights reserved.