Skip to content

Instantly share code, notes, and snippets.

@D-sense
Created October 8, 2018 10:41
Show Gist options
  • Save D-sense/ba80184db3e4514b073501bf076a0576 to your computer and use it in GitHub Desktop.
Save D-sense/ba80184db3e4514b073501bf076a0576 to your computer and use it in GitHub Desktop.
setState() challenge in Flutter
Hello Devs,
I keep in receiving this error message "I/flutter (11828): Another exception was thrown: setState() called in constructor: CartState#040e6(lifecycle state: created, no widget, not mounted)
". I believe this is due to how I'm using setState() method in my CartState class below:
class CartScreen extends StatefulWidget{
createState(){
return CartState();
}
}
class CartState extends State<CartScreen>{
List cart = [];
int counter = 1 ;
void storeInCart(int id) {
setState((){
cart.add(id);
});
print("added successfully");
}
int getCart(){
return cart.length;
}
Widget build(context){
return Scaffold(
appBar: AppBar(
title: Center(
child: Text('Shopping Cart'),
),
),
body: Center(
child: Text("Number of added of items is ${storeInCart(2)}")
),
);
}
}
import 'package:flutter/material.dart';
import 'login_screen.dart';
import 'checkout/checkout_screen.dart';
import 'login_screen.dart';
import '../models/store_model.dart';
import 'bottom_nav_screens/cart_screen.dart';
class ItemDetailScreen extends StatefulWidget {
final String itemName;
final double itemPrice;
final String itemImage;
final double itemRating;
final int itemId;
ItemDetailScreen(
{this.itemId, this.itemName, this.itemPrice, this.itemImage, this.itemRating});
createState() {
return ItemDetailState();
}
}
class ItemDetailState extends State<ItemDetailScreen> {
.
.
.
.
Widget itemDescriptionWidget() {
return Stack(
children: <Widget>[
.
.
.
.
Container(
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
ButtonTheme(
minWidth: 195.0,
height: 50.0,
shape: RoundedRectangleBorder(borderRadius:BorderRadius.zero),
child: RaisedButton(
child: Text('Add to cart', style: TextStyle(color: Colors.white)),
color: Colors.black,
elevation: 4.0,
splashColor: Colors.blueGrey,
onPressed: () {
CartScreen();
CartState cart = CartState();
cart.storeInCart(widget.itemId);
_navCart(context);
}),
),
ButtonTheme(
minWidth: 195.0,
height: 50.0,
shape: RoundedRectangleBorder(borderRadius:BorderRadius.zero),
child: RaisedButton(
child: Text('Buy Now', style: TextStyle(color: Colors.white)),
color: Colors.green,
elevation: 4.0,
splashColor: Colors.blueGrey,
onPressed: () {
if(login_screen.getLoginStatue() == true){
return _navCheckoutScreen(context);
}else{
return _navLogin(context);
}
}),
)
],
),
Container(margin: EdgeInsets.only(bottom: 20.0)),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Icon(Icons.favorite),
Icon(Icons.share)
],
),
Container(margin: EdgeInsets.only(bottom: 5.0)),
Divider(),
Container(margin: EdgeInsets.only(bottom: 10.0)),
],
)),
description(),
reviewsWidget(),
],
),
),
),
],
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment