在颤动的页面之间传递API生成的对象

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

我有一个API生成的项目列表,我想将项目查看到购物车页面,当前正在添加项目,但不作为Item对象添加,因为当我尝试访问购物车页面中的项目属性之一时,给我错误:NoSuchMethodError:在空调用getter'image_URL'。接收方为空,尝试调用:image_url。我知道应该以某种方式传递所选项目,但是我应该怎么做?

这是正在生成项目列表并查看的主页中的代码

import 'package:flutter/material.dart';
import 'cart_page.dart';
import 'cart_bloc.dart';
import 'package:provider/provider.dart';
import 'repository/item_repo.dart';
import 'models/item.dart';
import 'components/item_card.dart';

//My own Imports
import 'package:alsyed_bakeries_ecommerce/components/horizontal_list_view.dart';
import 'package:alsyed_bakeries_ecommerce/components/drawerr.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<CartBloc>(
        create: (context) => CartBloc(),
        child: MaterialApp(
          title: 'Alsayed E-commerce',
          debugShowCheckedModeBanner: false,
          theme: ThemeData(
            primarySwatch: Colors.red,
          ),
          home: MyHomePage(title: 'Alsayed Bakeries'),
        ));
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {

  List<Item> _items = <Item>[];
  //final Item item;


  @override
  void initState() {
    super.initState();
    listenForItems();
  }


  @override
  Widget build(BuildContext context) {
    var bloc = Provider.of<CartBloc>(context);
    int totalCount = 0;
    if (bloc.cart.length > 0) {
      totalCount = bloc.cart.values.reduce((a, b) => a + b);
    }
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text(widget.title),
        actions: <Widget>[
          new IconButton(
              icon: new Icon(
                Icons.search,
                color: Colors.white,
              ),
              onPressed: null),
          new Padding(
            padding: const EdgeInsets.all(10.0),
            child: new Container(
                height: 150.0,
                width: 30.0,
                child: new GestureDetector(
                  onTap: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                      builder: (context) => CartPage(),
                      ),
                    );
                  },
                  child: new Stack(
                    children: <Widget>[
                      new IconButton(
                        icon: new Icon(
                          Icons.shopping_cart,
                          color: Colors.white,
                        ),
                        onPressed: null,
                      ),
                      new Positioned(
                          child: new Stack(
                            children: <Widget>[
                              new Icon(Icons.brightness_1,
                                  size: 20.0, color: Colors.red[700]),
                              new Positioned(
                                  top: 3.0,
                                  right: 7,
                                  child: new Center(
                                    child: new Text(
                                      '$totalCount',
                                      style: new TextStyle(
                                          color: Colors.white,
                                          fontSize: 12.0,
                                          fontWeight: FontWeight.w500),
                                    ),
                                  )),
                            ],
                          )),
                    ],
                  ),
                )),
          )
        ],

      ),
      drawer: Drawerr(),
      body:Column(
        children: <Widget>[
          SizedBox(
            height: 150,
            child: ListView(
              children: <Widget>[
                new Padding(padding: const EdgeInsets.all(10.0),
                  child: new Text('Categories',
                      style: TextStyle(
                          //fontFamily: 'Montserrat',
                        // color: Colors.grey,
                          fontWeight: FontWeight.bold,
                          fontSize: 20.0)), ),
                //Horizontal list
                HorizontalList(),
              ],

            ),
          ),

            Container(
              height: 600,
              child: ListView.builder(

                  itemCount: _items.length,
                  itemBuilder: (context, index) => ItemTile(_items[index]),
              ),
            ),


        ],
      ),
    );

  }

  void listenForItems() async {
    final Stream<Item> stream = await getItems();
    stream.listen((Item item) =>
        setState(() =>  _items.add(item))
    );
  }

}

这是购物车页面的代码

import 'package:flutter/material.dart';
import 'cart_bloc.dart';
import 'package:provider/provider.dart';
import 'models/item.dart';
import 'main.dart';

class CartPage extends StatelessWidget {
  final Item item;

  CartPage({Key key, this.item}) : super(key: key);
  //CartPage(this._item);

  @override
  Widget build(BuildContext context) {
    var bloc = Provider.of<CartBloc>(context);
    var cart = bloc.cart;
    return Scaffold(
      appBar: AppBar(
        title: Text("Shopping Cart"),
      ),
      body: ListView.builder(
        padding: const EdgeInsets.all(23.0),
        itemCount: cart.length,
        itemBuilder: (context, index) {
          int giftIndex = cart.keys.toList()[index];
          int count = cart[giftIndex];
          return Card(
            child: ListTile(
              leading: Container(
                height: 70,
                width: 70,
               child: Image.network(item.image_url, height: 50.0, fit: BoxFit.fill,)
              ),
              title: Text('Quantity: $count',),
             // subtitle: Text(item.name),
              trailing: RaisedButton(
                child: Text('Remove'),
                color: Theme.of(context).buttonColor,
                elevation: 1.0,
                splashColor: Colors.blueGrey,
                onPressed: () {
                  bloc.clear(giftIndex);
                },
              ),
            ),
          );
        },
      ),
      bottomNavigationBar: new Container(
        color: Colors.white,
        child: Row(
          children: <Widget>[
            Expanded(child: ListTile(
              title: new Text("Total"),
              subtitle: new Text("232"),
            ),),
            Expanded(
              child: new MaterialButton(onPressed: (){},
              child: new Text("Check out" , style: TextStyle(color: Colors.white)),
                color: Colors.red,),
            )
          ],
        ),
      ) ,
    );
  }
}
api flutter model shopping-cart
1个回答
0
投票

您可以参考此https://flutter.dev/docs/cookbook/navigation/passing-data#4-navigate-and-pass-data-to-the-detail-screen

代码段

new GestureDetector(
                  onTap: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                      builder: (context) => CartPage(item: _items),
                      ),
                    );
                  }

...
class CartPage extends StatelessWidget {
  final List<Item> item;
© www.soinside.com 2019 - 2024. All rights reserved.