1

嗨,我正在尝试使用 Streambuilder 创建简单的实时数据库,同时返回一个 Listview.builder,其中是一个 ListTile。我希望让 ListTile 能够移动到其他飞镖。但是,当我尝试传递该项目时,我设法做到,但是当我尝试打印它时,它会在终端中打印。

 class ListItemsDisplay extends StatefulWidget {
      @override
      State<StatefulWidget> createState() {
        return _ListItemsDisplay();
      }
    }

    class _ListItemsDisplay extends State<ListItemsDisplay> {
      @override
      Widget build(BuildContext context) {
        return new StreamBuilder(
            stream: Firestore.instance.collection("posts").snapshots(),
            builder: (context, snapshot) {
              if (!snapshot.hasData) {
                return Center(
                  child: Text("Loading.."),
                );
              } else {
                return ListView.builder(
                    itemCount: snapshot.data.documents.length,
                    itemBuilder: (context, index)`enter code here` {
                      if (snapshot.data.documents[index]['title'] != null &&
                          snapshot.data.documents[index]['title'] != "" &&
                          snapshot.data.documents[index]['title'] != " ") {
                        print(
                            "MY ID: " + snapshot.data.documents[index].documentID);
                        return ListTile(
                            title: Text(snapshot.data.documents[index]['title']),
                            onTap: () {
                              if (snapshot.data.documents[index]['title'] != 

    null) {**//AS YOU CAN SEE HERE IM TRYING TO PASS the document id in DISPLAYITEM() CLASS**
    print("MY ITEM ID ""TAPPED"" IF NOT NULL: "+snapshot.data.documents[index].documentID);
                                Navigator.push(
                                    context,
                                    MaterialPageRoute(
                                        builder: (context) => DisplayItem(snapshot.data.documents[index].documentID)));
                              } else {
                                print("MY ITEM ID ""TAPPED"" IF NULL: "+snapshot.data.documents[index].documentID);
                              }
                            });
                      } else {
                        return ListTile(
                          title: Text("Missing title [REPORT TO ADMIN]"),
                        );
                      }
                    });
              }
            });
      }
    }

这是我的 DisplayItem 类。

class DisplayItem extends StatelessWidget {
  final String _myId;
  DisplayItem(this._myId);
  String getTitle(String myId) {
    if(myId.isNotEmpty && myId!=null) {
      Firestore.instance.collection("posts").document(myId).get().then((value) {
        if (value.data['title'].toString() != null &&
            value.data['title'].toString() != "" &&
            value.data['title'].toString() != " ")
          return value.data['title'].toString();
        else
          return "NONE FOUND";
      });
    }
    else
      return "HELLO";
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
        appBar: AppBar(
        title: Text("TITLE"),
    ),
    body: Column(
    children: <Widget>[
      Text(getTitle(_myId)),
    ],
    ),
    ),
    );
    }
  }

我正在尝试构建一个实时数据库(firestore),在单击任何列出的项目时,颤动将在其中显示项目列表;它会将用户重定向到另一个 dart 文件,该 dart 文件将在其中显示项目的更多描述。

这是错误:

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following assertion was thrown building DisplayItem(dirty):
A non-null String must be provided to a Text widget.
'package:flutter/src/widgets/text.dart':
Failed assertion: line 298 pos 10: 'data != null'

The relevant error-causing widget was: 
  DisplayItem file:///D:/Programming/Flutter_Projects/flutter_study/lib/main.dart:64:59
When the exception was thrown, this was the stack: 
#2      new Text (package:flutter/src/widgets/text.dart:298:10)
#3      DisplayItem.build (package:flutterstudy/Display.dart:31:7)
#4      StatelessElement.build (package:flutter/src/widgets/framework.dart:4576:28)
#5      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4502:15)
#6      Element.rebuild (package:flutter/src/widgets/framework.dart:4218:5)
...
════════════════════════════════════════════════════════════════════════════════════════════════════

HERE IS THE ID IM TRYING TO PASS
I/flutter (11339): MY NOT NULL ID: sc94LjZJz57t947YgTlp

当我点击这个(DisplayItem file:///D:/Programming/Flutter_Projects/flutter_study/lib/main.dart:64:59)

我在这里重定向: builder: (context) => DisplayItem(snapshot.data.documents[index].documentID)));

HERE's WHAT HAPPENS:
as soon as I ran, I added a print method that will print document ID of items.
I/flutter (13386): MY ID: sc94LjZJz57t947YgTlp
I/flutter (13386): MY ID: xqbkvhVveYhr3YvgSGuE

Now I clicked any of the Item

IT Shows the **ERROR ON TERMINAL** then it displays the last method I added in onTap, which print method:
I/flutter (13386): MY ITEM ID TAPPED IF NOT NULL: sc94LjZJz57t947YgTlp
4

0 回答 0