0

The idea

I want to display followers. the page take list of followers user id and then display their username.


Error

when I tried to I get an Error say type 'Future<dynamic>' is not a subtype of type 'Widget' The issue in this line Text(user["username"]),


Code

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';

class Following extends StatefulWidget {
  final following ;
  const Following({Key? key, required this.following}) : super(key: key);

  @override
    _FollowingState createState() => _FollowingState();
}

 class _FollowingState extends State<Following> {
   /*attribute*/
   var following =[];
   bool islouded = false;
   var usersData= [];


  @override
   void initState() {
     super.initState();
     setState(() {
       following = widget.following;
     });
     getFollowing();
   }

   void getFollowing() {
     for(var user in following){
       setState(() {
         print(user);
         // print(getUser(user));
         usersData.add( getUser(user));
       });
     }
     setState(() {
       islouded = true;
     });
   }

   getUser(uid)async{
     try {
       if (uid != null) {
         var userSnap = await FirebaseFirestore.instance
             .collection('users')
             .doc(uid)
             .get();
         var userData = userSnap.data()!;

         // print(userSnap.data()!["username"].toString());
         return userData;
       }
     }catch(e){
       showSnackBar(context, e.toString());
     }
   }

   @override
   Widget build(BuildContext context) {
     return Scaffold(
         body: !islouded?
       const Center(
         child: CircularProgressIndicator(),
       ):following.isNotEmpty?
       Column(
         children: [
           for(var user in usersData)
             Text(user["username"]),
         ],
       ):Text("No following yet!"),
     );
   }

 }

Tried

I tried use FutureBuilder but I did not how to use it right because it return nothing. I believe I'm using it wrong.

the code as follow:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';

class Following extends StatefulWidget {
  final following ;
  const Following({Key? key, required this.following}) : super(key: key);

  @override
  _FollowingState createState() => _FollowingState();
}

class _FollowingState extends State<Following> {
  /*attribute*/
  var following =[];
  bool islouded = false;
  var usersData= [];



@override
void initState() {
  super.initState();
  setState(() {
    following = widget.following;
  });
  getFollowing();
}

void getFollowing() {
  for(var user in following){
    setState(() {
      print(user);
      // print(getUser(user));
      usersData.add( getUser(user));
    });
  }
  setState(() {
    islouded = true;
  });
}

getUser(uid) async{
  try {
    if (uid != null) {
      var userSnap = await FirebaseFirestore.instance
          .collection('users')
          .doc(uid)
          .get();
      return userSnap;

      // print(userSnap.data()!["username"].toString());
      // return userData;
    }
  }catch(e){
    print(e.toString());
  }
}

@override
Widget build(BuildContext context) {
  return Scaffold(
      body: !islouded?
    const Center(
      child: CircularProgressIndicator(),
    ):following.isNotEmpty?
    Column(
      children: [
        for(var user in usersData)
          FutureBuilder(
              future: user,
              builder: (context, snapshot){
                switch(snapshot.connectionState){
                  case ConnectionState.none:
                    return Text("No following yet!");
                  case ConnectionState.active:
                    return Text("active");
                  case ConnectionState.waiting:
                    return Center(
                      child: CircularProgressIndicator(),
                    );
                  case ConnectionState.done:
                    print(user);//Instance of 'Future<dynamic>'
                    print(snapshot);//AsyncSnapshot<Object?>(ConnectionState.done, Instance of '_JsonDocumentSnapshot', null, null)
                    return Text("username");//i want to display username but getting different error
                  default:
                    return Text("No following yet");
                }
              }
          )
          // Text(user["username"]),
      ],
    ):Text("No following yet!"),
  );
}}

Thank you for taking the time reading my question. I hope you have beautiful day like you <3

Shatha
  • 35
  • 6
  • I suggest you turn on your linter first and follow the advice. There are lots of errors in your project that you do not see, because you did not follow best practices, but instead went with making all types "dynamic" to hide those mistakes. Getting warning is a good thing, because then you can fix them instead of running into those mistakes later on. – nvoigt May 10 '22 at 15:03

1 Answers1

0

I feel this may be the culprit: usersData.add( getUser(user));.

Try this instead: await usersData.add( getUser(user));.

As you call the async method getUser(user) async { ... } it returns a Future, and this Future gets added to the List not the user. This would explain the error complaining about an unexpected Future.

Dabbel
  • 2,468
  • 1
  • 8
  • 25