skip to Main Content

Testing for something that might not exist, is spawning risk of a null
but what is the option to acknowledge that and assign good results to a non-null variable?

For example [path_provider getExternalStorageDirectory();] might reasonably respond null in the case there is no external storage.

How can a Directory be assigned, if it does exist, with that risk that might null?

Assigning a temporary variable to test for the null, doesn’t work.

Error is consistently then

A value of type ‘Directory?’ can’t be assigned to a variable of type ‘Directory’.

2

Answers


  1. Chosen as BEST ANSWER

    Reading replies then as "nullable forces nullable", which is workable but odd there is not that option to remove the null.


  2. getExternalStorageDirectory() return nullable Directory from future, you can make data type as nullable.

    Directory? directory =  await getExternalStorageDirectory();
    

    Find more about understanding-null-safety

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search