List is emptied after notifyListeners
I've got a weird one, I've been scratching my head for a while
I have a property on one of my classes using provider I can access and modify just fine except for this particular scenario:
I add a product to the _products list
_products.add(product);
notifyListeners();
At this point product is a class with the following properties
name String;
id String;
ingredients List<String>;
let's say for example product is
final product = Product(name:'milk', id:1, ingredients:['milk']);
This element gets added to the List and I can see in debugging that now _products is indeed a list with 1 item inside and all properties are correct
Once it notifies listeners they get the new value of _products and I can see that they receive the new value just fine except that for some odd reason the ingredients list is empty!
[ Product(name:'milk', id:1, ingredients:[]) ] // _products for representation purposes
I just verified the value of _products before notifying and it had the ingredients property correctly then right after notifying, that property becomes an empty list The other 2 properties id and name are updated correctly
I have checked very thoroughly and I have no code to modify _products else where
Solution 1:
Since we do not know your implementation details for Product and ProductList, I created an example myself in this case.
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class Product
{
final String name;
final String id;
final List<String> ingredients;
const Product({
required this.name,
required this.id,
required this.ingredients
});
}
class ProductList extends ChangeNotifier
{
final _products = <Product>[];
void add(Product product)
{
_products.add(product);
notifyListeners();
}
Iterable<Product> get products => _products;
}
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => ProductList()),
],
child: MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
const MyHomePage({
Key? key,
required this.title,
}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
void _incrementCounter() {
context.read<ProductList>().add(const Product(name: 'Product 1', id: '1234', ingredients: ['Hello', 'There']));
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: const SafeArea(
child: Products(),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}
class Products extends StatelessWidget {
const Products({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
for(var product in context.watch<ProductList>().products)
ListTile(
leading: Text(product.id),
title: Text(product.name),
subtitle: Text(product.ingredients.join(', ')),
),
]
);
}
}
You can check it out on Dart Pad. Hope this helps. Please refer to How to create a Minimal, Reproducible Example.