'Range Error(index): invalid value: valid value range is empty

im trying to set initial value for 'title' but it keeps showing me the error: range error (index): invalid value: valid value range is empty

class _ProductCreateState extends State<ProductCreate> {
 final Map<String, dynamic> formData = {

   'title': '',
   'description': '',
   'price': 0.0,
   'image': 'images/images (2).jpeg'
 };
    final GlobalKey<FormState> formKey = GlobalKey<FormState>();

 Widget _buildTitleTextField() {
   return Padding(
     padding: const EdgeInsets.only(right: 30),
     child: TextFormField(
       initialValue: widget.stuff[0]['title'],
       decoration: InputDecoration(
         labelText: 'Product Title',
       ),
validator: (String? value) {
if (value!.isEmpty || value.length < 5) {
           //   (||) means or
           return 'Title is required and should be 5+ characters';
         }
       },

       onSaved: (String? value) {
         setState(() {
           formData['title'] = value!;
           // titleValue = value!;
         });
       },
     ),
   );
 }



Solution 1:[1]

Before accessing the widget.stuff[0] directly please do handle for the null as well as is that the array is empty in the buildTextFormField method.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abdul Kadhar