如何从流构建器获取所有数据作为“地图”或“列表”?

时间:2019-07-15 10:47:00

标签: android json api flutter stream

我有这个流生成器,可以从用户那里选择获取Item并将其显示为订单, 在流构建器中,我可以通过 index 来获得它们,例如snapshot.data.orders[**index**] 当我从流中按一个按钮以将它们保存为 json 字符串并将其作为来自客户的订单发送时,如何获取所有这些数据?

这是代码

import 'package:cosmetics_store/models/Cart.dart';
import 'package:cosmetics_store/bloc/CartBloc.dart';
import 'package:cosmetics_store/components/OrderWidget.dart';

class CartManager extends StatefulWidget {
@override
_CartManager createState() => new _CartManager();
}

class _CartManager extends State<CartManager> {
CartBloc _cartBloc = new CartBloc();

@override
Widget build(BuildContext context) {
 double _gridSize = MediaQuery.of(context).size.height * 0.88;

 return new Container(
     height: MediaQuery.of(context).size.height,
     child: new Stack(children: <Widget>[
       new Column(
           crossAxisAlignment: CrossAxisAlignment.start,
           children: <Widget>[
             new StreamBuilder(
                 initialData: _cartBloc.currentCart,
                 stream: _cartBloc.observableCart,

                 builder: (context, AsyncSnapshot<Cart> snapshot) {



                   return new Container(
                       margin: EdgeInsets.symmetric(horizontal: 20),
                       height: _gridSize,
                       width: double.infinity,
                       child: new Column(
                           crossAxisAlignment: CrossAxisAlignment.start,
                           children: <Widget>[
                             new Padding(
                                 padding: EdgeInsets.symmetric(vertical: 40),
                                 child: new Text("Cart",
                                     style: TextStyle(
                                         color: Colors.white,
                                         fontSize: 40,
                                         fontWeight: FontWeight.bold))),
                             new Container(
                                 margin: EdgeInsets.only(bottom: 10),
                                 height: _gridSize * 0.60,
                                 child: new ListView.builder(
                                     itemCount: snapshot.data.orders.length,
                                     itemBuilder: (context, index) {
                                       return Dismissible(
                                         background: Container(
                                             color: Colors.transparent),
                                         key: Key(snapshot
                                             .data.orders[index].id
                                             .toString()),
                                         onDismissed: (_) {
                                           _cartBloc.removerOrderOfCart(
                                               snapshot.data.orders[index]);
                                         },
                                         child: new Padding(
                                             padding: EdgeInsets.symmetric(
                                                 vertical: 10),
                                             child: new OrderWidget(
                                                 snapshot.data.orders[index],
                                                 _gridSize)),
                                       );
                                     })),
                             new Container(
                                 height: _gridSize * 0.15,
                                 child: new Row(
                                     mainAxisAlignment:
                                         MainAxisAlignment.spaceBetween,
                                     children: <Widget>[
                                       new Text("Total",
                                           style: TextStyle(
                                               color: Colors.white,
                                               fontSize: 20)),
                                       new Text(
                                           "\$${snapshot.data.totalPrice().toStringAsFixed(2)}",
                                           style: TextStyle(
                                               color: Colors.white,
                                               fontWeight: FontWeight.bold,
                                               fontSize: 40)),
                                     ]))
                           ]));
                 })
           ]),
       new Align(
           alignment: Alignment.bottomLeft,
           child: new Container(
               padding: EdgeInsets.only(left: 10, bottom: _gridSize * 0.02),
               width: MediaQuery.of(context).size.width - 80,
               child: new RaisedButton(
                   color: Colors.amber,
                   shape: RoundedRectangleBorder(
                       borderRadius: BorderRadius.circular(60)),
                   padding: EdgeInsets.all(20),
                   onPressed: () {
                     if (_cartBloc.currentCart.isEmpty)
                       Scaffold.of(context).showSnackBar(
                           SnackBar(content: Text("Cart is empty")));
                     else {

                     }
                   },
                   child: new Text("Next",
                       style: TextStyle(fontWeight: FontWeight.bold)))))
     ]));
}
}

0 个答案:

没有答案