0

in this below code _invoiceInformation in first initialize is null and i'm trying to use dart null safe to manage that in my flutter applications

in this code although i used ? operation i still get error:

Error:

RangeError (index): Index out of range: no indices are valid: 0 

what i want to try:

_invoiceInformation = Hive.box<InvoiceInformation>('invoice_information');
_province.text=_invoiceInformation?.getAt(0)?.province??'';
0

1 Answer 1

2
_province.text=_invoiceInformation?.getAt(0)?.province??'';

If the list _invoiceInformation is empty i.e having zero elements, then the null aware operator ? will allow you to access the element at 0, which is causing the error.

You will also have to check whether the list is empty or not before accessing its elements.

if(_invoiceInformation != null && _invoiceInformation.isNotEmpty) {
    _province.text=_invoiceInformation.getAt(0)?.province ?? '';
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.