Streambuilder空错误。变量返回Null

时间:2019-11-30 11:25:06

标签: firebase flutter google-cloud-firestore stream-builder

我正在尝试将String变量传递到Streambuilder中的Stream中,但是它返回

'必须将非空字符串提供给Text小部件。 '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(),

1 个答案:

答案 0 :(得分:2)

您不能直接发送数据,必须通过文本小部件(如以下示例)发送数据

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);
                }
              }
          }
        });