产品已添加到工作表,但不保存产品。热重启或切换页面时,工作表为空。
class ProductUi extends StatelessWidget {
ProductTest productTest;
ProductUi(this.productTest);
@override
Widget build(BuildContext context) {
final productBloc = BlocProvider.of<ProductBloc>(context);
return Scaffold(
body: Container(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(productTest.name),
RaisedButton(
onPressed: () {
productBloc.add(AddToCart(productTest: productTest));
},
child: Text('add to cart'),
),
],
),
)),
);
}
}
功能
class CartService {
List<ProductTest> cartProduct = [];
Future addProduct(ProductTest productTest) async {
return cartProduct.add(productTest);
}
}
堵塞
class ProductBloc extends Bloc<ProductEvent, ProductState> {
CartService cartService = CartService();
ProductBloc({@required this.cartService});
@override
ProductState get initialState => ProductInitial();
@override
Stream<ProductState> mapEventToState(ProductEvent event) async* {
if (event is AddToCart) {
await cartService.addProduct(event.productTest);
}
}
}
事件
class AddToCart extends ProductEvent {
ProductTest productTest;
AddToCart({this.productTest});
@override
List<Object> get props => [];
}
主屏幕
class HomeScreenTest extends StatefulWidget {
@override
_HomeScreenTestState createState() => _HomeScreenTestState();
}
class _HomeScreenTestState extends State<HomeScreenTest> {
CartService cartService = CartService();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Padding(
padding: const EdgeInsets.only(top: 4),
child: SizedBox(
height: kToolbarHeight,
child: Image.asset('assets/dafna_logo.png'),
),
),
backgroundColor: Colors.blueGrey,
elevation: 20,
),
body: CategoryTest(),
);
}
}
class CategoryTest extends StatefulWidget {
@override
_CategoryTestState createState() => _CategoryTestState();
}
class _CategoryTestState extends State<CategoryTest> {
CartService cartService = CartService();
@override
Widget build(BuildContext context) {
BlocProvider.of<ProductBloc>(context)..add(LoadCategoriesEvent());
return Scaffold(
backgroundColor: Colors.blueGrey[400],
body: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.fromLTRB(20, 15, 20, 15),
child: Column(
children: <Widget>[
Text('Категории'),
BlocBuilder<ProductBloc, ProductState>(
builder: (context, state) {
if (state is ProductLoaded) {
return categoryColumn(
state.categoriesTest, state.productTest);
} else {
return Container();
}
},
)
],
),
),
),
);
}
Widget categoryColumn(
List<CategoriesTest> categoriesTest, List<ProductTest> productTest) {
return Column(
children: categoriesTest.map((category) {
return Card(
color: Colors.blueGrey[300],
elevation: 10.0,
margin: EdgeInsets.fromLTRB(10, 10, 10, 10),
child: InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => BlocProvider(
create: (context) =>
ProductBloc(cartService: cartService),
child: ProductList(
categoriesTest: category,
))));
},
child: Container(
padding: EdgeInsets.all(20),
child: Column(
children: <Widget>[
Text(
category.categoryname,
),
],
),
),
),
);
}).toList(),
);
}
class ProductList extends StatefulWidget {
CategoriesTest categoriesTest;
ProductList({@required this.categoriesTest});
@override
_ProductListState createState() => _ProductListState();
}
class _ProductListState extends State<ProductList> {
CartService cartService = CartService();
@override
Widget build(BuildContext context) {
BlocProvider.of<ProductBloc>(context)..add(LoadProductListEvent());
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.blueGrey,
),
backgroundColor: Colors.blueGrey[100],
body: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.fromLTRB(20, 30, 20, 15),
child: Column(
children: <Widget>[
BlocBuilder<ProductBloc, ProductState>(
builder: (context, state) {
if (state is TableListLoaded) {
return tableListColumn(
state.productTest, widget.categoriesTest);
} else {
return Container();
}
},
)
],
),
),
),
);
}
Widget tableListColumn(
List<ProductTest> productTest, CategoriesTest categoriesTest) {
return Column(
children: productTest
.where((product) => product.category == categoriesTest.categoryname)
.map((product) {
return Card(
elevation: 10.0,
child: InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => BlocProvider(
create: (context) =>
ProductBloc(cartService: cartService),
child: ProductUi(product))));
},
child: Container(
padding: EdgeInsets.all(15),
child: Column(
children: <Widget>[
Image.network(product.image),
Text(product.name),
],
),
),
),
);
}).toList(),
);
}
}
您有一个类
CartService
,您在该类中创建该类的一个对象 (CartService cartService = CartService();
)ProductUi
。这个对象只存在于这个类中。当这个类被销毁时(例如,热重启时或页面切换回来时),其中的对象也会被销毁。为避免这种情况,您需要
CartService
在小部件树的上方创建一个类对象并将其传递给构造函数:或者创建一个类
CartService
Singleton
(为类的实例提供全局访问点,一个不可破坏的对象)并在任何地方使用它(我提请您注意,您不应该Singleton
那样创建它,因为它占用内存空间,它包含的物体越多,他将占据的地方就越多):为了在应用程序重新启动时保存数据,需要使用数据库,从中保存和加载数据。