0

我正在尝试将字符串变量传递到 Streambuilder 中的流中,但它正在返回

'必须向文本小部件提供非空字符串。'package:flutter/src/widgets/text.dart':断言失败:第 269 行 pos 10:'data != null'' 错误。

我检查了变量,它没有通过打印函数返回空值。

class NoteStream extends StatefulWidget {
  final String reqDataBase;

  NoteStream({this.reqDataBase});

  @override
  _NoteStreamState createState() => _NoteStreamState();
}
class _NoteStreamState extends State<NoteStream> {
  String database;

  @override
  void initState() {
    database = '${widget.reqDataBase}Notes';
    print('Data is : $database');
    //I need to fix the database
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
      stream: Firestore.instance.collection(database).snapshots(),
4

1 回答 1

4

您不能直接发送数据,您必须通过 Text 小部件发送数据,如下例所示

StreamBuilder(
        stream: Firestore.instance.collection(database).snapshots(),
        builder: (context, snapshot) {
          switch (snapshot.connectionState) {
            case ConnectionState.waiting:
              /// While waiting for the data to load, show a loading spinner.
              return Center(child: CircularProgressIndicator());
            default:
              if (snapshot.hasError) {
                return Center(child: Text('Error: ${snapshot.error}'));
              } else {
                if (snapshot.data == null) {
                  return Text('No data to show');
                } else {
                  /// if we want to do some data manipulation we 
                  /// can do before it sending to a widget.
                  return Text(snapshot.data);
                }
              }
          }
        });
于 2019-11-30T12:56:03.303 回答