Null check operator used on a null value – Flutter

Issue

I was trying to make a google drive app that lists files from the drive. but I got Null check operator used on a null value error. I got it what’s happening. but I could not solve it.

 @override
  Widget build(BuildContext context) {
    return Scaffold(
         body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            TextButton(
              onPressed: () {},
              child: Text('UPLOAD'),
            ),
            if (list != null)
              SizedBox(
                height: 300,
                width: double.infinity,
                child: ListView.builder(
                  shrinkWrap: true,
                  itemCount: list!.files?.length,
                  itemBuilder: (context, index) {
                    final title = list!.files![index].originalFilename;
                    return ListTile(
                      leading: Text(title!),
                      trailing: ElevatedButton(
                        child: Text('Download'),
                        onPressed: () {
                        },
                      ),
                    );
                  },
                ),
              )
          ],
        ),
      ),
      floatingActionButton: Row(
        children: [
          FloatingActionButton(
            onPressed: _listGoogleDriveFiles,
            child: Icon(Icons.photo),
          ),
          FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: const Icon(Icons.add),
          ),
        ],
      ),
    );
  }
}


When I run it upload text is showing and the error is showing below the text. So error must be due to list is null. but I just want to show the list only if it is not null.

What to do?

Solution

The error means that you have used the null check operator (the exclamation mark) on something that instead happened to be null at runtime. So, looking at your code, it’s not only the list that might be null, but also the other objects you marked with !.

The problem is somewhere here, unless I missed some ! in your code:

itemCount: list!.files?.length,
itemBuilder: (context, index) {
    final title = list!.files![index].originalFilename;
    return ListTile(
      leading: Text(title!),
      trailing: ElevatedButton(
        child: Text('Download'),
        onPressed: () {
          downloadGoogleDriveFile(
              filename: list!.files![index].originalFilename,
              id: list!.files![index].id);
        },
      ),
    );
},

To avoid encountering that error using !, you can explicitly check whether an object is null, something like:

if (list == null) {
   [...some error handling or a message with a warning]
} else {
   [your original code where you can use ! without fear of errors]
}

or you can assign a value to an object only in the case it is null, like this:

title??= ['Default title for when some loading failed or something'];

Answered By – il_boga

Answer Checked By – Katrina (FlutterFixes Volunteer)

Leave a Reply

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