无法显示快照中的字符串

时间:2019-10-25 06:52:57

标签: flutter google-cloud-firestore

我想从Firestore中获取数据并显示在文本中,但有一个错误弹出。 塔尔错误说'_BroadcsastStream'类型不是'String'类型的子类型。 我上课叫方法

void main() => runApp(MaterialApp(
  home : profUser(),
));

class profUser extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home : new profiUser(),
    );
  }
}

class profiUser extends StatefulWidget {
  @override
  _profiUserState createState() => _profiUserState();
}

class _profiUserState extends State<profiUser> {    
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.indigo,
      appBar: AppBar(
        title: Text('Profile'),
        actions: <Widget>[
          IconButton(
            icon: Icon(choices[1].icon),
            onPressed: (){
              logOut();
              Navigator.push(context,MaterialPageRoute(builder: (context)
              => myLogin()));
            },
          ),
        ],
      ),

      body: Column(
        children: <Widget>[
          userDetail(),
        ],
      ),
    );
  }
}

根据Firestore中的数据显示文本的类

class userDetail extends StatelessWidget{
  @override
  Widget build(BuildContext context){
    return new StreamBuilder(
        stream: UniQuery().getUserDetail(),
        builder: (BuildContext context, snapshot){
          if(!snapshot.hasData)
            return new Text('Loading..... Please wait');

          var userDocument = snapshot.data;
          return new Column(
            children: <Widget>[
              Text('Name: ' + userDocument['name']),
              Text('Age: ' + userDocument['age'],toString()),
              Text('Address: ' + userDocument['address']),
              Text('Result: ' + userDocument['result']),
            ],
          );
        }
    );
  }
}

在上述代码中用于从Firestore获取数据的空白

var userID;

      Future<String> getCurrentUser() async{
    final FirebaseUser user = await FirebaseAuth.instance.currentUser();
//    return uID != null ? uID : null;
    final String uID = user.uid.toString();
    return uID;
  }

  setUserDetail() async{
    userID = await getCurrentUser();
  }

1 个答案:

答案 0 :(得分:0)

问题出在getUserDetail()内部,您正在将异步函数传递给documents(),因此该函数返回Future。在调用documents()之前,您应该首先获取字符串,等待异步调用完成,然后将字符串传递给documents()。