import 'package:flutter/material.dart';
import '../../Data/Globalvariable.dart' as global;
class MenuBar extends StatefulWidget {
final key = UniqueKey();
// final pin;
// MenuBar({this.pin='112'});
@override
_MenuBarState createState() {
return _MenuBarState();
}
}
class _MenuBarState extends State<MenuBar> {
ValueNotifier<List<String>> st = ValueNotifier(global.pincode);
Widget total() {
return ListView.builder(
itemCount: global.pincode.length,
itemBuilder: (context, index) {
final item = global.pincode[index];
return Dismissible(
key: UniqueKey(),
onDismissed: (direction) {
setState(() {
global.pincode.removeAt(index);
});
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text('$item Removed')));
},
background: Container(color: Colors.red),
child: ListTile(
title: Text(item),
),
);
},
);
}
@override
Widget build(BuildContext context) {
return ValueListenableBuilder(
builder: (context, n, menu) {
print(global.pincode);
return total();
},
child: total(),
valueListenable: st,
);
}
}
this is my main file i have a global file and all of my working variables are there
Globalvariable.dart
library project.globals;
List<String> pincode = ['Select Pin','110084','110088'];
now as i am adding data into pincode valuenotifier should listen to it and update the widget automatically but it is not doing so i verified that using debug console it gets updated when i call setstate which is ok but i want to update widget as i add data to my list
this is to demonstrate what i said above that wigdet is updating on removal but not for addition
>` will not know when the value is changed or not. See the source of `ValueNotifier`, it uses equality operator `==`. This won't work for lists, you are comparing the same List pointer with itself (even though List's content changed). See this answer on list comparison in Dart https://stackoverflow.com/a/22333042/10830091
– om-ha Nov 08 '21 at 23:58