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


I have got an error while working with Stream builders.
In my code, I used if( != null) { list.add(; } but I m getting a msg that add was called on null. I even added print('') statement to check if is null, but it is working fine with print() too. So I m not at all understanding how to solve this error. Someone please respond. Thanks in advance.

Here is my code

class Wrapper extends StatefulWidget {

  _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);
  void initState() {
  Widget build(BuildContext context) {

    List<FileSystemEntity> alldata;

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


And this is the result:

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<anonymous closure> (package:easyscan/wrapper.dart:53:25)
#2 (package:flutter/src/widgets/async.dart:509:81)
#3 (package:flutter/src/widgets/async.dart:127:48)
#4 (package:flutter/src/widgets/framework.dart:4619:28)


Your error is because allData is null so you cannot call the add() method.
You can declare your variable as follow:

List<FileSystemEntity> alldata = [];

By doing this allData will be initialized as an empty list and you will be able to call its methods.

Answered By – Guillaume Roux

Answer Checked By – Willingham (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *