Skip to main content

Creating a list inside of a list- morning code

 So right now I have a collection for the user that holds the list tiles. What I want to do today is create a way for another list to be created when you click on the list tiles. I will use my Shopping list page to display each list. What I will need to do is find a way to pass the title of the list from the list page to the Shopping list page. The list name will be used to identify the document within the user collection. 



Then when you click on the list tile the list name will be passed into the Shopping list page stateful widget.

FlatButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ShoppingListPage(
listName: listName,
)));
},


 I know that I will not be able to access this variable within the rest of my shopping list page code because it is created under the Shopping list page state. So I need to pass the variable from the shopping list page widget to the Shopping list page state widget. When I passed it I changed the variable name from listName to nameOfList.

class ShoppingListPage extends StatefulWidget {
ShoppingListPage({this.listName});
String listName;
@override
_ShoppingListPageState createState() =>
_ShoppingListPageState(nameOfList: listName);
}

class _ShoppingListPageState extends State<ShoppingListPage> {
_ShoppingListPageState({this.nameOfList});
String nameOfList;

From there I can use that variable in my code to create the new collection and associated documents. I also use it to title the Shopping List page. 

return Scaffold(
appBar: AppBar(
backgroundColor: Colors.black87,
elevation: 100,
title: Text(
nameOfList,
style: TextStyle(
shadows: <Shadow>[
Shadow(
color: Colors.black12, blurRadius: 5, offset: Offset(3, 2)),
],
),
),
centerTitle: true,

AND
StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance
.collection(userIdentification)
.doc(nameOfList)
.collection(nameOfList)
.orderBy("timeStamp")
.snapshots(),
AND
FirebaseFirestore.instance
.collection(userIdentification)
.doc(nameOfList)
.collection(nameOfList)
.add({
'item': additionToList,
'timeStamp': DateTime.now()
});

Then I need to pass the list name to the Shopping list widget so that I am able to delete items within the new list. 


class ShoppingList extends StatelessWidget {
ShoppingList({this.item, this.id, this.nameOfList});

final item;
final id;
final nameOfList;
AND
final items = snapshot.data.docs;
List<ShoppingList> shoppingList = [];
for (var item in items) {
final newItem = item.data()['item'];
final id = item.id;
final listBlock = ShoppingList(
item: newItem,
id: id,
nameOfList: nameOfList,
);
shoppingList.add(listBlock);
}
AND
FirebaseFirestore.instance
.collection(userIdentification)
.doc(nameOfList)
.collection(nameOfList)
.doc(id)
.delete();

End the end I am now able to add and delete items in my new list.



Comments