api - 在颤动的页面之间传递API生成的对象
问题描述
我有一个 API 生成的项目列表,我想在购物车页面中查看项目,目前正在添加项目但不是作为项目对象,因为当我尝试访问购物车页面中的项目属性之一时,它给了我错误:NoSuchMethodError:在 null 上调用了 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,),
)
],
),
) ,
);
}
}
解决方案
代码片段
new GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => CartPage(item: _items),
),
);
}
...
class CartPage extends StatelessWidget {
final List<Item> item;
推荐阅读
- excel - 单元格中的 Excel 数据验证下拉列表
- mysql - 没有全文搜索的 SQL 叙词类行为
- c++ - 在 main() 函数中使用 drawText() 失败
- python-3.x - 从父数组构造二叉树
- node.js - 数据库中的动态下拉列表
- ios - 运行 FetchRequest 时出错(Core Data,SwiftUI)
- python - 在 django 中发送诸如“n 个用户喜欢你的帖子”之类的通知
- spring - SimpMessagingTemplate 没有符合条件的 bean 类型
- ruby-on-rails - 急切地通过 rails 加载 has_one
- vb.net - 通过 accdb 将新列连接到 Visual Studio