Categories
Development

Can’t create a Flutter/Firebase non-authenticated user favorite list in ListTile

I am trying to create a NON-authenticated user icon ‘select favorite’ option where if an icon is clicked ( on a list of titles in a ListView of Firestore data(book titles)) it saves the data to be routed to a new favorites page. I can not find information on how top do this in the Firestore docs, but I found a youtube tutorial that does this for a popular flutter WordPair app. Here is the string used to store their WordPair data:

final _randomWordPairs = <WordPair>[];

and another variable to check to see if WordPair lists were already saved is:

final _savedWordPairs = Set<WordPair>[];

If my Firebase data string is

Firestore.instance.collection("titles").snapshots()

what is the equivalent to the above WordPair variables so I can create this favorites list with a list of my titles

snapshot.data.documents[index]['title']

Nothing works. Still trying to figure it out: variables:

final savedDb = Firestore.instance.collection("books").document('title');

final alreadySaved = Firestore.instance.collection(“books”).document(‘title’);

ListTile code to call it:

trailing: Icon( alreadySaved ?  Icons.favorite :
                        Icons.favorite_border,
                        color: alreadySaved ? Colors.red : null),

                          onTap: () {
                          setState(() {
                            if(alreadySaved) {
                              savedDb.remove('title');
                            }else{
                              savedDb.add('title');
                            }
                          });
                        }

I tried this but now it just crashes that page;

final alreadySaved = Firestore.instance.collection("books").where((item) => item.contains("title"));

List<String> savedDb = ['Firestore.instance.collection("books").document(title)'];

                              trailing: Icon( alreadySaved == null ?  Icons.favorite :
                          Icons.favorite_border,
                              color: alreadySaved != null ? Colors.red : null),

                          onTap: () {
                            setState(() {
                              savedDb.add('title');
                            });
                          },

Last attempt:

bool alreadySaved = false;

savedDb() async{
    DocumentSnapshot ds = await         
Firestore.instance.collection("books").document('title').get();
    this.setState(() {
      alreadySaved = ds.exists;
    });
  }

  @override
  void initState() {
    super.initState();
    savedDb();
  }

and onTap says .remove and .add isn’t defined for type Function, and my icons won’t even change color. This is impossible

                            onTap: () {
                          setState(() {
                            if(alreadySaved) {
                              savedDb.remove('Firestore.instance.collection("books")');
                            }else{
                              savedDb.add(Firestore.instance.collection("books"));
                            }
                          });
                        },

Returns .remove and .add isn’t defined for type Function

3 replies on “Can’t create a Flutter/Firebase non-authenticated user favorite list in ListTile”

Did you figured out on how to do it? Because I am also trying to do the same thing for my wallpaper app I am working on and I am also using a Firebase non-authenticated user approach.

Hey bro im also trying to make wallpaper app with same feature.
Can you contact me? Reply and i will give contact details

Leave a Reply

Your email address will not be published. Required fields are marked *