I'm currently testing and trying out a new settings page for a Flutter application. I'm new to Flutter so I was following along with a tutorial, however I got an error that the tutorial video didn't have through VS Code which says:
The named parameter 'username' isn't defined. Try correcting the name to an existing named parameter's name, or defining a named parameter with the name 'username'.dartundefined_named_parameter
and
The parameter 'username' can't have a value of 'null' because of its type, but the implicit default value is 'null'. Try adding either an explicit non-'null' default value or the 'required' modifier.dartmissing_default_value_for_parameter
Which VS Code underlined the "username" variable in red in both files (lines 217 and 8 respectively). I was testing around with the variable and look up solutions online, but I couldn't find an incredibly simple solution that I understood. I'm sorry if this a very simple issue, but I am new to Flutter and don't understand what's wrong. Thank you for your time.
The overall goal of the code is to save the username and display it in other pages.
main.dart File (Lines 148 - 222)
// Settings Page & Account Information
class Settings extends StatelessWidget {
Settings({Key? key}) : super(key: key);
final _usernameController = TextEditingController();
@override
Widget build(BuildContext context) {
final theme = MediaQuery.of(context).platformBrightness == Brightness.dark
? 'DarkTheme'
: 'LightTheme';
return Scaffold(
appBar: AppBar(title: const Text('Settings'), actions: <Widget>[
IconButton(
onPressed: () async {
_saveSettings;
},
icon: const Icon(Icons.save),
tooltip: 'Save Settings')
]),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.fromLTRB(8, 8, 8, 8),
child: Column(
children: [
Column(
// Account
children: [
const Padding(
padding: EdgeInsets.fromLTRB(0, 12, 0, 0),
child: Text('Account Information',
style: TextStyle(
fontSize: 17.0,
))),
Padding(
padding: const EdgeInsets.fromLTRB(12, 0, 12, 12),
child: TextField(
controller: _usernameController,
inputFormatters: [LengthLimitingTextInputFormatter(25)],
decoration: InputDecoration(
hintText: 'Username',
labelText: 'Username',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0)),
),
),
),
],
),
Container(
child: Column(
// App Settings
children: [
// SwitchListTile(value: DarkMode, onChanged: Light => Dark => Light)
// ChangeThemeButtonWidget(),
TextButton(
onPressed: _saveSettings,
child: const Text('Save Settings'))
],
),
),
],
),
)));
}
void _saveSettings() {
final newSettings = Settings(
username: _usernameController.text,
);
print(newSettings);
}
}
saved_data.dart File
import 'package:shared_preferences/shared_preferences.dart';
import 'package:bit/main.dart';
class Settings {
final String username;
Settings({
this.username,
});
}
CodePudding user response:
You should consider using different name for both classes. Also, for 2nd Settings class the message is clear:
Try adding either an explicit non-'null' default value or the 'required' modifier.dartmissing_default_value_for_parameter
so, Settings become:
class SettingsModel {
final String username;
SettingsModel({
required this.username,
});
}
and, Settings should be:
void _saveSettings() {
final newSettings = SettingsModel(
username: _usernameController.text,
);
}