【问题标题】:Streambuilder Null Error. Variable returning NullStreambuilder 空错误。返回 Null 的变量
【发布时间】:2020-03-25 16:40:22
【问题描述】:

我正在尝试将字符串变量传递到 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(),

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-01-28
      • 2020-12-19
      • 2020-07-12
      • 2021-04-19
      • 2019-10-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-13
      • 2020-01-18
      相关资源
      最近更新 更多