类型 'Future<dynamic>' 不是类型 'Widget' 的子类型。扑

type 'Future<dynamic>' is not a subtype of type 'Widget'. flutter

想法

我想显示关注者。该页面获取关注者用户 ID 列表,然后显示他们的用户名。


错误

当我尝试时我得到一个错误说 type 'Future<dynamic>' is not a subtype of type 'Widget' 这一行的问题Text(user["username"]),


代码

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!"),
     );
   }

 }

尝试过

我尝试使用 FutureBuilder 但我不知道如何正确使用它,因为它 return 什么都没有。我相信我用错了。

代码如下:

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!"),
  );
}}

感谢您花时间阅读我的问题。我希望你有像你一样美好的一天 <3

我觉得这可能是罪魁祸首: usersData.add( getUser(user));.

试试这个:await usersData.add( getUser(user));.

当您调用异步方法 getUser(user) async { ... } 时,它 returns 一个 Future,这个 Future 被添加到列表中而不是 user。这将解释抱怨意外未来的错误。