Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Null check operator used on a null value – Flutter

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.



  Future<void> _listGoogleDriveFiles() async {
    final googleSignIn = signin.GoogleSignIn.standard(
      scopes: ['https://www.googleapis.com/auth/drive.appdata'],
    );

    final GoogleSignInAccount? account = await googleSignIn.signIn();

    final authHeaders = await account?.authHeaders;
    final authenticateClient = GoogleAuthClient(authHeaders!);
    final driveApi = drive.DriveApi(authenticateClient);
    print('above files list');

    list = await driveApi.files.list();

    setState(() {});

  }


 @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: () {
                          downloadGoogleDriveFile(
                              filename: list!.files![index].originalFilename,
                              id: list!.files![index].id);
                        },
                      ),
                    );
                  },
                ),
              )
          ],
        ),
      ),
      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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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'];
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading