title: Flutter error: A value of type 'Object?' can't be assigned to a variable of type 'String'
body: <p>I am using a dropdownbuttonfield and getting this error:</p>
  <pre><code>A value of type 'Object?' can't be assigned to a variable of type 'String'.
  Try changing the type of the variable, or casting the right-hand type to 'String'.dart(invalid_assignment)
  </code></pre>
  <p>Code :</p>
  <pre><code>    class SettingsForm extends StatefulWidget {
    @override
    _SettingsFormState createState() =&gt; _SettingsFormState();
  }
  
  class _SettingsFormState extends State&lt;SettingsForm&gt; {
    final _formKey = GlobalKey&lt;FormState&gt;();
    final List&lt;String&gt; sugars = ['0', '1', '2', '3', '4'];
    final List&lt;int&gt; strengths = [100, 200, 300, 400, 500, 600, 700, 800, 900];
  
    // form values
     String? _currentName;
     String? _currentSugars;
     int? _currentStrength;
  
    @override
    Widget build(BuildContext context) {
      MyUser user = Provider.of&lt;MyUser&gt;(context);
  
      return StreamBuilder&lt;UserData&gt;(
          stream: DatabaseService(uid: user.uid).userData,
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              UserData? userData = snapshot.data;
              return Form(
                key: _formKey,
                child: Column(
                  children: &lt;Widget&gt;[
                    Text(
                      'Update your brew settings.',
                      style: TextStyle(fontSize: 18.0),
                    ),
                    SizedBox(height: 20.0),
                    TextFormField(
                      initialValue: userData!.name,
                      decoration: textInputDecoration,
                      validator: (val) =&gt;
                          val!.isEmpty ? 'Please enter a name' : null,
                      onChanged: (val) =&gt; setState(() =&gt; _currentName = val),
                    ),
                    SizedBox(height: 10.0),
                    DropdownButtonFormField(
                      value: _currentSugars ?? userData.sugars,
                      decoration: textInputDecoration,
                      items: sugars.map((sugar) {
                        return DropdownMenuItem(
                          value: sugar,
                          child: Text('$sugar sugars'),
                        );
                      }).toList(),
                      onChanged: (val) =&gt; setState(() =&gt; _currentSugars = val), &lt;--Error here **val** right one
                    ),
                    SizedBox(height: 10.0),
                    Slider(
                      value: (_currentStrength ?? userData.strength).toDouble(),
                      activeColor:
                          Colors.brown[_currentStrength ?? userData.strength],
                      inactiveColor:
                          Colors.brown[_currentStrength ?? userData.strength],
                      min: 100.0,
                      max: 900.0,
                      divisions: 8,
                      onChanged: (val) =&gt;
                          setState(() =&gt; _currentStrength = val.round()),
                    ),
                    ElevatedButton(
                        style:
                            ElevatedButton.styleFrom(primary: Colors.pink[400]),
                        child: Text(
                          'Update',
                          style: TextStyle(color: Colors.white),
                        ),
                        onPressed: () async {
                          if (_formKey.currentState!.validate()) {
                            await DatabaseService(uid: user.uid).updateUserData(
                                _currentSugars ?? snapshot.data!.sugars,
                                _currentName ?? snapshot.data!.name,
                                _currentStrength ?? snapshot.data!.strength);
                            Navigator.pop(context);
                          }
                        }),
                  ],
                ),
              );
            } else {
              return Loading();
            }
          });
    }
  }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/mF6pr.png" rel="nofollow noreferrer">Update</a> <a href="https://i.stack.imgur.com/w1oiV.png" rel="nofollow noreferrer">Update 2</a>
  A value of type 'String?' can't be assigned to a variable of type 'String'.
  Try changing the type of the variable, or casting the right-hand type to 'String'.</p>