0

使用 Stream 构建器时出现错误。在我的代码中,我使用if(snapshot.data != null) { list.add(snapshot.data); }了但我得到了一个在 null 上调用 add 的 msg。我什至添加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)
...
════════════════════════════════════════════════════════════════════════════════════════════════════


4

1 回答 1

0

你的错误是因为allData是 null 所以你不能调用该add()方法。您可以按如下方式声明变量:

List<FileSystemEntity> alldata = [];

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

于 2020-07-23T10:47:55.237 回答