如何从 userUid() 获取它到 StreamBuilder Flutter

How to get it from userUid() to the StreamBuilder Flutter

我想知道如何将我的函数 userUid() 传递给 StreamBuilder {

FirebaseAuth auth = FirebaseAuth.instance;

  userUid() async {
    final FirebaseUser user = await auth.currentUser;
    final uid = user.uid;
    return  uid;


  }

我想传递它的地方,根据登录的用户 UID 获取文档。

final tabs = [

final documentId = await userUid();
    Center(
        child: (Scaffold(
        body: StreamBuilder (
              stream: FirebaseFirestore.instance.collection('users').document(userUid()).snapshots(),
                builder: (context, snapshot) {
                  if (snapshot.data == null) return CircularProgressIndicator();

                  return Container(
                    padding: EdgeInsets.all(16),
                    child: ListView.builder(

当我在 final tabs =[

中传递函数 final documentId = await useruid();

我收到错误
error: Expected an identifier. (missing_identifier at lib/pages/home.dart:39) error: Expected to find ']'. (expected_token at lib/pages/home.dart:39)

idk 我可以做些什么来传递这个函数...

有人知道示例文档,这样我就可以研究如何做,

完整代码:

final tabs = [

  final documentId = await userUid();
    Center(

        child: (Scaffold(
        body: StreamBuilder (
              stream: FirebaseFirestore.instance.collection('users').document(userUid()).snapshots(),
                builder: (context, snapshot) {
                  if (snapshot.data == null) return CircularProgressIndicator();

                  return Container(
                    padding: EdgeInsets.all(16),
                    child: ListView.builder(
                        itemCount: snapshot.data.documents.length,
                        itemBuilder: (context, index) {
                          DocumentSnapshot manuais =
                          snapshot.data.documents[index];

                          return Card(
                            color: Colors.grey[250],
                            child: Container(
                              padding: EdgeInsets.all(10),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,

                                children: <Widget>[

                                  new Image.asset('Images/pdflogo.png', width: 32,
                                  ),
                                  Center(
                                    child: Text(
                                      (manuais.data()['nome'].toString()),
                                      maxLines: 1,
                                      overflow: TextOverflow.ellipsis,
                                      style: TextStyle(fontSize: 16),

                                    ),
                                  ),

                                  ButtonBar(
                                    children: <Widget>[

                                      FlatButton(
                                          child: const Text('Compartilhar / Download'),
                                          onPressed: () async {
                                            var request = await HttpClient().getUrl(Uri.parse(manuais.data()['documento']));
                                            var response = await request.close();Uint8List bytes = await consolidateHttpClientResponseBytes(response);
                                            await Share.file(
                                                'ESYS AMLOG',
                                                'Manual.pdf',
                                                bytes,
                                                'image/jpg');
                                          }),

                                    ],
                                  ),
                                ],
                              ),
                            ),
                          );
                        }),
                  );
                })))),
    Center(
        child: (Scaffold(
            body: StreamBuilder(
                stream: FirebaseFirestore.instance.collection('producao').snapshots(),
                builder: (context, snapshot) {
                  if (snapshot.data == null) return CircularProgressIndicator();

                  return Container(
                    padding: EdgeInsets.all(16),
                    child: ListView.builder(
                        itemCount: snapshot.data.documents.length,
                        itemBuilder: (context, index) {
                          DocumentSnapshot producao =
                          snapshot.data.documents[index];

                          return Card(
                            color: Colors.grey[250],
                            child: Container(
                              padding: EdgeInsets.all(10),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: <Widget>[
                                  Center(
                                    child: Image.network(producao.data()['img'].toString(), width: 260,
                                    ),

                                  ),
                                  Text(
                                    (producao.data()['data'].toString()),
                                    maxLines: 1,
                                    overflow: TextOverflow.ellipsis,
                                    style: TextStyle(fontSize: 22),
                                  ),
                                  Text(
                                    (producao.data()['detail'].toString()),
                                    style: TextStyle(fontSize: 16),
                                  ),
                                  ButtonBar(
                                    children: <Widget>[

                                      FlatButton(
                                          child: const Text('DETALHES'),
                                          onPressed: () {
                                            ImageViewer.showImageSlider(
                                              images: [
                                                (producao.data()['img']),
                                                //List of images' URLs
                                              ],
                                            );

                                          }),
                                      FlatButton(
                                          child: const Text('COMPARTILHAR'),
                                          onPressed: () async {

                                            var request = await HttpClient().getUrl(Uri.parse(producao.data()['img']));
                                            var response = await request.close();Uint8List bytes = await consolidateHttpClientResponseBytes(response);
                                            await Share.file(
                                                'ESYS AMLOG',
                                                'amlog.jpg',
                                                bytes,
                                                'image/jpg'
                                            );
                                          }),
                                    ],
                                  ),
                                ],
                              ),
                            ),
                          );
                        }),
                  );
                })))),
    Center(child: Text('Documentos')),
  ]; //<<<<<<<<<<<<<<< here closes the tab

````

 









您不能在 List 中定义变量。你可以做两件事:

  1. List 实例化之前声明 documentId
final documentId = await userUid();
final tabs = [
  ...
];
  1. 创建一个 returns 列表的函数:
/// ... simulates the parameters of the function
List<Widget> getWidgetList(...) async {
  final documentId = await userUid();
  final List<Widget> list = []; 
  // To insert in a final list use the method add(...)
  ...
  // Do your logic here
  ...

  return list;
}

final tabs = getWidgetList(...);