【问题标题】:Pass document content from one screen to another in flutter firestore在 Flutter Firestore 中将文档内容从一个屏幕传递到另一个屏幕
【发布时间】:2020-11-14 04:11:45
【问题描述】:

在下面的代码中,当用户单击主屏幕上列出的任何产品时,我试图将数据从主屏幕传递到详细信息屏幕。我似乎在这里遇到了困难。

Similar Question but does not solve my issue

主屏幕上的代码摘录。

                  StreamBuilder(
                  stream: FirebaseFirestore.instance
                      .collection("name")
                      .limit(12)
                      .orderBy("Published Date", descending: true)
                      .snapshots(),
                  builder: (context, snapshot){
                    if (!snapshot.hasData) {
                      return Center(
                        child: spinkit,
                      );
                    }
                    return GridView.builder(
                        physics: ScrollPhysics(),
                        scrollDirection: Axis.vertical,
                        shrinkWrap: true,
                        itemCount: snapshot.data.docs.length,
                        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                          crossAxisCount: 3,
                          crossAxisSpacing: 10.0,
                          mainAxisSpacing: 10,
                          
                        ),
                        itemBuilder: (BuildContext context, int index){
                          if(snapshot.connectionState == ConnectionState.waiting){
                            return Center(
                              child: spinkit,
                            );
                          }
                          print("${snapshot.data.documents[index].get('Product Title')}");
                          return GestureDetector(
                            onTap: (){
                              // ===> SEND USER TO THE DETAILS SCREEN <===
                              Navigator.push(
                                context,
                                MaterialPageRoute(builder: (context) => ProductDetailsScreen()),
                              );
                            },
                            child: ClipRRect(
                              borderRadius: BorderRadius.circular(5),
                              child: Stack(
                                children: [
                                  Container(
                                    height: 150,
                                    width: 150,
                                    child: Image.network(
                                      snapshot.data.documents[index].get('image') ?? spinkit,
                                      fit: BoxFit.cover,
                                      width: double.infinity,
                                      height: double.infinity,
                                    ),
                                  ),
                                  Positioned(
                                    left: 0,
                                    bottom: 0,
                                    child: Container(
                                      height: 20,
                                      width: 150,
                                      decoration: BoxDecoration(
                                          gradient: LinearGradient(
                                            colors: [
                                              Colors.black38,
                                              Colors.black38,
                                            ],
                                            begin: Alignment.bottomCenter,
                                            end: Alignment.topCenter,
                                          )),
                                    ),
                                  ),
                                  Positioned(
                                    left: 4,
                                    bottom: 5,
                                    child: Text(
                                      snapshot.data.documents[index].get('name') ?? "Product Name",
                                      overflow: TextOverflow.ellipsis,
                                      style: TextStyle(
                                          color: Palette.whiteColor,
                                          fontSize: 11,
                                          fontWeight: FontWeight.bold),
                                    ),
                                  )
                                ],
                              ),
                            ),
                          );
                        }
                    );
                  },
                )

详细信息屏幕上的代码摘录。您在详细信息屏幕上看到的是我手动填充的数据。它不是来自 Firestore。

class ProductDetailsScreen extends StatefulWidget {

  @override
  _ProductDetailsScreenState createState() => _ProductDetailsScreenState();
}

class _ProductDetailsScreenState extends State<ProductDetailsScreen> {

  final productDb = FirebaseFirestore.instance.collection("name");

  User user = FirebaseAuth.instance.currentUser;

  final spinkit = SpinKitHourGlass(
    color: Colors.white,
    size: 50.0,
  );

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: _onBackPressed,
      child: Scaffold(
        body: SafeArea(
            child: Padding(
              padding: EdgeInsets.only(left: 15, right: 15, top: 3, bottom: 10),
              child: FutureBuilder(
                future: productDb.get(),
                builder: (context, snapshot){
                  if(snapshot.connectionState == ConnectionState.waiting){
                    return Center(
                      child: spinkit,
                    );
                  }
                  return Container(
                    child: ListView(
                      children: [
                        Column(
                          children: [
                            Center(
                              child: Container(
                                width: 350,
                                child: Card(
                                  elevation: 5,
                                  child: Container(
                                    padding: EdgeInsets.all(8),
                                    child: Container(
                                      height: 220,
                                      decoration: BoxDecoration(
                                          image: DecorationImage(
                                              fit: BoxFit.fill,
                                              image: AssetImage("asset/images/headphone.jpg",)
                                          )
                                      ),
                                    ),
                                  ),
                                ),
                              ),
                            ),
                            SizedBox(height: 10,),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text("Item Name",
                                  style: TextStyle(
                                      color: Palette.blackColor,
                                      fontSize: 18,
                                      fontWeight: FontWeight.w300
                                  ),),
                                Text("Item Price",
                                  style: TextStyle(
                                      color: Palette.blackColor,
                                      fontSize: 18,
                                      fontWeight: FontWeight.w300
                                  ),),
                              ],
                            ),
                            SizedBox(height: 10,),
                            Text("Item Description",
                              style: TextStyle(
                                  color: Palette.blackColor,
                                  fontSize: 18,
                                  fontWeight: FontWeight.w300
                              ),),
                            Container(
                              height: 1,
                              width: 100,
                              color: Colors.black12,
                            ),
                            SizedBox(height: 10,),
                            Container(
                              height: 140,
                              width: 350,
                              child: SingleChildScrollView(
                                child: Wrap(
                                    children: [
                                      Text(
                                        "What is Lorem Ipsum Lorem Ipsum is simply dummy "
                                            "text of the printing and typesetting industry"
                                            " Lorem Ipsum has been the industry's standard"
                                            " dummy text ever since the 1500s when an "
                                            "unknown printer took a galley of type and "
                                            "scrambled it to make a type specimen book "
                                            "it has?",
                                        textAlign: TextAlign.justify,
                                        style: TextStyle(
                                          color: Palette.blackColor,
                                          fontSize: 16,
                                        ),),
                                    ]
                                ),
                              ),
                            ),
                          ],
                        ),
                      ],
                    ),
                  );
                },
              ),
            )),
      ),
    );
  }
}

【问题讨论】:

    标签: flutter dart google-cloud-firestore


    【解决方案1】:

    在你的GestureDetectoronTap

    // ===> SEND USER TO THE DETAILS SCREEN WITH DOC<===
    Navigator.push(
      context,
      MaterialPageRoute(builder: (context) => ProductDetailsScreen(snapshot.data.documents[index])),
    );
    

    然后

    class ProductDetailsScreen extends StatefulWidget {
      ProductDetailsScreen(this.doc);
    
      QueryDocumentSnapshot doc;
    
      @override
      _ProductDetailsScreenState createState() => _ProductDetailsScreenState();
    }
    

    widget.doc 访问_ProductDetailsScreenState

    文档数据将以Map&lt;String, dynamic&gt; 的形式在widget.doc.data 中找到。

    【讨论】:

    • 感谢您的回复。在 ProductDetailsS​​creen 中实施您的建议后,我收到 Document is not defined 错误。 @李3
    • 我很抱歉。我不记得我脑海中的班级名称。答案已更新。
    猜你喜欢
    • 1970-01-01
    • 2019-10-27
    • 2021-01-06
    • 2022-12-07
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-03
    相关资源
    最近更新 更多