skip to Main Content

My Question is a bit similar to How can I get my bool value out of a future<bool>? but i couldn’t get an aswer to my question from it. I have the function in a seperate file because i also want to use it elsewhere in my code. I am trying to create a function that checks if the current user is the creator of the document. For that i use an async method in my function so it has to be Future but if I run my App i get this error: wrong return type error

The field userId in my Database is defined and saves the userId of the Author when it is created.

this is the function:

 Future<bool> isUserAuthor(documentId) async {
      var userId = '';
      String? currentUserId = FirebaseAuth.instance.currentUser?.uid;
      var collection = FirebaseFirestore.instance.collection('notes');
      var docSnapshot = await collection.doc(documentId).get();
      if (docSnapshot.exists) {
        userId = docSnapshot.data()?['userId'];
      } else {
        userId = 'no User available';
      }
      if (userId == currentUserId) {
        return true;
      } else {
        return false;
      }
    }

and this is where i use it:

typedef NoteCallback = void Function(CloudNote note);

class NotesListView extends StatelessWidget {
  final Iterable<CloudNote> notes;
  final NoteCallback onDeleteNote;
  final NoteCallback onTap;


  const NotesListView({
    Key? key,
    required this.notes,
    required this.onDeleteNote,
    required this.onTap,
  }) : super(key: key);
  

  

  @override
  Widget build(BuildContext context) {
    
    return ListView.builder(
      itemCount: notes.length,
      itemBuilder: (context, index) {
        final note = notes.elementAt(index);
        bool isvisible = isUserAuthor(note.documentId) as bool;
        return ListTile(
          onTap: () {
            onTap(note);
          },
        
          title: Text(
            note.textJob,
            maxLines: 1,
            softWrap: true,
            overflow: TextOverflow.ellipsis,
          ),
          trailing: 
            Visibility(
              visible: isvisible,
            child:  IconButton(
            
            onPressed: () async {
              
              final shouldDelete = await showDeleteDialog(context);
              if (shouldDelete) {
                onDeleteNote(note);
              }
            },
            icon: const Icon(Icons.delete),
          ),)
           
          
        );
      },
    );
  }
}

If you have any other wuestiones to the rest of my code please write me. I don’t programm very long and for that don’t know what else you would need.

3

Answers


  1. In cases like this you need to use a FutureBuilder

    here is a link: https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html

    Login or Signup to reply.
  2. Save the response of the isUserAuthor function in boolean variable and then use it.
    Also call isUserAuthor function before the execution of ListView.builder.

    Login or Signup to reply.
  3. I think that line is the source of the error

    bool isvisible = isUserAuthor(note.documentId) as bool;
    

    isUserAuthor() , is an asynchronous function the returns a boolean value in the future.

    So, you should wait for it to get the complete result, eg:

     itemBuilder: (context, index) async{
           
            bool isvisible = await isUserAuthor(note.documentId) ;
            ....
              },
    

    and of course you must mark your function (itemBuilder) as asynchronous.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search