At first I encountered the issue that the Scaffold.of(context)
referred to the wrong context. I implemented the Builder
widget properly. The error message was gone but the snackbar still doesn't show up. My first thought was that I've done something wrong and tried the other way around and set a key to the Scaffold
.
All this referring to the examples and explanations of this thread which definitely make sense: How to properly display a Snackbar in Flutter?
But nothing of this worked. I was wondering why so I tried a little bit around and found out that the Snackbar works when i remove all the other function calls in my onPressed
function of the button
import 'package:flutter/material.dart';
import 'package:flutter_complete_guide/providers/cart_provider.dart'
show CartProvider;
import 'package:flutter_complete_guide/providers/order_provider.dart';
//import 'package:flutter_complete_guide/screens/order_screen.dart';
import 'package:flutter_complete_guide/widgets/cart_item.dart';
import 'package:provider/provider.dart';
class CartScreen extends StatelessWidget {
static const routeName = "/cart";
@override
Widget build(BuildContext context) {
final cart = Provider.of<CartProvider>(context);
final _scaffoldKey = GlobalKey<ScaffoldState>();
return Scaffold(
key: _scaffoldKey,
appBar: AppBar(
title: Text("Your Cart"),
),
body: Builder(
builder: (BuildContext context) => Column(
children: <Widget>[
Card(
margin: EdgeInsets.all(15.0),
child: Padding(
padding: EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
"Total",
style: TextStyle(fontSize: 20.0),
),
SizedBox(width: 10),
Spacer(),
Chip(
label: Text("\$${cart.totalAmount}"),
backgroundColor: Theme.of(context).primaryColor,
),
FlatButton(
onPressed: () {
Scaffold.of(context).showSnackBar(
SnackBar(
content: Text("Test Snackbar"),
),
);
/*
** If this code gets called, the snackbar does not work anymore **
Provider.of<OrderProvider>(context, listen: false)
.addOrder(
cart.items.values.toList(), cart.totalAmount);
cart.clear();
*/
},
child: Text("Order now"),
),
],
),
),
),
SizedBox(height: 10),
Expanded(
child: ListView.builder(
itemCount: cart.items.length,
itemBuilder: (ctx, index) => CartItem(
id: cart.items.values.toList()[index].id,
productId: cart.items.keys.toList()[index],
title: cart.items.values.toList()[index].title,
price: cart.items.values.toList()[index].price,
quantity: cart.items.values.toList()[index].quantity,
),
),
),
],
),
),
);
}
}
The strange part about that is, I've added another snackbar to another Button and in that onPressed
is another function call aswell but this works perfectly fine:
IconButton(
icon: Icon(Icons.shopping_cart),
onPressed: () {
cart.addItem(products.id, products.price, products.title);
Scaffold.of(context).hideCurrentSnackBar();
Scaffold.of(context).showSnackBar(
SnackBar(
content: Text("${products.title} added"),
duration: Duration(seconds: 1),
action: SnackBarAction(
label: "UNDO",
onPressed: () => cart.removeItemSignle(products.id),
),
),
);
},
I clearly have no clue whats going wrong here, does anyone have an idea?