StreamBuilder<FileSystemEntity>(脏,状态:_StreamBuilderBaseState<FileSystemEntity,AsyncSnapshot<FileSystemEntity>>#25d1b)

StreamBuilder<FileSystemEntity>(dirty, state: _StreamBuilderBaseState<FileSystemEntity, AsyncSnapshot<FileSystemEntity>>#25d1b)

我在使用 Stream 构建器时遇到错误。 在我的代码中,我使用了 if(snapshot.data != null) { list.add(snapshot.data); } 但我收到一条消息,在 null 上调用了 add。我什至添加了 print('') 语句来检查 snapshot.data 是否为空,但它也适用于 print() 。所以我完全不明白如何解决这个错误。有人请回应。提前致谢。

这是我的代码

class Wrapper extends StatefulWidget {

  @override
  _WrapperState createState() => _WrapperState();
}

class _WrapperState extends State<Wrapper> {
  Stream<FileSystemEntity> mystream;
  var _directory;
  void getfilesdata() async
  {
    _directory = await getApplicationDocumentsDirectory();
    await Directory('${_directory.path}/data').create(recursive: true);

    mystream = Directory('${_directory.path}/data').list().map((data) => data);

    await Directory('${_directory.path}/documents').create(recursive: true);
  }
  @override
  void initState() {
    super.initState();
    getfilesdata();
  }
  @override
  Widget build(BuildContext context) {

    List<FileSystemEntity> alldata;

    return StreamBuilder<FileSystemEntity>(
      stream: mystream,
      builder: (context, snapshot) {
        if(!snapshot.hasData)
        {
          print('No data yet');
          return Loading();
        }
        else
          {
            if(snapshot.data != null && !snapshot.hasError)
            {
                print(snapshot.data);
                alldata.add(snapshot.data);
            }
            if(snapshot.connectionState==ConnectionState.done)
            {
              return HomeScreen(alldata: alldata);
            }
            else return Loading();
          }

      }
    );
  }
}

这是结果:

Performing hot restart...
Syncing files to device ZUK Z2132...
Restarted application in 1,734ms.
I/flutter (11840): No data yet
I/flutter (11840): No data yet
I/flutter (11840): File: '/data/user/0/com.example.easyscan/app_flutter/data/data[0]'

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following NoSuchMethodError was thrown building StreamBuilder<FileSystemEntity>(dirty, state: _StreamBuilderBaseState<FileSystemEntity, AsyncSnapshot<FileSystemEntity>>#25d1b):
The method 'add' was called on null.
Receiver: null
Tried calling: add(Instance of '_File')

The relevant error-causing widget was: 
  StreamBuilder<FileSystemEntity> file:///home/praneeth/AndroidStudioProjects/easyscan/lib/wrapper.dart:40:12
When the exception was thrown, this was the stack: 
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
#1      _WrapperState.build.<anonymous closure> (package:easyscan/wrapper.dart:53:25)
#2      StreamBuilder.build (package:flutter/src/widgets/async.dart:509:81)
#3      _StreamBuilderBaseState.build (package:flutter/src/widgets/async.dart:127:48)
#4      StatefulElement.build (package:flutter/src/widgets/framework.dart:4619:28)
...
════════════════════════════════════════════════════════════════════════════════════════════════════


您的错误是因为 allData 为空,所以您无法调用 add() 方法。 您可以按如下方式声明您的变量:

List<FileSystemEntity> alldata = [];

这样做 allData 将被初始化为一个空列表,您将能够调用它的方法。