Виджет, в котором есть кнопка (и некоторые другие текстовые поля с названием элемента и т. д.):
Код: Выделить всё
@override
Widget build(BuildContext context) {
final currentItemList = ref.watch(jsonInventoryServiceNotifierProvider);
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text("Manually Add An Item"),
),
body: Form(
key: _formKey,
child: Column(
children: [
//TEXT FIELD HERE TO INPUT ITEM NAME
ElevatedButton(
onPressed: () async {
if (_formKey.currentState!.validate()) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Item Added!')),
);
Item newItem = Item(
productName: productNameTextFieldController.text,
isActive: true,
);
await ref
.read(jsonInventoryServiceNotifierProvider.notifier)
.insertItem(newItem);
currentItemList.when(
data: (item) {
if(item.any((element) =>
element.productName.toLowerCase() ==
(newItem.productName.toLowerCase()))) {
print("OPEN DIALOG BOX TO SAY THIS PRODUCT WAS BOUGHT BEFORE");
}
},
error: (Object error, StackTrace stackTrace) {},
loading: () {});
setState(() {});
}
},
child: const Text('Submit'),
),
],
Код: Выделить всё
Future insertItem(Item item) async {
final currentState = state;
late var currentList;
if (currentState is AsyncData) {
currentList = currentState.value;
state = AsyncValue.data([...currentList, item]);
} else if (currentState is AsyncLoading) {
} else if (currentState is AsyncError) {
}
inventoryResult.results = currentList;
final jsonmap = inventoryResult.toJson();
storage.writeFile(jsonmap);
}
Сообщайте мне о любых улучшениях или исправлениях в моем коде, спасибо.
Подробнее здесь: https://stackoverflow.com/questions/790 ... rpod-inven