diff --git a/lib/widgets/new_item.dart b/lib/widgets/new_item.dart index 11cfa02..5c3a58e 100644 --- a/lib/widgets/new_item.dart +++ b/lib/widgets/new_item.dart @@ -1,4 +1,9 @@ +import 'dart:convert'; + import 'package:flutter/material.dart'; + +import 'package:http/http.dart' as http; + import 'package:flutter_shopping_list/data/categories.dart'; import 'package:flutter_shopping_list/models/category.dart'; import 'package:flutter_shopping_list/models/grocery_item.dart'; @@ -18,16 +23,31 @@ class _NewItemState extends State { var _enteredQuantity = 1; var _selectedCategory = categories[Categories.vegetables]!; - void _saveItem() { + void _saveItem() async { if (_formKey.currentState!.validate()) { _formKey.currentState!.save(); - Navigator.of(context).pop( - GroceryItem( - id: DateTime.now().toString(), - name: _enteredName, - quantity: _enteredQuantity, - category: _selectedCategory), + final url = Uri.https('flutter-prep-dccfe-default-rtdb.firebaseio.com', + 'shopping-list.json'); + final response = await http.post( + url, + headers: {'Content-Type': 'application/json'}, + body: json.encode( + { + 'name': _enteredName, + 'quantity': _enteredQuantity, + 'category': _selectedCategory.title, + }, + ), ); + + print(response.body); + print(response.statusCode); + + if (!context.mounted) { + return; + } + + Navigator.of(context).pop(); } } diff --git a/pubspec.lock b/pubspec.lock index 00dd3be..dcddf63 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -75,6 +75,22 @@ packages: description: flutter source: sdk version: "0.0.0" + http: + dependency: "direct main" + description: + name: http + sha256: a2bbf9d017fcced29139daa8ed2bba4ece450ab222871df93ca9eec6f80c34ba + url: "https://pub.dev" + source: hosted + version: "1.2.0" + http_parser: + dependency: transitive + description: + name: http_parser + sha256: "2aa08ce0341cc9b354a498388e30986515406668dbcc4f7c950c3e715496693b" + url: "https://pub.dev" + source: hosted + version: "4.0.2" lints: dependency: transitive description: @@ -168,6 +184,14 @@ packages: url: "https://pub.dev" source: hosted version: "0.7.0" + typed_data: + dependency: transitive + description: + name: typed_data + sha256: facc8d6582f16042dd49f2463ff1bd6e2c9ef9f3d5da3d9b087e244a7b564b3c + url: "https://pub.dev" + source: hosted + version: "1.3.2" vector_math: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 1732d2a..e793584 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -35,6 +35,7 @@ dependencies: # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.2 + http: ^1.2.0 dev_dependencies: flutter_test: